Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. function drawAngles () {
  2. var d = 50; //start line at (10, 20), move 50px away at angle of 30 degrees
  3. var angle = 80 * Math.PI/180;
  4. ctx.beginPath();
  5. ctx.moveTo(300,0);
  6. ctx.lineTo(300,600); //x, y
  7. ctx.moveTo(0,300);
  8. ctx.lineTo(600,300);
  9. ctx.moveTo(300,300);
  10. ctx.lineTo(600,100);
  11. ctx.arc(300,300,300,0,2*Math.PI);
  12. ctx.stroke();
  13. }
  14.  
  15. function getAngleX (x) {
  16. return x = x + (50 * Math.cos(Math.PI/6));
  17. }
  18. function getAngleY(y) {
  19. return y = y + (50 * Math.sin(Math.PI/6));
  20. }
  21.  
  22. //just animate this box to move at an angle from center down at 30 degrees
  23. $(".anotherBox").mouseenter(function(e) {
  24. pos = $(this).position();
  25. box2X = pos.left;
  26. box2Y = pos.top;
  27.  
  28. $(this).animate({
  29. //top : $(window).outerHeight(),
  30. top : getAngleY(box2Y)+"px",
  31. left: getAngleX(box2X)+"px",
  32. }, "slow");
  33. });
  34.  
  35. function getSteps(angle) {
  36.  
  37. var cos = Math.cos(angle),
  38. sin = Math.sin(angle);
  39.  
  40. return {
  41. x: cos -sin,
  42. y: sin + cos
  43. }
  44. }
  45.  
  46. function loop() {
  47.  
  48. var x = i * step.x, // scale using i
  49. y = i * step.y;
  50.  
  51. ctx.fillRect(200 + x, 200 + y, 2, 2); // add to origin start point 200, 200
  52.  
  53. i += 1; // increase i
  54.  
  55. if (i < length) requestAnimationFrame(loop);
  56. }
  57.  
  58. function lineAtAngle(x1, y1, length, angle) {
  59. ctx.moveTo(x1, y1);
  60. ctx.lineTo(x1 + length * Math.cos(angle), y1 + length * Math.sin(angle));
  61. }
  62.  
  63. var ctx = cv.getContext('2d');
  64.  
  65. var ball = {
  66. x:200, y:200,
  67. r : 30,
  68. vx : 0.4, vy:0.4
  69. }
  70.  
  71. // when mouse moved that distance, ball speed norm will be 1
  72. var speedNorm = 10;
  73.  
  74.  
  75. function collide() {
  76. var dist = sq(ball.x - mx) + sq (ball.y-my);
  77. // too far from ball ?
  78. if (dist > sq(ball.r)) {
  79. collisionOnGoing = false;
  80. return;
  81. }
  82. // return if collision allready handled
  83. if (collisionOnGoing) return;
  84. var mouseDist =Math.sqrt( sq(mx-lastmx) + sq(my-lastmy) );
  85. // no collision if mouse too slow
  86. if (mouseDist<speedNorm/5) return;
  87. // launch the ball in current direction
  88. // with a speed relative to the mouse speed.
  89. var mouseAngle = Math.atan2(my-lastmy, mx-lastmx);
  90. ball.vx= (mouseDist / speedNorm ) * Math.cos(mouseAngle);
  91. ball.vy= (mouseDist / speedNorm ) * Math.sin(mouseAngle);
  92. collisionOnGoing = true;
  93. }
  94. var collisionOnGoing = false;
  95.  
  96. function animate() {
  97. requestAnimationFrame(animate);
  98. ctx.clearRect(0,0,400,400);
  99. // collide ball with mouse
  100. collide();
  101. // draw ball
  102. ctx.beginPath();
  103. ctx.arc(ball.x, ball.y, ball.r, 0, 6.3);
  104. ctx.fill();
  105. ctx.closePath();
  106. // move
  107. ball.x+=ball.vx;
  108. ball.y+=ball.vy;
  109. // collide with screen
  110. if (ball.x>400) ball.vx=-Math.abs(ball.vx);
  111. if (ball.x<0) ball.vx=Math.abs(ball.vx);
  112. if (ball.y>400) ball.vy=-Math.abs(ball.vy);
  113. if (ball.y<0) ball.vy=Math.abs(ball.vy);
  114. }
  115.  
  116. animate();
  117.  
  118. // --- Mouse handling ---
  119.  
  120. addEventListener('mousemove', mouseMove);
  121.  
  122. var mx=-1, my=-1, lastmx=-1, lastmy=-1;
  123. var cvRect = cv.getBoundingClientRect();
  124. var cvLeft = cvRect.left;
  125. var cvTop = cvRect.top;
  126. function mouseMove(e) {
  127. lastmx = mx; lastmy=my;
  128. mx=e.clientX - cvLeft;
  129. my=e.clientY - cvTop;
  130. }
  131.  
  132. function sq(x) { return x*x; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement