Repository navigation
Expand file tree
/
Copy patheditor-patterns.js
More file actions
184 lines (148 loc) · 6.48 KB
/
Copy patheditor-patterns.js
File metadata and controls
184 lines (148 loc) · 6.48 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* VectorCutter - SVG Editor Repeat & Pattern Tools
* Module: editor-patterns.js
* Description: Linear Repeat, Rotational Repeat, Mirror Repeat & Grid/Tile Repeat
*/
Object.assign(SVGEditor.prototype, {
// --- 1. LINEAR REPEAT ---
linearRepeat(count = 4, dxMm = 15, dyMm = 0) {
if (!this.selectedElements || this.selectedElements.length === 0) {
const countStr = prompt('Linear Repeat - Number of copies:', '4');
if (!countStr) return;
count = parseInt(countStr, 10);
const dxStr = prompt('Distance X between copies (mm):', '15');
dxMm = parseFloat(dxStr) || 0;
const dyStr = prompt('Distance Y between copies (mm):', '0');
dyMm = parseFloat(dyStr) || 0;
}
if (isNaN(count) || count < 2) return;
const factor = this.unitFactors[this.currentUnits] || 3.7795;
const dx = dxMm * factor;
const dy = dyMm * factor;
const originalElements = [...this.selectedElements];
const allNewElements = [];
for (let i = 1; i < count; i++) {
originalElements.forEach(el => {
const clone = el.cloneNode(true);
clone.style.outline = 'none';
if (clone.id) clone.id = `${clone.id}_lin_${i}`;
el.parentNode.appendChild(clone);
this.moveElement(clone, dx * i, dy * i);
allNewElements.push(clone);
});
}
this.clearSelection();
this.selectedElements = [...originalElements, ...allNewElements];
this.selectedElements.forEach(el => el.style.outline = '2px dashed #007acc');
this.selectedElement = this.selectedElements[0];
this.updateSelectionHandles();
this.updateContextMenu();
this.saveState();
},
// --- 2. ROTATIONAL (CIRCULAR) REPEAT ---
rotationalRepeat(count = 8) {
if (!this.selectedElements || this.selectedElements.length === 0) {
alert('Select elements to repeat in a circle.');
return;
}
const countStr = prompt('Rotational Repeat - Number of radial copies around center:', '8');
if (!countStr) return;
count = parseInt(countStr, 10);
if (isNaN(count) || count < 2) return;
const bbox = this.getGroupBoundingBox();
if (!bbox) return;
const cx = bbox.x + bbox.width / 2;
const cy = bbox.y + bbox.height / 2;
const angleStep = 360 / count;
const originalElements = [...this.selectedElements];
const allNewElements = [];
for (let i = 1; i < count; i++) {
const angle = angleStep * i;
originalElements.forEach(el => {
const clone = el.cloneNode(true);
clone.style.outline = 'none';
if (clone.id) clone.id = `${clone.id}_rot_${i}`;
el.parentNode.appendChild(clone);
// Rotate clone around group center
const t = clone.getAttribute('transform') || '';
clone.setAttribute('transform', `rotate(${angle.toFixed(2)}, ${cx.toFixed(2)}, ${cy.toFixed(2)}) ${t}`);
allNewElements.push(clone);
});
}
this.clearSelection();
this.selectedElements = [...originalElements, ...allNewElements];
this.selectedElements.forEach(el => el.style.outline = '2px dashed #007acc');
this.selectedElement = this.selectedElements[0];
this.updateSelectionHandles();
this.updateContextMenu();
this.saveState();
},
// --- 3. GRID / TILE REPEAT ---
gridRepeat(rows = 3, cols = 3, gapMm = 5) {
if (!this.selectedElements || this.selectedElements.length === 0) {
alert('Select elements to repeat in a grid matrix.');
return;
}
const colsStr = prompt('Columns (X):', '3');
if (!colsStr) return;
cols = parseInt(colsStr, 10);
const rowsStr = prompt('Rows (Y):', '3');
if (!rowsStr) return;
rows = parseInt(rowsStr, 10);
const gapStr = prompt('Gap between items (mm):', '5');
gapMm = parseFloat(gapStr) || 5;
if (cols < 1 || rows < 1 || (cols === 1 && rows === 1)) return;
const factor = this.unitFactors[this.currentUnits] || 3.7795;
const gap = gapMm * factor;
const bbox = this.getGroupBoundingBox();
if (!bbox) return;
const stepX = bbox.width + gap;
const stepY = bbox.height + gap;
const originalElements = [...this.selectedElements];
const allNewElements = [];
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (r === 0 && c === 0) continue; // Skip original
originalElements.forEach(el => {
const clone = el.cloneNode(true);
clone.style.outline = 'none';
if (clone.id) clone.id = `${clone.id}_grid_${r}_${c}`;
el.parentNode.appendChild(clone);
this.moveElement(clone, stepX * c, stepY * r);
allNewElements.push(clone);
});
}
}
this.clearSelection();
this.selectedElements = [...originalElements, ...allNewElements];
this.selectedElements.forEach(el => el.style.outline = '2px dashed #007acc');
this.selectedElement = this.selectedElements[0];
this.updateSelectionHandles();
this.updateContextMenu();
this.saveState();
},
// --- 4. MIRROR REPEAT ---
mirrorRepeat(direction = 'horizontal') {
if (!this.selectedElements || this.selectedElements.length === 0) return;
const bbox = this.getGroupBoundingBox();
if (!bbox) return;
const isHoriz = direction === 'horizontal';
const axis = isHoriz ? (bbox.x + bbox.width) : (bbox.y + bbox.height);
const clones = [];
this.selectedElements.forEach(el => {
const clone = el.cloneNode(true);
clone.style.outline = 'none';
if (clone.id) clone.id = `${clone.id}_mirror`;
el.parentNode.appendChild(clone);
this.flipElement(clone, direction, axis);
clones.push(clone);
});
this.clearSelection();
this.selectedElements = clones;
this.selectedElement = clones[0];
clones.forEach(c => c.style.outline = '2px dashed #007acc');
this.updateSelectionHandles();
this.updateContextMenu();
this.saveState();
}
});