UNPKG

1.1 kBPlain 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 pressure: number;
6 time: number;
7}
8
9export class Point implements BasicPoint {
10 public x: number;
11 public y: number;
12 public pressure: number;
13 public time: number;
14
15 constructor(x: number, y: number, pressure?: number, time?: number) {
16 if (isNaN(x) || isNaN(y)) {
17 throw new Error(`Point is invalid: (${x}, ${y})`);
18 }
19 this.x = +x;
20 this.y = +y;
21 this.pressure = pressure || 0;
22 this.time = time || Date.now();
23 }
24
25 public distanceTo(start: BasicPoint): number {
26 return Math.sqrt(
27 Math.pow(this.x - start.x, 2) + Math.pow(this.y - start.y, 2),
28 );
29 }
30
31 public equals(other: BasicPoint): boolean {
32 return (
33 this.x === other.x &&
34 this.y === other.y &&
35 this.pressure === other.pressure &&
36 this.time === other.time
37 );
38 }
39
40 public velocityFrom(start: BasicPoint): number {
41 return this.time !== start.time
42 ? this.distanceTo(start) / (this.time - start.time)
43 : 0;
44 }
45}