-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheffects.js
More file actions
91 lines (83 loc) · 2.55 KB
/
Copy patheffects.js
File metadata and controls
91 lines (83 loc) · 2.55 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
81
82
83
84
85
86
87
88
89
90
91
// effects.js
import { shapes, Shape, createCircle, createSquare, createTriangle, createPolygon, createLine } from './shapes.js';
// Keep track of anchored shapes
export const anchoredShapes = new Set();
// Anchor a shape (it won't move, but others can collide)
export function anchorShape(shape) {
if (!anchoredShapes.has(shape)) {
anchoredShapes.add(shape);
shape.vx = 0;
shape.vy = 0;
shape.angularVelocity = 0;
}
}
// Unanchor a shape
export function unanchorShape(shape) {
if (anchoredShapes.has(shape)) {
anchoredShapes.delete(shape);
}
}
// Toggle anchor state
export function toggleAnchor(shape) {
if (anchoredShapes.has(shape)) {
unanchorShape(shape);
} else {
anchorShape(shape);
}
}
// Copy a shape (creates a new shape with same properties)
export function copyShape(shape) {
let newShape;
switch (shape.type) {
case "circle":
newShape = createCircle(shape.x + 20, shape.y + 20, shape.size, shape.color, shape.mass);
break;
case "square":
newShape = createSquare(shape.x + 20, shape.y + 20, shape.size, shape.color, shape.mass);
break;
case "triangle":
newShape = createTriangle(shape.x + 20, shape.y + 20, shape.size, shape.color, shape.mass);
break;
case "polygon":
newShape = createPolygon(shape.x + 20, shape.y + 20, shape.size, shape.color, shape.mass);
break;
case "line":
newShape = createLine(shape.x + 20, shape.y + 20, shape.size, shape.color, shape.mass);
break;
}
// Copy velocities and rotation
newShape.vx = shape.vx;
newShape.vy = shape.vy;
newShape.rotation = shape.rotation;
newShape.angularVelocity = shape.angularVelocity;
return newShape;
}
// Delete a shape from the simulation
export function deleteShape(shape) {
const index = shapes.indexOf(shape);
if (index !== -1) {
shapes.splice(index, 1);
anchoredShapes.delete(shape);
}
}
// Anchor all shapes under mouse click (optional helper)
export function anchorShapeAt(x, y) {
for (let shape of shapes) {
if (
x > shape.x &&
x < shape.x + shape.size &&
y > shape.y &&
y < shape.y + shape.size
) {
toggleAnchor(shape);
}
}
}
// Copy all anchored shapes
export function copyAnchoredShapes() {
const copies = [];
for (let shape of anchoredShapes) {
copies.push(copyShape(shape));
}
return copies;
}