How to move a picture to where a cursor clicked on the canvas
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
add a comment |
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
add a comment |
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
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
c# visual-studio uwp
asked Jan 1 at 13:56
KarolineKaroline
505
505
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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>
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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>
add a comment |
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>
add a comment |
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>
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>
answered Jan 2 at 3:26


Nico Zhu - MSFTNico Zhu - MSFT
10.1k1522
10.1k1522
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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