Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

62 строки
1.1 KiB

  1. <style>
  2. canvas {
  3. border: 1px solid #eee;
  4. }
  5. </style>
  6. <script src="../build/build.js"></script>
  7. <canvas width=500 height=400></canvas>
  8. <script>
  9. var Tween = require('tween');
  10. var raf = require('component-raf');
  11. var canvas = document.querySelector('canvas');
  12. var ctx = canvas.getContext('2d');
  13. var w = canvas.width, h = canvas.height;
  14. function Ball(x, y) {
  15. this.moveTo(x, y);
  16. this.rad = 10;
  17. }
  18. Ball.prototype.moveTo = function(x, y){
  19. this.x = x;
  20. this.y = y;
  21. };
  22. Ball.prototype.draw = function(ctx){
  23. ctx.save();
  24. ctx.fillStyle = 'red';
  25. ctx.arc(this.x | 0, this.y | 0, this.rad, 0, Math.PI * 2, false);
  26. ctx.fill();
  27. ctx.restore();
  28. };
  29. var ball = new Ball(100, 100);
  30. ball.draw(ctx);
  31. canvas.onclick = function(e){
  32. console.log('move');
  33. var tween = Tween({ x: ball.x, y: ball.y })
  34. .ease('out-bounce')
  35. .to({ x: e.offsetX, y: e.offsetY })
  36. .duration(1500);
  37. tween.update(function(o){
  38. canvas.width = canvas.width;
  39. ball.moveTo(o.x, o.y);
  40. ball.draw(ctx);
  41. });
  42. function animate() {
  43. raf(animate);
  44. tween.update();
  45. }
  46. animate();
  47. };
  48. </script>