c# - Color Animation with ease -
i have been following this question make color animation.
i can create animation , runs fine until animation gets started again. there moment feels animation has been started/stopped there moment. want animation run smoothly doesn't show restarting effects in colors. possible that?
i using following code:
<storyboard x:key="gradientanimation" repeatbehavior="forever" storyboard.targetname="backgroundbrush" speedratio="0.3"> <coloranimationusingkeyframes storyboard.targetproperty="(uielement.background).(lineargradientbrush.gradientstops)[0].(gradientstop.color)" enabledependentanimation="true" begintime="-0:0:0.5"> <linearcolorkeyframe keytime="0:0:0" value="black"/> <linearcolorkeyframe keytime="0:0:1" value="red"/> <linearcolorkeyframe keytime="0:0:2" value="black"/> <linearcolorkeyframe keytime="0:0:3" value="red"/> <linearcolorkeyframe keytime="0:0:4" value="black"/> <linearcolorkeyframe keytime="0:0:5" value="red"/> <linearcolorkeyframe keytime="0:0:6" value="black"/> <linearcolorkeyframe keytime="0:0:7" value="red"/> </coloranimationusingkeyframes> <coloranimationusingkeyframes storyboard.targetproperty="(uielement.background).(lineargradientbrush.gradientstops)[1].(gradientstop.color)" enabledependentanimation="true"> <linearcolorkeyframe keytime="0:0:0" value="red"/> <linearcolorkeyframe keytime="0:0:1" value="black"/> <linearcolorkeyframe keytime="0:0:2" value="red"/> <linearcolorkeyframe keytime="0:0:3" value="black"/> <linearcolorkeyframe keytime="0:0:4" value="red"/> <linearcolorkeyframe keytime="0:0:5" value="black"/> <linearcolorkeyframe keytime="0:0:6" value="red"/> <linearcolorkeyframe keytime="0:0:7" value="black"/> </coloranimationusingkeyframes> </storyboard>
and start animation in code-behind:
((storyboard)resources["gradientanimation"]).begin();
maybe there kind of 'easing' method mixes start , stop of animation appears smooth long running animation. suggestions?
you can try easingcolorkeyframe
animation never smooth because it's running on ui thread (i.e. enabledependentanimation="true"
).
here's , bad news. have new gradient brush api @ composition layer level, means it's animatable , running off ui thread. performance wise it's going better current storyboard
solution. it's available in windows 10 insider preview 16225 onward, means it's not going work of current users except insiders.
however, still going post new solution here future reference since there's no sample available on topic yet.
note have added sauce animation make more interesting. if want colors animated, feel free move endpoint
, rotationangleindegrees
animations (i use endpoint
animation animate solid background color gradient @ startup of app , rotationangleindegrees
rotate gradient brush forever).
var compositor = window.current.compositor; // initially, set end point (0,0) 'cause want animate @ start. // if don't want behavior, set different value within (1,1). _gradientbrush = compositor.createlineargradientbrush(); _gradientbrush.endpoint = vector2.zero; // create gradient initial colors. var gradientstop1 = compositor.createcolorgradientstop(); gradientstop1.offset = 0.0f; gradientstop1.color = gradientstop1startcolor; var gradientstop2 = compositor.createcolorgradientstop(); gradientstop2.offset = 1.0f; gradientstop2.color = gradientstop2startcolor; _gradientbrush.colorstops.add(gradientstop1); _gradientbrush.colorstops.add(gradientstop2); // assign gradient brush root element's visual. _backgroundvisual = compositor.createspritevisual(); _backgroundvisual.brush = _gradientbrush; elementcompositionpreview.setelementchildvisual(root, _backgroundvisual); // there 3 animations going on here. // first, kick off endpoint offset animation create special entrance scene. // once it's finished, kick off 2 other animations simultaneously. // these 2 animations include set of gradient stop color animations , // rotation animation rotates gradient brush. var linearease = compositor.createlineareasingfunction(); var batch = compositor.createscopedbatch(compositionbatchtypes.animation); batch.completed += (s, e) => { startgradientcoloranimations(); startgradientrotationanimation(); }; var endpointoffsetanimation = compositor.createvector2keyframeanimation(); endpointoffsetanimation.duration = timespan.fromseconds(3); endpointoffsetanimation.insertkeyframe(1.0f, vector2.one); _gradientbrush.startanimation(nameof(_gradientbrush.endpoint), endpointoffsetanimation); batch.end(); void startgradientcoloranimations() { var color1animation = compositor.createcolorkeyframeanimation(); color1animation.duration = timespan.fromseconds(10); color1animation.iterationbehavior = animationiterationbehavior.forever; color1animation.direction = animationdirection.alternate; color1animation.insertkeyframe(0.0f, gradientstop1startcolor, linearease); color1animation.insertkeyframe(0.5f, color.fromargb(255, 65, 88, 208), linearease); color1animation.insertkeyframe(1.0f, color.fromargb(255, 43, 210, 255), linearease); gradientstop1.startanimation(nameof(gradientstop1.color), color1animation); var color2animation = compositor.createcolorkeyframeanimation(); color2animation.duration = timespan.fromseconds(10); color2animation.iterationbehavior = animationiterationbehavior.forever; color2animation.direction = animationdirection.alternate; color2animation.insertkeyframe(0.0f, gradientstop2startcolor, linearease); color1animation.insertkeyframe(0.5f, color.fromargb(255, 200, 80, 192), linearease); color2animation.insertkeyframe(1.0f, color.fromargb(255, 43, 255, 136), linearease); gradientstop2.startanimation(nameof(gradientstop2.color), color2animation); } void startgradientrotationanimation() { var rotationanimation = compositor.createscalarkeyframeanimation(); rotationanimation.duration = timespan.fromseconds(15); rotationanimation.iterationbehavior = animationiterationbehavior.forever; rotationanimation.insertkeyframe(1.0f, 360.0f, linearease); _gradientbrush.startanimation(nameof(_gradientbrush.rotationangleindegrees), rotationanimation); }
here's working sample , following how looks like. :)
Comments
Post a Comment