-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshape.js
More file actions
80 lines (67 loc) · 2.11 KB
/
shape.js
File metadata and controls
80 lines (67 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const Shape = (base) => {
let translation = { x: 0, y: 0 };
let rotation = 0;
let scale = 1;
let cosRot = Math.cos(rotation);
let sinRot = Math.sin(rotation);
const transform = ({ x: X, y: Y }) => {
let x = X, y = Y;
x *= scale;
y *= scale;
const t = x;
x = x * cosRot - y * sinRot;
y = t * sinRot + y * cosRot;
x += translation.x;
y += translation.y;
return { x, y };
};
const verts = function* (base) {
for (const vert of base) {
yield transform(vert);
}
};
let hasTransformed = true;
let min, max;
const getBoundingBox = (base) => {
if (!hasTransformed) return { min, max };
let newMin = { x: Infinity, y: Infinity };
let newMax = { x: -Infinity, y: -Infinity };
for (const vert of verts(base)) {
newMin.x = Math.min(newMin.x, vert.x);
newMin.y = Math.min(newMin.y, vert.y);
newMax.x = Math.max(newMax.x, vert.x);
newMax.y = Math.max(newMax.y, vert.y);
}
[min, max] = [newMin, newMax];
hasTransformed = false;
return { min, max };
}; getBoundingBox(base);
return {
base,
get verts() { return verts(this.base) },
get boundingBox() { return getBoundingBox(this.base) },
get translation() { return translation },
set translation(value) {
if (value.x !== translation.x || value.y !== translation.y) {
translation = value;
hasTransformed = true;
}
},
get rotation() { return rotation },
set rotation(value) {
if (value !== rotation) {
rotation = value;
cosRot = Math.cos(rotation);
sinRot = Math.sin(rotation);
hasTransformed = true;
}
},
get scale() { return scale },
set scale(value) {
if (value !== scale) {
scale = value;
hasTransformed = true;
}
},
};
};