javascript - Have a dot follow a path selected path -


What I'm trying to do, is to click the canvas position to choose.

This is my code so far.

  var canvas = document.getElementById ("canvas"); Var ctx = canvas.getContext ("2D"); var rect = canvas.getBoundingClientRect () // var clicked chords = {x: 0, y: 0}; Var player = {x: 100, y: 100}; Canvas.addEventListener ("Mousewood", received fetal, false); Function getChords (e) {clickedChords.x = e.pageX - rect.left; Clicked Chords.y = e.pageY - rect.top; } Function draw () {ctx.clearRect (0,0, canvas.Wide, canvas.height); Ctx.fillStyle = 'black'; Ctx.beginPath (); Ctx.arc (clickedChords.x, clickedChords.y, 3,0,360); ctx.arc (player.x, player.y, 3,0,360); Ctx.fill (); If (player.x> Clicked Chords.x & player.x! == Clicked Chords.x) {player.x - = 1; } If (player.x   

The work I did right now was to get the dot to follow the angle of 45 degrees, then follow the X or Y value of the location I clicked. I tried to use the formula but I do not know how to implement it in my code.

Thank you for your help, because I'm really stuck.

You can use interpolation for:

  var t = 0, // will be in the range [0.0, 1.0] Old PlayerX, // for these launches; OldPlayerY; Function getChords (e) {clickedChords.x = e.pageX - rect.left; Clicked Chords.y = e.pageY - rect.top; OldPlayerX = player.x, // Make a copy as per its requirement PlayYY = player.y; Attract (); } Function draw () {ctx.clearRect (0,0, canvas.Wide, canvas.height); Ctx.fillStyle = 'black'; Ctx.beginPath (); Ctx.arc (clickedChords.x, clickedChords.y, 3,0,360); ctx.arc (player.x, player.y, 3,0,360); Ctx.fill (); If (T & LT; = 1) {player.x = Old Player X + (Clicks Chords.x - Old Player X) * T; Player.y = Old Player Y + (Clicked Y. - Old Player Y) * T; T + = 0.05; // Speed ​​determines the request animationframe (draw); Use this instead of // setInterval}}   

launches on these lines:

  player.x = oldPhilax + (clicked carded. X - Older Player X) * T; Player.y = Old Player Y + (Clicked Y. - Old Player Y) * T;   

This basically makes a difference between the new and the old location. t will determine how much difference is added to the original condition, that means if T = 0 is nothing then, if T = 1 then the full difference in the old condition is added, which is clearly new Position is equal to Anything in the middle (for t ) will add a percentage of the point that will allow you to enliven the point.

requestAnimationFrame lets you sync the animation to refresh.

Hope it helps!

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -