Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var myX = 100, myY = 100, speed = 3;
- var dist, dx, dy;
- function d(x1, x2, y1, y2){
- return Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))
- }
- function update() {
- dist = d(myX, mouseX, myY, mouseY);
- console.log(dist)
- dx = (mouseX-myX)/dist*speed;
- dy = (mouseY-myY)/dist*speed;
- myX += dx;
- myY += dy;
- }
- function draw() {
- // This is how you draw a rectangle
- context.fillRect(myX, myY, 30, 30);
- };
- function keyup(key) {
- // Show the pressed keycode in the console
- console.log("Pressed", key);
- };
- function mouseup() {
- // Show coordinates of mouse on click
- console.log("Mouse clicked at", mouseX, mouseY);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement