Smooth text animation (Marquee) using WPF
Asked Answered
D

3

7

Trying to build a marquee control with smooth text animation. Current efforts include:

  • Using translate transform
  • Using animation on Canvas dependency properties (Left, Right)
  • Using animation on custom dependency property (Point) and using drawing visuals (formattedtext)
  • Using CompositionTarget.Rendering

But the animation is still choppy and resource intensive (2-10% CPU).

Test code used in default wpf window which I assume should produce a smooth animation:

   <TextBlock x:Name="_box" FontSize="64" CacheMode="BitmapCache" Text="lorem ipsum">
        <TextBlock.RenderTransform>
            <TranslateTransform x:Name="AnimatedTranslateTransform" X="0" Y="0" />
        </TextBlock.RenderTransform>
        <TextBlock.Triggers>
            <EventTrigger RoutedEvent="TextBlock.Loaded">
              <BeginStoryboard>
                <Storyboard>
                  <DoubleAnimation
                    Storyboard.TargetName="AnimatedTranslateTransform" 
                    Storyboard.TargetProperty="X"
                    From="-300" To="300" Duration="0:0:5" 
                    AutoReverse="True" RepeatBehavior="Forever" />
                </Storyboard>
              </BeginStoryboard>
            </EventTrigger>
          </TextBlock.Triggers>
    </TextBlock>

Checklist:

  • Confirmed no software rendering is taking place (ms performance tool and checking RenderCapability.Tier)
  • Calling freeze on any imaginable object
  • Disabled any bitmap effect and transparency
  • Checked all marquee controls out there (same issues)

Tested on:

  • CPU: Intell core 2 duo (T6600) @2.2Ghz
  • RAM: 4GB
  • GPU: NVidia GeForce 9600M GS (latest drivers)
  • OS: Windows 7 (64bit)

Any ideas (or better yet code example)?

From the responses it seems this is not a wpf issue (other marquee controls work fine for others but not for me), nut I'm getting the same issues on every machine I tested this on.

Diplex answered 9/6, 2010 at 0:9 Comment(1)
It seems there's a problem with some of the computers this was tested on. Also building a release version helps a lot.Diplex
T
1

Hope this may help you - http://jobijoy.blogspot.com/2008/08/silverlight-marquee-control.html

And the WPF version also can be found here

Toadflax answered 9/6, 2010 at 0:12 Comment(4)
Yes, it's one of the controls I looked at (including wpf version) it's not smooth and it is quite resource intensive for such a simple task.Diplex
I tried both the Silverlight and WPF versions of Jobi Joy's sample application on some oldish hardware. Both animated smoothly and used very little resources, even though they were animating much more than a simple marquee. I think there must be something special about your hardware or environment causing the problem. Perhaps you should add detailed hardware, OS, NET Framework specs and resource usage numbers to your question so others can attempt to duplicate your problem.Texas
This is really odd - every machine I've tested this on the text and the image jitters every 2 to 5 seconds. I've updated my question.Diplex
While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.Settler
T
4

Your animation will be handled entirely at the MilCore layer if:

  1. Your TranslateTransform is a RenderTransform (not a LayoutTransform), and
  2. You use a simple animation such as a DoubleAnimation, and
  3. Your object has no clipping or opacity calculations

Try using a DoubleAnimation-animated TranslateTransform for a RenderTransform on a TextBlock that is a direct child of a Window with default settings.

  • If this is still slow, there is something slow about your Direct3D system because managed code is not involved at all and MilCore's calls are very simple, but

  • If it works smoothly and efficiently, incrementally change it to your poorly-performing code to see what change causes the slowdown.

Given your response to Jobi Joy's answer I would suspect the problem is somewhere in your hardware or Direct3D setup, but the only way to find out is to test it.

Texas answered 9/6, 2010 at 0:43 Comment(1)
I've tried that (see updated question) - I'm still having the same issues. Any diagnostics you can think of to pinpoint if there's a problem with machines I'm dealing with?Diplex
F
3

If you are using WPF 4.0, try setting the CacheMode="BitmapCache" (in the XAML) on the element you are animating, in this case, probably a TextBlock.

Fullerton answered 9/6, 2010 at 1:34 Comment(2)
Good idea - but it does not resolve my problem. It seems the fault lies with my machine (OS or HW) but I can't think of anything.Diplex
Holy cow! I'd had a problem with an animation that involved about fifty UI elements. This plagued me for several weeks, but was cleared up with this one attribute. Arg, why did it take me so long to find this?Kwangchow
T
1

Hope this may help you - http://jobijoy.blogspot.com/2008/08/silverlight-marquee-control.html

And the WPF version also can be found here

Toadflax answered 9/6, 2010 at 0:12 Comment(4)
Yes, it's one of the controls I looked at (including wpf version) it's not smooth and it is quite resource intensive for such a simple task.Diplex
I tried both the Silverlight and WPF versions of Jobi Joy's sample application on some oldish hardware. Both animated smoothly and used very little resources, even though they were animating much more than a simple marquee. I think there must be something special about your hardware or environment causing the problem. Perhaps you should add detailed hardware, OS, NET Framework specs and resource usage numbers to your question so others can attempt to duplicate your problem.Texas
This is really odd - every machine I've tested this on the text and the image jitters every 2 to 5 seconds. I've updated my question.Diplex
While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.Settler

© 2022 - 2024 — McMap. All rights reserved.