Created
November 15, 2011 15:30
-
-
Save rlemon/1367346 to your computer and use it in GitHub Desktop.
Canvas move object to mouse click. With easing.. :P
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.onload = function() { | |
var cvs = document.getElementById("cvs"); | |
var ctx = cvs.getContext("2d"); | |
var h, w, speed = 10; | |
this.onresize = function() { | |
h = window.innerHeight, w = window.innerWidth; | |
cvs.setAttribute("height", h); | |
cvs.setAttribute("width", w); | |
}; | |
this.onresize.call(); | |
var cx = w / 2, | |
cy = h / 2; | |
var nx = cx, | |
ny = cy; | |
this.onmouseup = function(event) { | |
nx = event.clientX, ny = event.clientY; | |
}; | |
var draw = function() { | |
ctx.save(); | |
ctx.fillStyle = "#000"; | |
ctx.fillRect(0, 0, w, h); | |
cx += (nx - cx) / speed; | |
cy += (ny - cy) / speed; | |
ctx.beginPath(); | |
ctx.fillStyle = "#fff"; | |
ctx.arc(cx, cy, 10, 0, Math.PI * 2); | |
ctx.fill(); | |
ctx.closePath(); | |
ctx.restore(); | |
setTimeout(draw, 1000 / 32); | |
}; | |
draw(); | |
}; |
Which part is the easing?
cx += (nx - cx) / speed;
cy += (ny - cy) / speed;
this part
Where is moving object code?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Which part is the easing?