How to move a picture to where a cursor clicked on the canvas












0















I have an image of a character and I want to be able to move it to the position of where my/the user's cursor is clicking on my canvas. for example-if the user clicked on the middle of the screen, I want his animal to walk there gradually, (at a certain speed). Does anyone know how I can achieve that?










share|improve this question



























    0















    I have an image of a character and I want to be able to move it to the position of where my/the user's cursor is clicking on my canvas. for example-if the user clicked on the middle of the screen, I want his animal to walk there gradually, (at a certain speed). Does anyone know how I can achieve that?










    share|improve this question

























      0












      0








      0


      1






      I have an image of a character and I want to be able to move it to the position of where my/the user's cursor is clicking on my canvas. for example-if the user clicked on the middle of the screen, I want his animal to walk there gradually, (at a certain speed). Does anyone know how I can achieve that?










      share|improve this question














      I have an image of a character and I want to be able to move it to the position of where my/the user's cursor is clicking on my canvas. for example-if the user clicked on the middle of the screen, I want his animal to walk there gradually, (at a certain speed). Does anyone know how I can achieve that?







      c# visual-studio uwp






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 1 at 13:56









      KarolineKaroline

      505




      505
























          1 Answer
          1






          active

          oldest

          votes


















          3















          How to move a picture to where a cursor clicked on the canvas




          For your requirement, you could create DoubleAnimation for image. You could get your cursor clicked position with PointerPressed event then pass the position X,Y value to DoubleAnimation ToProperty. I created the sample you could refer.



          Code behind



          public MainPage()
          {
          this.InitializeComponent();
          RootCanvasLayout.PointerPressed += new PointerEventHandler(Pointer_Pressed);
          }

          private void Pointer_Pressed(object sender, PointerRoutedEventArgs e)
          {
          Windows.UI.Input.PointerPoint currentPoint = e.GetCurrentPoint(RootCanvasLayout);
          CreateAnimation(currentPoint);
          }
          void CreateAnimation(PointerPoint point)
          { var duration = new Duration(TimeSpan.FromMilliseconds(1000));
          DoubleAnimation doubleAnimationX = new DoubleAnimation();
          DoubleAnimation doubleAnimationY = new DoubleAnimation();
          doubleAnimationX.To = point.Position.X-Pic.ActualWidth/2;
          doubleAnimationX.Duration = duration;
          doubleAnimationY.To = point.Position.Y-Pic.ActualHeight/2;
          doubleAnimationY.Duration = duration;
          var conStoryboard = new Storyboard();
          conStoryboard.Children.Add(doubleAnimationX);
          conStoryboard.Children.Add(doubleAnimationY);
          Storyboard.SetTarget(doubleAnimationX, Pic);
          Storyboard.SetTarget(doubleAnimationY, Pic);
          Storyboard.SetTargetProperty(doubleAnimationX, "(Canvas.Left)");
          Storyboard.SetTargetProperty(doubleAnimationY, "(Canvas.Top)");
          conStoryboard.Begin();
          }


          Xaml



          <Canvas Name="RootCanvasLayout" Background="Ivory">
          <Image Name="Pic" Width="100" Height="100"
          Source="Assets/hello.png"
          Canvas.Left="0"
          Canvas.Top = "0"/>
          </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%2f53996043%2fhow-to-move-a-picture-to-where-a-cursor-clicked-on-the-canvas%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









            3















            How to move a picture to where a cursor clicked on the canvas




            For your requirement, you could create DoubleAnimation for image. You could get your cursor clicked position with PointerPressed event then pass the position X,Y value to DoubleAnimation ToProperty. I created the sample you could refer.



            Code behind



            public MainPage()
            {
            this.InitializeComponent();
            RootCanvasLayout.PointerPressed += new PointerEventHandler(Pointer_Pressed);
            }

            private void Pointer_Pressed(object sender, PointerRoutedEventArgs e)
            {
            Windows.UI.Input.PointerPoint currentPoint = e.GetCurrentPoint(RootCanvasLayout);
            CreateAnimation(currentPoint);
            }
            void CreateAnimation(PointerPoint point)
            { var duration = new Duration(TimeSpan.FromMilliseconds(1000));
            DoubleAnimation doubleAnimationX = new DoubleAnimation();
            DoubleAnimation doubleAnimationY = new DoubleAnimation();
            doubleAnimationX.To = point.Position.X-Pic.ActualWidth/2;
            doubleAnimationX.Duration = duration;
            doubleAnimationY.To = point.Position.Y-Pic.ActualHeight/2;
            doubleAnimationY.Duration = duration;
            var conStoryboard = new Storyboard();
            conStoryboard.Children.Add(doubleAnimationX);
            conStoryboard.Children.Add(doubleAnimationY);
            Storyboard.SetTarget(doubleAnimationX, Pic);
            Storyboard.SetTarget(doubleAnimationY, Pic);
            Storyboard.SetTargetProperty(doubleAnimationX, "(Canvas.Left)");
            Storyboard.SetTargetProperty(doubleAnimationY, "(Canvas.Top)");
            conStoryboard.Begin();
            }


            Xaml



            <Canvas Name="RootCanvasLayout" Background="Ivory">
            <Image Name="Pic" Width="100" Height="100"
            Source="Assets/hello.png"
            Canvas.Left="0"
            Canvas.Top = "0"/>
            </Canvas>





            share|improve this answer




























              3















              How to move a picture to where a cursor clicked on the canvas




              For your requirement, you could create DoubleAnimation for image. You could get your cursor clicked position with PointerPressed event then pass the position X,Y value to DoubleAnimation ToProperty. I created the sample you could refer.



              Code behind



              public MainPage()
              {
              this.InitializeComponent();
              RootCanvasLayout.PointerPressed += new PointerEventHandler(Pointer_Pressed);
              }

              private void Pointer_Pressed(object sender, PointerRoutedEventArgs e)
              {
              Windows.UI.Input.PointerPoint currentPoint = e.GetCurrentPoint(RootCanvasLayout);
              CreateAnimation(currentPoint);
              }
              void CreateAnimation(PointerPoint point)
              { var duration = new Duration(TimeSpan.FromMilliseconds(1000));
              DoubleAnimation doubleAnimationX = new DoubleAnimation();
              DoubleAnimation doubleAnimationY = new DoubleAnimation();
              doubleAnimationX.To = point.Position.X-Pic.ActualWidth/2;
              doubleAnimationX.Duration = duration;
              doubleAnimationY.To = point.Position.Y-Pic.ActualHeight/2;
              doubleAnimationY.Duration = duration;
              var conStoryboard = new Storyboard();
              conStoryboard.Children.Add(doubleAnimationX);
              conStoryboard.Children.Add(doubleAnimationY);
              Storyboard.SetTarget(doubleAnimationX, Pic);
              Storyboard.SetTarget(doubleAnimationY, Pic);
              Storyboard.SetTargetProperty(doubleAnimationX, "(Canvas.Left)");
              Storyboard.SetTargetProperty(doubleAnimationY, "(Canvas.Top)");
              conStoryboard.Begin();
              }


              Xaml



              <Canvas Name="RootCanvasLayout" Background="Ivory">
              <Image Name="Pic" Width="100" Height="100"
              Source="Assets/hello.png"
              Canvas.Left="0"
              Canvas.Top = "0"/>
              </Canvas>





              share|improve this answer


























                3












                3








                3








                How to move a picture to where a cursor clicked on the canvas




                For your requirement, you could create DoubleAnimation for image. You could get your cursor clicked position with PointerPressed event then pass the position X,Y value to DoubleAnimation ToProperty. I created the sample you could refer.



                Code behind



                public MainPage()
                {
                this.InitializeComponent();
                RootCanvasLayout.PointerPressed += new PointerEventHandler(Pointer_Pressed);
                }

                private void Pointer_Pressed(object sender, PointerRoutedEventArgs e)
                {
                Windows.UI.Input.PointerPoint currentPoint = e.GetCurrentPoint(RootCanvasLayout);
                CreateAnimation(currentPoint);
                }
                void CreateAnimation(PointerPoint point)
                { var duration = new Duration(TimeSpan.FromMilliseconds(1000));
                DoubleAnimation doubleAnimationX = new DoubleAnimation();
                DoubleAnimation doubleAnimationY = new DoubleAnimation();
                doubleAnimationX.To = point.Position.X-Pic.ActualWidth/2;
                doubleAnimationX.Duration = duration;
                doubleAnimationY.To = point.Position.Y-Pic.ActualHeight/2;
                doubleAnimationY.Duration = duration;
                var conStoryboard = new Storyboard();
                conStoryboard.Children.Add(doubleAnimationX);
                conStoryboard.Children.Add(doubleAnimationY);
                Storyboard.SetTarget(doubleAnimationX, Pic);
                Storyboard.SetTarget(doubleAnimationY, Pic);
                Storyboard.SetTargetProperty(doubleAnimationX, "(Canvas.Left)");
                Storyboard.SetTargetProperty(doubleAnimationY, "(Canvas.Top)");
                conStoryboard.Begin();
                }


                Xaml



                <Canvas Name="RootCanvasLayout" Background="Ivory">
                <Image Name="Pic" Width="100" Height="100"
                Source="Assets/hello.png"
                Canvas.Left="0"
                Canvas.Top = "0"/>
                </Canvas>





                share|improve this answer














                How to move a picture to where a cursor clicked on the canvas




                For your requirement, you could create DoubleAnimation for image. You could get your cursor clicked position with PointerPressed event then pass the position X,Y value to DoubleAnimation ToProperty. I created the sample you could refer.



                Code behind



                public MainPage()
                {
                this.InitializeComponent();
                RootCanvasLayout.PointerPressed += new PointerEventHandler(Pointer_Pressed);
                }

                private void Pointer_Pressed(object sender, PointerRoutedEventArgs e)
                {
                Windows.UI.Input.PointerPoint currentPoint = e.GetCurrentPoint(RootCanvasLayout);
                CreateAnimation(currentPoint);
                }
                void CreateAnimation(PointerPoint point)
                { var duration = new Duration(TimeSpan.FromMilliseconds(1000));
                DoubleAnimation doubleAnimationX = new DoubleAnimation();
                DoubleAnimation doubleAnimationY = new DoubleAnimation();
                doubleAnimationX.To = point.Position.X-Pic.ActualWidth/2;
                doubleAnimationX.Duration = duration;
                doubleAnimationY.To = point.Position.Y-Pic.ActualHeight/2;
                doubleAnimationY.Duration = duration;
                var conStoryboard = new Storyboard();
                conStoryboard.Children.Add(doubleAnimationX);
                conStoryboard.Children.Add(doubleAnimationY);
                Storyboard.SetTarget(doubleAnimationX, Pic);
                Storyboard.SetTarget(doubleAnimationY, Pic);
                Storyboard.SetTargetProperty(doubleAnimationX, "(Canvas.Left)");
                Storyboard.SetTargetProperty(doubleAnimationY, "(Canvas.Top)");
                conStoryboard.Begin();
                }


                Xaml



                <Canvas Name="RootCanvasLayout" Background="Ivory">
                <Image Name="Pic" Width="100" Height="100"
                Source="Assets/hello.png"
                Canvas.Left="0"
                Canvas.Top = "0"/>
                </Canvas>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 2 at 3:26









                Nico Zhu - MSFTNico Zhu - MSFT

                10.1k1522




                10.1k1522
































                    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%2f53996043%2fhow-to-move-a-picture-to-where-a-cursor-clicked-on-the-canvas%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

                    Npm cannot find a required file even through it is in the searched directory