strong animation in silverlight - silverlight

I want to make an animation in the Silverlight 4 Expression Blend 4 - a running man.
How is it possible to do, because the only way I see it to be done is to draw him frame by frame with the interval, say, 0.2s?

Your options are bitmap animation or key-frame animation of body parts.
Key-frame animation seems appropriate unless you want photo-realistic movement.
You will need to select regular time internals in Blend and place the various components of the man (legs segments, arm segments etc) in the correct positions for that key frame. Silverlight will do the interpolation between frames.
Build up one complete cycle and set the storyboard property to repeat the sequence.

Related

Rendering video using WriteableBitmap causes choppy animation

So here's my setup:
Camera images coming in at 1920x1080 # 25 FPS
Writing image data to WriteableBitmap on UI thread (simple copy, no processing)
Two Image controls in two different windows on two different monitors has their Source property set to the WriteableBitmap
Some generic UI stuff goes over the camera images
This works great, uses about 4% CPU on an old laptop (8 logical processors). The video is as smooth as can be. However, my UI has some animations (stuff moving around). When the camera display is running, those animations gets choppy.
Right now, the camera image is in Gray8 format, so it will be converted (I guess when calling WritePixels?). I also tried forcing one of the animations to 25 FPS too, no change.
Where should I start to resolve this? Is the issue that I'm locking the bitmap for too long, or is there something else going on? From what I can see locking the bitmap will cause the render thread to block, so moving that to another thread seems pointless. And it does feel like that somewhat defeats the purpose of WriteableBitmap.
This is always going to be tricky because you're capturing at 25FPS whilst WPF tries to update at 60. It's difficult to offer any meaninful advice without seeing a testable project but I'd probably start by doing the updates in a CompositionTarget.Rendering handler.

Optimizing Canvas animation

In my XAML UI, I have 4 canvas panels.
At a time only one is visible and others are collapsed.
I am using transform to have horizontal sliding effect while displaying a canvas.
I have two options to do this:
option1:
When I want to goto a Canvas, I move all other 3 canvases out of screen because i dont know which one is currently active. Advantage of this is I have only 4 animations, one for each canvas.
option2:
I define new animation for going to a particular canvas from another particular canvas. In this case I do not need to move all three canvases but only one. But then I need to create12 animations, 3 for each canvas.
I think option 1 is memory efficient since only 4 animation instances are there but there is CPU performance overhead.
Option 2 is CPU efficient but causes memory overhead due to 12 animation instances.
Which option should i prefer?
Thanks,
Tushar
Just out of interest, have you considered using the VisualStateManager? By defining an 'OnScreen' and 'OffScreen' VisualState via a style, and applying that to each of the Canvases, you would only have to code it once, let the VisualStateManager call the whichever animation is appropriate by using VisualStateManager.GoToState() which means you don't need 12 animation instances, and you don't need to call 4 animations every state change. Best of both worlds?
If you need more information, I can provide some example code shortly.
it looks like you've thought through the implications of each approach rather well. Unfortunately, only you can answer the question that you've posed. If memory is of concern to your application, then you should go with option 1. But if your application is sensitive to being CPU-bound, then you should go with option 2.
I know that's largely a non-answer ... but as it always is with performance, you should do some profiling and testing of both approaches. You may find that the relative benefits or drawbacks of both approaches is negligible, in which case you should go with whatever option is more maintainable and easier for you to code (probably option 1) :-)

Smooth Video Transitions for WPF

I'm using VB.net 2010 and WPF 4. I need to have a smooth transition between two videos played on the mediaelement. I absolutely cannot use anything that requires me to use a winhost in the WPF window, as that will make my project impossible (since the video is full screen, and the controls are over the video)
Basically, I need for the video to play through, and then smoothly go to another video specified in code behind. I cannot splice the two videos together - they must be separate.
How do I have the videos transition smoothly, with no "blink"?
I'm guessing without testing here. You're probably going to need some CPU cores and a good video card.
If you have the memory, use two MediaElements.
Queue up both videos, one on each element.
Set the opacity of the second one to completely transparent. They're UIElements so this should work...
Use timers of some kind keyed from the start of playback on the first one so that you get an event a couple of seconds before playback ends.
With that event delegate, start the video in the second MediaElement, animate the first one's opacity to zero while simultaneously animating the second one to fully opaque.
If you need to do it again, set up the timer again and make sure your delegate animates things the other way.

Prevent WPF stutter / dropped frames

I've written a simple game-like app in WPF. The number of objects drawn is well within WPF capabilities - something like a few hundred ellipses and lines with simple fills. I have a DispatcherTimer to adjust the positions of the objects from time to time (1/60th of a second).
The code to compute the new positions can be quite intensive when there are lots of objects, and can fully load a processor. Whenever this occurs, WPF starts skipping frames, presumably trying to compensate for the "slowness" of my application.
What I would much rather happen is for all the frames to be drawn anyway, only slower. The dropped frames do not add any speed - because visual updates were pretty quick anyway.
Can I somehow force WPF to have my changes to the visuals be reflected on the screen regardless of whether WPF thinks it's a good idea?
Unfortunately I don't think there's anything you can do about this, although I will happily be corrected! WPF is designed to be an application creation framework, not a games library, so it priortises application performance and "usability" over framerate. This actually works very well when producing applications as it allows you to use quite rich animations and effects while maintaining perceived performance on lower end systems.
The only thing I think you might be able to try is push your movement code's Dispatcher priority down slightly to below Render (Loaded is the next one down) using something like:
this.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, MoveMyStuff);
I don't have any kind of test harness to verify if that will help though.
This issue was fixed by using a Canvas with an OnRender override instead of creating and moving UIElements. This does mean that everything needs to be drawn by hand in OnRender, but it can now run at any FPS consistently, without skipping any frames.

WPF Virtualizing a Canvas

We have a series of rectangles produced inside a Canvas and they are nested in this order ScrollView>Canvas>VirtualizingStackPanel>Rectangles in the XAML.
There seems to be little information on Microsoft about under what senarios Virtualization is supported, and in cases where it isn't no error is thrown, it merely treats it like a standard StackPanel.
Given that our view has roughly 60 rectangles on the screen at any one time out of a total of about 800 on the whole canvas the loading times of about 5 minutes are unacceptably slow.
We have already tryed removing the ScrollView to no effect ( other than removing the scroll bars the performance didn't improve), and removing some of the other nesting elements, the net effect seems to be either nothing is displayed, or its displayed but still slow.
The best solution I've found so far a sample piece of free ware code VirtualizedCanvas but its very very limited and doesn't support anything nested inside it, infact the only input it takes is a collection of UI Elements.
This solution basically ignores the databinding and requires a you to pass a completed Canvas back to it, this renders all our existing code and converters pretty useless, and would be major rework to implement.
Is there a way to simply clip the Virtualize the Canvas to only draw our 60 on screen items?

Resources