Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. // Creating variables
  2. var myX = 0, myY = 0;
  3. var speed = 3;
  4. var l1 = 50;
  5.  
  6. function update() {
  7. if (isKeyPressed[key_up]){myY -= speed;}
  8. if (isKeyPressed[key_down]){myY += speed;}
  9. if (isKeyPressed[key_left]){myX -= speed;}
  10. if (isKeyPressed[key_right]){myX += speed;}
  11. }
  12.  
  13. function draw() {
  14. // This is how you draw a rectangle
  15. context.fillRect(myX, myY, 30, 30);
  16.  
  17. context.beginPath();
  18. context.moveTo(myX, myY);
  19. var x2 = mouseX - myX;
  20. var y2 = mouseY - myY;
  21. var l2 = Math.sqrt(x2*x2 + y2*y2);
  22. var x1 = x2*l1/l2;
  23. var y1 = y2*l1/l2;
  24. context.lineTo(myX + x1, myY + y1);
  25. context.stroke();
  26. }
  27.  
  28. function keyup(key) {
  29. // Show the pressed keycode in the console
  30. console.log("Pressed", key);
  31. }
  32. function mouseup() {
  33. // Show coordinates of mouse on click
  34. console.log("Mouse clicked at", mouseX, mouseY);
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement