Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. // Creating variables
  2. var circlesX=[100,300,500,600];
  3. var circlesY=[100,500,150,350];
  4. var angle=0;
  5. var state=0;
  6. var smallCircleX,deltaX;
  7. var smallCircleY,deltaY;
  8.  
  9. function areCirclesColliding(circleOneX,circleOneY,circleTwoX,circleTwoY,radiuses){
  10. return Math.sqrt((circleOneX-circleTwoX)*(circleOneX-circleTwoX)+(circleOneY-circleTwoY)*(circleOneY-circleTwoY))<radiuses;
  11. }
  12.  
  13. function update() {
  14. if(state!=-1){
  15. angle+=0.01;
  16. if(angle>=360){
  17. angle-=360;
  18. }
  19. }else{
  20. smallCircleX+=deltaX;
  21. smallCircleY+=deltaY;
  22. for(var i=0;i<4;i++){
  23. if(areCirclesColliding(smallCircleX,smallCircleY,circlesX[i],circlesY[i],70)){
  24. state=i;
  25. angle=Math.atan2(smallCircleY-circlesY[i],smallCircleX-circlesX[i]);
  26. }
  27. }
  28. }
  29. }
  30.  
  31. function draw() {
  32. // This is how you draw a rectangle
  33. for(var i=0;i<4;i++){
  34. context.beginPath();
  35. context.arc(circlesX[i],circlesY[i],50,0,2*Math.PI);
  36. context.fillStyle="blue";
  37. context.strokeStyle="black";
  38. context.stroke();
  39. context.fill();
  40. context.closePath();
  41. }
  42. if(state!=-1){
  43. smallCircleX=circlesX[state]+Math.cos(angle)*50;
  44. smallCircleY=circlesY[state]+Math.sin(angle)*50;
  45. }
  46. context.beginPath();
  47. context.arc(smallCircleX,smallCircleY,20,0,2*Math.PI);
  48. context.fillStyle="green";
  49. context.strokeStyle="black";
  50. context.stroke();
  51. context.fill();
  52. context.closePath();
  53. }
  54.  
  55. function keyup(key) {
  56. // Show the pressed keycode in the console
  57. console.log("Pressed", key);
  58. // Show coordinates of mouse on click
  59. smallCircleX=circlesX[state]+Math.cos(angle)*71;
  60. smallCircleY=circlesY[state]+Math.sin(angle)*71;
  61. deltaX=Math.cos(angle)*2;
  62. deltaY=Math.sin(angle)*2;
  63. state=-1;
  64. }
  65. function mouseup() {
  66. // Show coordinates of mouse on click
  67. smallCircleX=circlesX[state]+Math.cos(angle)*71;
  68. smallCircleY=circlesY[state]+Math.sin(angle)*71;
  69. deltaX=Math.cos(angle)*2;
  70. deltaY=Math.sin(angle)*2;
  71. state=-1;
  72. console.log("Mouse clicked at", mouseX, mouseY);
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement