Skip to content

Instantly share code, notes, and snippets.

@joehonton
Created February 21, 2021 03:43
Show Gist options
  • Save joehonton/581c4a13ac2cf32b23f421fc25a28739 to your computer and use it in GitHub Desktop.
Save joehonton/581c4a13ac2cf32b23f421fc25a28739 to your computer and use it in GitHub Desktop.
calculateLatest(event) {
this.latest.t = Date.now();
this.latest.x = event.offsetX;
this.latest.y = event.offsetY;
// time difference
this.deltaT = this.latest.t - this.initial.t;
// distance traveled
this.deltaX = Math.abs(this.latest.x - this.initial.x);
this.deltaY = Math.abs(this.latest.y - this.initial.y);
this.deltaXY = Math.hypot(this.deltaX, this.deltaY);
// direction of movement
this.directionX = (this.latest.x - this.initial.x >= 0) ?
'right' : 'left';
this.directionY = (this.latest.y - this.initial.y >= 0) ?
'down' : 'up';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment