Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. var myX = 100, myY = 100, speed = 3;
  2. var dist, dx, dy;
  3. function d(x1, x2, y1, y2){
  4. return Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))
  5. }
  6. function update() {
  7. dist = d(myX, mouseX, myY, mouseY);
  8. console.log(dist)
  9. dx = (mouseX-myX)/dist*speed;
  10. dy = (mouseY-myY)/dist*speed;
  11. myX += dx;
  12. myY += dy;
  13. }
  14.  
  15. function draw() {
  16. // This is how you draw a rectangle
  17. context.fillRect(myX, myY, 30, 30);
  18. };
  19.  
  20. function keyup(key) {
  21. // Show the pressed keycode in the console
  22. console.log("Pressed", key);
  23. };
  24.  
  25. function mouseup() {
  26. // Show coordinates of mouse on click
  27. console.log("Mouse clicked at", mouseX, mouseY);
  28. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement