{"id":56,"date":"2011-04-06T15:16:00","date_gmt":"2011-04-06T15:16:00","guid":{"rendered":"https:\/\/wdev-blog.azurewebsites.net\/index.php\/2011\/04\/06\/animate-image-left-to-right\/"},"modified":"2011-04-06T15:16:00","modified_gmt":"2011-04-06T15:16:00","slug":"animate-image-left-to-right","status":"publish","type":"post","link":"http:\/\/panahy.nl\/index.php\/2011\/04\/06\/animate-image-left-to-right\/","title":{"rendered":"Animate Image Left To Right"},"content":{"rendered":"<p>The basics of any animation is moving something from one location to the other. In this code snippet I use to Image elements to slide away previously selected image and slide in the newly selected one.<\/p>\n<p><\/p>\n<div>The Image elements need to sit in a Canvas in order to let the Left property make sence.<\/div>\n<div>\n<pre><span style=\"color: #000000;\">&lt;Label x:Name=<\/span><span style=\"color: #800000;\">\"txtFile\"<\/span><span style=\"color: #000000;\">  HorizontalAlignment=<\/span><span style=\"color: #800000;\">\"Stretch\"<\/span><span style=\"color: #000000;\">  \/&gt;<br \/>&lt;Canvas HorizontalAlignment=<\/span><span style=\"color: #800000;\">\"Stretch\"<\/span><span style=\"color: #000000;\"> VerticalAlignment=<\/span><span style=\"color: #800000;\">\"Stretch\"<\/span><span style=\"color: #000000;\">&gt;<br \/>    <br \/>    &lt;Image x:Name=<\/span><span style=\"color: #800000;\">\"newImage\"<\/span><span style=\"color: #000000;\"> Canvas.Left=<\/span><span style=\"color: #800000;\">\"0\"<\/span><span style=\"color: #000000;\"> Canvas.Top=<\/span><span style=\"color: #800000;\">\"0\"<\/span><span style=\"color: #000000;\">  \/&gt;<br \/>    &lt;Image x:Name=<\/span><span style=\"color: #800000;\">\"oldImage\"<\/span><span style=\"color: #000000;\">  Canvas.Left=<\/span><span style=\"color: #800000;\">\"0\"<\/span><span style=\"color: #000000;\"> Canvas.Top=<\/span><span style=\"color: #800000;\">\"0\"<\/span><span style=\"color: #000000;\"> \/&gt;<br \/><br \/>&lt;\/Canvas&gt;<\/span><\/pre>\n<p><\/div>\n<p><\/p>\n<div>assuming that I have already given the previously selected image, my new image file will be given through a function call like <\/p>\n<blockquote><p>AnimateLeftToRight(selectedFile);<\/p><\/blockquote>\n<p>This method will swap the images and create a storyboard to begin animating. I start the newImage from x-position that equals to minus Window.Width and the old one starts from 0 and moves to Window.Width:<\/div>\n<p><\/p>\n<div><\/p>\n<pre><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;summary&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> Begins a story which animates two images from left to right<br \/><\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> the old image takes the source from te new image just before assigning the new one<br \/><\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> and they move together in the distance of Window.Width<br \/><\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;\/summary&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;param name=\"path\"&gt;<\/span><span style=\"color: #008000;\">the path of the new image<\/span><span style=\"color: #808080;\">&lt;\/param&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #0000FF;\">private<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">void<\/span><span style=\"color: #000000;\"> AnimateLeftToRight(<\/span><span style=\"color: #0000FF;\">string<\/span><span style=\"color: #000000;\"> path)<br \/>{<br \/>    oldImage.Source = newImage.Source;<br \/>    newImage.Source = <\/span><span style=\"color: #0000FF;\">new<\/span><span style=\"color: #000000;\"> BitmapImage(<\/span><span style=\"color: #0000FF;\">new<\/span><span style=\"color: #000000;\"> Uri(path));<br \/><br \/>    var myStoryboard = <\/span><span style=\"color: #0000FF;\">new<\/span><span style=\"color: #000000;\"> Storyboard();<br \/><br \/>    myStoryboard.Children.Add(MakeAnimation(-<\/span><span style=\"color: #0000FF;\">this<\/span><span style=\"color: #000000;\">.Width, <\/span><span style=\"color: #FF0000;\">0<\/span><span style=\"color: #000000;\">, <\/span><span style=\"color: #800000;\">\"newImage\"<\/span><span style=\"color: #000000;\">));<br \/>    myStoryboard.Children.Add(MakeAnimation(<\/span><span style=\"color: #FF0000;\">0<\/span><span style=\"color: #000000;\">, <\/span><span style=\"color: #0000FF;\">this<\/span><span style=\"color: #000000;\">.Width, <\/span><span style=\"color: #800000;\">\"oldImage\"<\/span><span style=\"color: #000000;\">));<br \/><br \/>    myStoryboard.Begin(<\/span><span style=\"color: #0000FF;\">this<\/span><span style=\"color: #000000;\">);<br \/>}<\/span><\/pre>\n<p><\/div>\n<p><\/p>\n<div>Making animation can be done in different ways using different EasingFunction, But this one looked easy to understand and look nice on my example:<\/p>\n<p>    <\/p>\n<pre><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;summary&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> Creates a DoubleAnimation that moves the target from left to right<br \/><\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;\/summary&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;param name=\"from\"&gt;<\/span><span style=\"color: #008000;\">starting point X<\/span><span style=\"color: #808080;\">&lt;\/param&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;param name=\"to\"&gt;<\/span><span style=\"color: #008000;\">ending point X<\/span><span style=\"color: #808080;\">&lt;\/param&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;param name=\"target\"&gt;<\/span><span style=\"color: #008000;\">the target element to set the x to <\/span><span style=\"color: #808080;\">&lt;\/param&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;returns&gt;<\/span><span style=\"color: #008000;\">the created animation<\/span><span style=\"color: #808080;\">&lt;\/returns&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #0000FF;\">private<\/span><span style=\"color: #000000;\"> DoubleAnimation MakeAnimation(<\/span><span style=\"color: #0000FF;\">double<\/span><span style=\"color: #000000;\"> from, <\/span><span style=\"color: #0000FF;\">double<\/span><span style=\"color: #000000;\"> to, <\/span><span style=\"color: #0000FF;\">string<\/span><span style=\"color: #000000;\"> target)<br \/>{<br \/>    <\/span><span style=\"color: #0000FF;\">const<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">double<\/span><span style=\"color: #000000;\"> durationSeconds = <\/span><span style=\"color: #FF0000;\">1<\/span><span style=\"color: #000000;\">;<br \/><br \/>    var animation = <\/span><span style=\"color: #0000FF;\">new<\/span><span style=\"color: #000000;\"> DoubleAnimation<br \/>                        {<br \/>                            Duration = <\/span><span style=\"color: #0000FF;\">new<\/span><span style=\"color: #000000;\"> Duration(TimeSpan.FromSeconds(durationSeconds)),<br \/>                            AutoReverse = <\/span><span style=\"color: #0000FF;\">false<\/span><span style=\"color: #000000;\">,<br \/>                            From = from,<br \/>                            To = to,<br \/>                            EasingFunction = <\/span><span style=\"color: #0000FF;\">new<\/span><span style=\"color: #000000;\"> PowerEase()<br \/>                                                 {<br \/>                                                     EasingMode = EasingMode.EaseInOut,<br \/>                                                     Power = <\/span><span style=\"color: #FF0000;\">5<\/span><span style=\"color: #000000;\"><br \/>                                                 }<br \/>                        };<br \/><br \/>    Storyboard.SetTargetName(animation, target);<br \/>    Storyboard.SetTargetProperty(animation, <\/span><span style=\"color: #0000FF;\">new<\/span><span style=\"color: #000000;\"> PropertyPath(Canvas.LeftProperty));<br \/><br \/>    <\/span><span style=\"color: #0000FF;\">return<\/span><span style=\"color: #000000;\"> animation;<br \/>}<\/span><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The basics of any animation is moving something from one location to the other. In this code snippet I use to Image elements to slide away previously selected image and slide in the newly selected one. The Image elements need to sit in a Canvas in order to let the Left property make sence. &lt;Label &hellip; <a href=\"http:\/\/panahy.nl\/index.php\/2011\/04\/06\/animate-image-left-to-right\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Animate Image Left To Right&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[50,52,51],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false},"uagb_author_info":{"display_name":"Pouya Panahy","author_link":"http:\/\/panahy.nl\/index.php\/author\/pouya\/"},"uagb_comment_info":0,"uagb_excerpt":"The basics of any animation is moving something from one location to the other. In this code snippet I use to Image elements to slide away previously selected image and slide in the newly selected one. The Image elements need to sit in a Canvas in order to let the Left property make sence. &lt;Label&hellip;","_links":{"self":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/56"}],"collection":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/comments?post=56"}],"version-history":[{"count":0,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/56\/revisions"}],"wp:attachment":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/media?parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/categories?post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/tags?post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}