UNPKG

776 BPlain TextView Raw
1// Interface for point data structure used e.g. in SignaturePad#fromData method
2export interface BasicPoint {
3 x: number;
4 y: number;
5 time: number;
6}
7
8export class Point implements BasicPoint {
9 public time: number;
10
11 constructor(public x: number, public y: number, time?: number) {
12 this.time = time || Date.now();
13 }
14
15 public distanceTo(start: BasicPoint): number {
16 return Math.sqrt(
17 Math.pow(this.x - start.x, 2) + Math.pow(this.y - start.y, 2),
18 );
19 }
20
21 public equals(other: BasicPoint): boolean {
22 return this.x === other.x && this.y === other.y && this.time === other.time;
23 }
24
25 public velocityFrom(start: BasicPoint): number {
26 return this.time !== start.time
27 ? this.distanceTo(start) / (this.time - start.time)
28 : 0;
29 }
30}