Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. moveTo(x, y) {
  2. const startX = this.coords.x,
  3. startY = this.coords.y,
  4. startTime = performance.now(),
  5. duration = Math.hypot(x-startX, y-startY)*3;
  6. let delta = 0;
  7.  
  8. let self = this;
  9. const animate = function () {
  10. self.animating = true;
  11. const elapsed = performance.now() - startTime;
  12. delta = elapsed / duration;
  13. self.coords.x = startX + (x - startX) * delta;
  14. self.coords.y = startY + (y - startY) * delta;
  15. if(delta < 1 && !self.stop) return requestAnimationFrame(animate);
  16. self.animating = false;
  17. self.stop = false;
  18. };
  19. if(this.animating) this.stop = true;
  20. requestAnimationFrame(animate);
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement