Animate an Object Along a Path





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I strted to play around with shapes in wpf, I need the following: I have an image and I draw some shape, I want the the image would walk on the lines of this shape.



I mean like:



enter image description here



For example with the shape:



<Path Stroke="Black" StrokeThickness="5" Fill="Goldenrod">
<Path.Data>
<PathGeometry>
<PathGeometry.Figures>
<PathFigure StartPoint="100,50" IsClosed="True">
<LineSegment Point="140,60"/>
<LineSegment Point="150,100"/>
<LineSegment Point="125,120"/>
<LineSegment Point="90,110"/>
<LineSegment Point="80,80"/>
</PathFigure>
</PathGeometry.Figures>
</PathGeometry>
</Path.Data>
</Path>


And here is the moving image:



<UserControl ...
xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options" ..>

<UserControl.Resources>
<PathGeometry x:Key="AnimationPath"
Figures="M 10,100 C 40,0 300,0 300,300 0,300 285,200 300,300 "
PresentationOptions:Freeze="True" />
</UserControl.Resources>

<Image
Source="/Resources/Myimage.png"
Width="200" >
<Image.RenderTransform>
<TranslateTransform x:Name="AnimatedTranslateTransform" />
</Image.RenderTransform>

<Image.Triggers>
<EventTrigger RoutedEvent="Path.Loaded">
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever">

<!-- Animates the rectangle horizotally along the path. -->
<DoubleAnimationUsingPath
Storyboard.TargetName="AnimatedTranslateTransform"
Storyboard.TargetProperty="X"
PathGeometry="{StaticResource AnimationPath}"
Source="X"
Duration="0:0:5"
AutoReverse="True"
/>

<!-- Animates the rectangle vertically along the path. -->
<DoubleAnimationUsingPath
Storyboard.TargetName="AnimatedTranslateTransform"
Storyboard.TargetProperty="Y"
PathGeometry="{StaticResource AnimationPath}"
Source="Y"
Duration="0:0:5" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Image.Triggers>
</Image>


Related links: link1, link2



I have tried like:



 Figures="M 10,100 C 100,50 140,60 150,100 125,120 90,110 80,80 "


i.e start at point 100,50 --> 140,60 and so on...



but it doesnt go exactly on this path










share|improve this question























  • @Clemens it doesn't let me to add this property, are you familiar with wpf? can you please help with this?

    – BugsFixer
    Jan 3 at 15:20


















1















I strted to play around with shapes in wpf, I need the following: I have an image and I draw some shape, I want the the image would walk on the lines of this shape.



I mean like:



enter image description here



For example with the shape:



<Path Stroke="Black" StrokeThickness="5" Fill="Goldenrod">
<Path.Data>
<PathGeometry>
<PathGeometry.Figures>
<PathFigure StartPoint="100,50" IsClosed="True">
<LineSegment Point="140,60"/>
<LineSegment Point="150,100"/>
<LineSegment Point="125,120"/>
<LineSegment Point="90,110"/>
<LineSegment Point="80,80"/>
</PathFigure>
</PathGeometry.Figures>
</PathGeometry>
</Path.Data>
</Path>


And here is the moving image:



<UserControl ...
xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options" ..>

<UserControl.Resources>
<PathGeometry x:Key="AnimationPath"
Figures="M 10,100 C 40,0 300,0 300,300 0,300 285,200 300,300 "
PresentationOptions:Freeze="True" />
</UserControl.Resources>

<Image
Source="/Resources/Myimage.png"
Width="200" >
<Image.RenderTransform>
<TranslateTransform x:Name="AnimatedTranslateTransform" />
</Image.RenderTransform>

<Image.Triggers>
<EventTrigger RoutedEvent="Path.Loaded">
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever">

<!-- Animates the rectangle horizotally along the path. -->
<DoubleAnimationUsingPath
Storyboard.TargetName="AnimatedTranslateTransform"
Storyboard.TargetProperty="X"
PathGeometry="{StaticResource AnimationPath}"
Source="X"
Duration="0:0:5"
AutoReverse="True"
/>

<!-- Animates the rectangle vertically along the path. -->
<DoubleAnimationUsingPath
Storyboard.TargetName="AnimatedTranslateTransform"
Storyboard.TargetProperty="Y"
PathGeometry="{StaticResource AnimationPath}"
Source="Y"
Duration="0:0:5" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Image.Triggers>
</Image>


Related links: link1, link2



I have tried like:



 Figures="M 10,100 C 100,50 140,60 150,100 125,120 90,110 80,80 "


i.e start at point 100,50 --> 140,60 and so on...



but it doesnt go exactly on this path










share|improve this question























  • @Clemens it doesn't let me to add this property, are you familiar with wpf? can you please help with this?

    – BugsFixer
    Jan 3 at 15:20














1












1








1


1






I strted to play around with shapes in wpf, I need the following: I have an image and I draw some shape, I want the the image would walk on the lines of this shape.



I mean like:



enter image description here



For example with the shape:



<Path Stroke="Black" StrokeThickness="5" Fill="Goldenrod">
<Path.Data>
<PathGeometry>
<PathGeometry.Figures>
<PathFigure StartPoint="100,50" IsClosed="True">
<LineSegment Point="140,60"/>
<LineSegment Point="150,100"/>
<LineSegment Point="125,120"/>
<LineSegment Point="90,110"/>
<LineSegment Point="80,80"/>
</PathFigure>
</PathGeometry.Figures>
</PathGeometry>
</Path.Data>
</Path>


And here is the moving image:



<UserControl ...
xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options" ..>

<UserControl.Resources>
<PathGeometry x:Key="AnimationPath"
Figures="M 10,100 C 40,0 300,0 300,300 0,300 285,200 300,300 "
PresentationOptions:Freeze="True" />
</UserControl.Resources>

<Image
Source="/Resources/Myimage.png"
Width="200" >
<Image.RenderTransform>
<TranslateTransform x:Name="AnimatedTranslateTransform" />
</Image.RenderTransform>

<Image.Triggers>
<EventTrigger RoutedEvent="Path.Loaded">
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever">

<!-- Animates the rectangle horizotally along the path. -->
<DoubleAnimationUsingPath
Storyboard.TargetName="AnimatedTranslateTransform"
Storyboard.TargetProperty="X"
PathGeometry="{StaticResource AnimationPath}"
Source="X"
Duration="0:0:5"
AutoReverse="True"
/>

<!-- Animates the rectangle vertically along the path. -->
<DoubleAnimationUsingPath
Storyboard.TargetName="AnimatedTranslateTransform"
Storyboard.TargetProperty="Y"
PathGeometry="{StaticResource AnimationPath}"
Source="Y"
Duration="0:0:5" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Image.Triggers>
</Image>


Related links: link1, link2



I have tried like:



 Figures="M 10,100 C 100,50 140,60 150,100 125,120 90,110 80,80 "


i.e start at point 100,50 --> 140,60 and so on...



but it doesnt go exactly on this path










share|improve this question














I strted to play around with shapes in wpf, I need the following: I have an image and I draw some shape, I want the the image would walk on the lines of this shape.



I mean like:



enter image description here



For example with the shape:



<Path Stroke="Black" StrokeThickness="5" Fill="Goldenrod">
<Path.Data>
<PathGeometry>
<PathGeometry.Figures>
<PathFigure StartPoint="100,50" IsClosed="True">
<LineSegment Point="140,60"/>
<LineSegment Point="150,100"/>
<LineSegment Point="125,120"/>
<LineSegment Point="90,110"/>
<LineSegment Point="80,80"/>
</PathFigure>
</PathGeometry.Figures>
</PathGeometry>
</Path.Data>
</Path>


And here is the moving image:



<UserControl ...
xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options" ..>

<UserControl.Resources>
<PathGeometry x:Key="AnimationPath"
Figures="M 10,100 C 40,0 300,0 300,300 0,300 285,200 300,300 "
PresentationOptions:Freeze="True" />
</UserControl.Resources>

<Image
Source="/Resources/Myimage.png"
Width="200" >
<Image.RenderTransform>
<TranslateTransform x:Name="AnimatedTranslateTransform" />
</Image.RenderTransform>

<Image.Triggers>
<EventTrigger RoutedEvent="Path.Loaded">
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever">

<!-- Animates the rectangle horizotally along the path. -->
<DoubleAnimationUsingPath
Storyboard.TargetName="AnimatedTranslateTransform"
Storyboard.TargetProperty="X"
PathGeometry="{StaticResource AnimationPath}"
Source="X"
Duration="0:0:5"
AutoReverse="True"
/>

<!-- Animates the rectangle vertically along the path. -->
<DoubleAnimationUsingPath
Storyboard.TargetName="AnimatedTranslateTransform"
Storyboard.TargetProperty="Y"
PathGeometry="{StaticResource AnimationPath}"
Source="Y"
Duration="0:0:5" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Image.Triggers>
</Image>


Related links: link1, link2



I have tried like:



 Figures="M 10,100 C 100,50 140,60 150,100 125,120 90,110 80,80 "


i.e start at point 100,50 --> 140,60 and so on...



but it doesnt go exactly on this path







wpf xaml shapes






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 9:57









BugsFixerBugsFixer

1178




1178













  • @Clemens it doesn't let me to add this property, are you familiar with wpf? can you please help with this?

    – BugsFixer
    Jan 3 at 15:20



















  • @Clemens it doesn't let me to add this property, are you familiar with wpf? can you please help with this?

    – BugsFixer
    Jan 3 at 15:20

















@Clemens it doesn't let me to add this property, are you familiar with wpf? can you please help with this?

– BugsFixer
Jan 3 at 15:20





@Clemens it doesn't let me to add this property, are you familiar with wpf? can you please help with this?

– BugsFixer
Jan 3 at 15:20












1 Answer
1






active

oldest

votes


















2














Your sketch seems to indicate that you want to animate kind of a red arrow along the path, including a rotation to the tangent angle of the current path segment.



You could achieve this by animating the Matrix property of a MatrixTransform with a MatrixAnimationUsingPath. The example below uses an additional TranslateTransform to center the image. As there is a DrawingImage in the Source property of the Image element, you may as well use another Path instead of an Image.



<Window.Resources>
<PathGeometry x:Key="AnimationPath"
Figures="M100,50 L140,60 150,100 125,120 90,110 80,80Z"/>
</Window.Resources>
<Canvas>
<Path Stroke="Black" StrokeThickness="5" Fill="Goldenrod"
Data="{StaticResource AnimationPath}"/>

<Image>
<Image.Source>
<DrawingImage>
<DrawingImage.Drawing>
<GeometryDrawing Geometry="M0,0 L10,8 0,16">
<GeometryDrawing.Pen>
<Pen Thickness="3" Brush="Red"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingImage.Drawing>
</DrawingImage>
</Image.Source>

<Image.RenderTransform>
<TransformGroup>
<TranslateTransform X="-5" Y="-8"/>
<MatrixTransform x:Name="AnimatedTransform"/>
</TransformGroup>
</Image.RenderTransform>

<Image.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever">
<MatrixAnimationUsingPath
Storyboard.TargetName="AnimatedTransform"
Storyboard.TargetProperty="Matrix"
Duration="0:0:5"
DoesRotateWithTangent="True"
PathGeometry="{StaticResource AnimationPath}"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Image.Triggers>
</Image>
</Canvas>





share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019929%2fanimate-an-object-along-a-path%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Your sketch seems to indicate that you want to animate kind of a red arrow along the path, including a rotation to the tangent angle of the current path segment.



    You could achieve this by animating the Matrix property of a MatrixTransform with a MatrixAnimationUsingPath. The example below uses an additional TranslateTransform to center the image. As there is a DrawingImage in the Source property of the Image element, you may as well use another Path instead of an Image.



    <Window.Resources>
    <PathGeometry x:Key="AnimationPath"
    Figures="M100,50 L140,60 150,100 125,120 90,110 80,80Z"/>
    </Window.Resources>
    <Canvas>
    <Path Stroke="Black" StrokeThickness="5" Fill="Goldenrod"
    Data="{StaticResource AnimationPath}"/>

    <Image>
    <Image.Source>
    <DrawingImage>
    <DrawingImage.Drawing>
    <GeometryDrawing Geometry="M0,0 L10,8 0,16">
    <GeometryDrawing.Pen>
    <Pen Thickness="3" Brush="Red"/>
    </GeometryDrawing.Pen>
    </GeometryDrawing>
    </DrawingImage.Drawing>
    </DrawingImage>
    </Image.Source>

    <Image.RenderTransform>
    <TransformGroup>
    <TranslateTransform X="-5" Y="-8"/>
    <MatrixTransform x:Name="AnimatedTransform"/>
    </TransformGroup>
    </Image.RenderTransform>

    <Image.Triggers>
    <EventTrigger RoutedEvent="Loaded">
    <BeginStoryboard>
    <Storyboard RepeatBehavior="Forever">
    <MatrixAnimationUsingPath
    Storyboard.TargetName="AnimatedTransform"
    Storyboard.TargetProperty="Matrix"
    Duration="0:0:5"
    DoesRotateWithTangent="True"
    PathGeometry="{StaticResource AnimationPath}"/>
    </Storyboard>
    </BeginStoryboard>
    </EventTrigger>
    </Image.Triggers>
    </Image>
    </Canvas>





    share|improve this answer






























      2














      Your sketch seems to indicate that you want to animate kind of a red arrow along the path, including a rotation to the tangent angle of the current path segment.



      You could achieve this by animating the Matrix property of a MatrixTransform with a MatrixAnimationUsingPath. The example below uses an additional TranslateTransform to center the image. As there is a DrawingImage in the Source property of the Image element, you may as well use another Path instead of an Image.



      <Window.Resources>
      <PathGeometry x:Key="AnimationPath"
      Figures="M100,50 L140,60 150,100 125,120 90,110 80,80Z"/>
      </Window.Resources>
      <Canvas>
      <Path Stroke="Black" StrokeThickness="5" Fill="Goldenrod"
      Data="{StaticResource AnimationPath}"/>

      <Image>
      <Image.Source>
      <DrawingImage>
      <DrawingImage.Drawing>
      <GeometryDrawing Geometry="M0,0 L10,8 0,16">
      <GeometryDrawing.Pen>
      <Pen Thickness="3" Brush="Red"/>
      </GeometryDrawing.Pen>
      </GeometryDrawing>
      </DrawingImage.Drawing>
      </DrawingImage>
      </Image.Source>

      <Image.RenderTransform>
      <TransformGroup>
      <TranslateTransform X="-5" Y="-8"/>
      <MatrixTransform x:Name="AnimatedTransform"/>
      </TransformGroup>
      </Image.RenderTransform>

      <Image.Triggers>
      <EventTrigger RoutedEvent="Loaded">
      <BeginStoryboard>
      <Storyboard RepeatBehavior="Forever">
      <MatrixAnimationUsingPath
      Storyboard.TargetName="AnimatedTransform"
      Storyboard.TargetProperty="Matrix"
      Duration="0:0:5"
      DoesRotateWithTangent="True"
      PathGeometry="{StaticResource AnimationPath}"/>
      </Storyboard>
      </BeginStoryboard>
      </EventTrigger>
      </Image.Triggers>
      </Image>
      </Canvas>





      share|improve this answer




























        2












        2








        2







        Your sketch seems to indicate that you want to animate kind of a red arrow along the path, including a rotation to the tangent angle of the current path segment.



        You could achieve this by animating the Matrix property of a MatrixTransform with a MatrixAnimationUsingPath. The example below uses an additional TranslateTransform to center the image. As there is a DrawingImage in the Source property of the Image element, you may as well use another Path instead of an Image.



        <Window.Resources>
        <PathGeometry x:Key="AnimationPath"
        Figures="M100,50 L140,60 150,100 125,120 90,110 80,80Z"/>
        </Window.Resources>
        <Canvas>
        <Path Stroke="Black" StrokeThickness="5" Fill="Goldenrod"
        Data="{StaticResource AnimationPath}"/>

        <Image>
        <Image.Source>
        <DrawingImage>
        <DrawingImage.Drawing>
        <GeometryDrawing Geometry="M0,0 L10,8 0,16">
        <GeometryDrawing.Pen>
        <Pen Thickness="3" Brush="Red"/>
        </GeometryDrawing.Pen>
        </GeometryDrawing>
        </DrawingImage.Drawing>
        </DrawingImage>
        </Image.Source>

        <Image.RenderTransform>
        <TransformGroup>
        <TranslateTransform X="-5" Y="-8"/>
        <MatrixTransform x:Name="AnimatedTransform"/>
        </TransformGroup>
        </Image.RenderTransform>

        <Image.Triggers>
        <EventTrigger RoutedEvent="Loaded">
        <BeginStoryboard>
        <Storyboard RepeatBehavior="Forever">
        <MatrixAnimationUsingPath
        Storyboard.TargetName="AnimatedTransform"
        Storyboard.TargetProperty="Matrix"
        Duration="0:0:5"
        DoesRotateWithTangent="True"
        PathGeometry="{StaticResource AnimationPath}"/>
        </Storyboard>
        </BeginStoryboard>
        </EventTrigger>
        </Image.Triggers>
        </Image>
        </Canvas>





        share|improve this answer















        Your sketch seems to indicate that you want to animate kind of a red arrow along the path, including a rotation to the tangent angle of the current path segment.



        You could achieve this by animating the Matrix property of a MatrixTransform with a MatrixAnimationUsingPath. The example below uses an additional TranslateTransform to center the image. As there is a DrawingImage in the Source property of the Image element, you may as well use another Path instead of an Image.



        <Window.Resources>
        <PathGeometry x:Key="AnimationPath"
        Figures="M100,50 L140,60 150,100 125,120 90,110 80,80Z"/>
        </Window.Resources>
        <Canvas>
        <Path Stroke="Black" StrokeThickness="5" Fill="Goldenrod"
        Data="{StaticResource AnimationPath}"/>

        <Image>
        <Image.Source>
        <DrawingImage>
        <DrawingImage.Drawing>
        <GeometryDrawing Geometry="M0,0 L10,8 0,16">
        <GeometryDrawing.Pen>
        <Pen Thickness="3" Brush="Red"/>
        </GeometryDrawing.Pen>
        </GeometryDrawing>
        </DrawingImage.Drawing>
        </DrawingImage>
        </Image.Source>

        <Image.RenderTransform>
        <TransformGroup>
        <TranslateTransform X="-5" Y="-8"/>
        <MatrixTransform x:Name="AnimatedTransform"/>
        </TransformGroup>
        </Image.RenderTransform>

        <Image.Triggers>
        <EventTrigger RoutedEvent="Loaded">
        <BeginStoryboard>
        <Storyboard RepeatBehavior="Forever">
        <MatrixAnimationUsingPath
        Storyboard.TargetName="AnimatedTransform"
        Storyboard.TargetProperty="Matrix"
        Duration="0:0:5"
        DoesRotateWithTangent="True"
        PathGeometry="{StaticResource AnimationPath}"/>
        </Storyboard>
        </BeginStoryboard>
        </EventTrigger>
        </Image.Triggers>
        </Image>
        </Canvas>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 3 at 16:28

























        answered Jan 3 at 16:23









        ClemensClemens

        90.3k894186




        90.3k894186
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019929%2fanimate-an-object-along-a-path%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith