-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSVGEditor.ts
286 lines (232 loc) · 9.7 KB
/
SVGEditor.ts
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
const svgns = "http://www.w3.org/2000/svg";
interface MenuSettings {
shape: string;
color: string;
fill: string;
size: number;
}
class Menu {
private shapes: Array<HTMLButtonElement>;
private colors: Array<HTMLButtonElement>;
private fills: Array<HTMLButtonElement>;
private sizes: Array<HTMLButtonElement>;
constructor(private settings: MenuSettings) {
}
setShapes(shapes: Array<string>) {
this.shapes = shapes.map(str => {
const button = document.createElement("button");
button.addEventListener("click", (event) => this.onClick("shape", str));
button.innerText = str;
button.value = str;
return button;
});
}
setColors(colors: Array<string>) {
this.colors = colors.map(str => {
const button = document.createElement("button");
button.addEventListener("click", (event) => this.onClick("color", str));
button.style.setProperty("border-color", str);
button.innerText = str;
button.value = str;
return button;
});
}
setFills(colors: Array<string>) {
this.fills = colors.map(str => {
const button = document.createElement("button");
button.addEventListener("click", (event) => this.onClick("fill", str));
button.style.setProperty("background-color", str);
button.innerText = str;
button.value = str;
return button;
});
}
setSizes(sizes: Array<number>) {
this.sizes = sizes.map(number => {
const button = document.createElement("button");
button.addEventListener("click", (event) => this.onClick("size", number));
button.innerText = String(number);
button.value = String(number);
return button;
});
}
getElement(): HTMLDivElement {
this.updateButtons();
const menu = document.createElement("div");
menu.classList.add("menu")
Object.keys(this.settings).forEach((setting) => {
const section = document.createElement("div");
this[setting + "s"].forEach((button: HTMLButtonElement) => section.appendChild(button));
menu.appendChild(section);
});
return menu;
}
private onClick(setting: string, value: string | number) {
this.settings[setting] = value;
this.updateButtons();
}
private updateButtons() {
Object.keys(this.settings).forEach((setting) => {
this[setting + "s"].forEach((button: HTMLButtonElement) => {
if (button.value == this.settings[setting])
button.classList.add("selected");
else
button.classList.remove("selected");
});
});
}
}
interface MousePosition {
x: number;
y: number;
}
interface Dimension {
height: string;
width: string;
}
interface EditorConfig {
element: HTMLElement;
dimension: Dimension;
}
class SVGEditor {
private svg: SVGSVGElement;
private objects: Array<SVGElement> = [];
private codeSection: HTMLPreElement;
private originalMousePosition: MousePosition = null;
private lastMousePosition: MousePosition = null;
private currentMousePosition: MousePosition = null;
private createdObject: SVGElement = null;
private focusedObject: SVGElement = null;
private menuSettings: MenuSettings;
constructor(editorConfig: EditorConfig) {
this.svg = SVGEditor.createSVG(editorConfig.dimension);
this.attachEvents(this.svg);
const menu = this.createMenu();
this.codeSection = document.createElement("pre");
const editor = document.createElement("div");
editor.classList.add("svg-editor");
editor.appendChild(menu);
editor.appendChild(this.svg);
editor.appendChild(this.codeSection);
editorConfig.element.appendChild(editor);
}
private static createSVG(dimension: Dimension): SVGSVGElement {
const svg = document.createElementNS(svgns, "svg");
svg.setAttribute("width", dimension.width);
svg.setAttribute("height", dimension.height);
return svg;
}
private attachEvents(svg: SVGSVGElement) {
svg.addEventListener("mousedown", this.onMouseDown.bind(this));
svg.addEventListener("mousemove", this.onMouseMove.bind(this));
svg.addEventListener("mouseup", this.onMouseUp.bind(this));
}
private createMenu(): HTMLElement {
this.menuSettings = {
shape: "path",
color: "black",
fill: "none",
size: 3
};
const menu = new Menu(this.menuSettings);
menu.setColors(["black", "red", "blue", "yellow"]);
menu.setFills(["none", "black", "red", "blue", "yellow"]);
menu.setSizes([1, 2, 3, 4, 5]);
menu.setShapes(["rect", "line", "circle", "ellipse", "path"]);
return menu.getElement();
}
private updateCode() {
this.codeSection.innerText = this.svg.innerHTML.replace(new RegExp("><", "g"), ">\n<");
}
private onMouseDown(event) {
this.createdObject = document.createElementNS(svgns, this.menuSettings.shape);
this.createdObject.setAttributeNS(null, "fill", this.menuSettings.fill);
this.createdObject.setAttributeNS(null, "stroke", this.menuSettings.color);
this.createdObject.setAttributeNS(null, "stroke-width", String(this.menuSettings.size));
this.svg.appendChild(this.createdObject);
this.originalMousePosition = this.lastMousePosition = this.currentMousePosition = {
x: event.offsetX,
y: event.offsetY
};
switch (this.menuSettings.shape) {
case "path":
const start = "M " + this.currentMousePosition.x + " " + this.currentMousePosition.y;
this.createdObject.setAttributeNS(null, "d", start);
}
}
private onMouseMove(event) {
// Ignore if not in mouse event
if (this.originalMousePosition === null)
return;
this.lastMousePosition = this.currentMousePosition;
this.currentMousePosition = {x: event.offsetX, y: event.offsetY};
this.updateCurrentObject();
}
private onMouseUp() {
// If shape is nothing
if (this.originalMousePosition.x != this.currentMousePosition.x ||
this.originalMousePosition.y != this.currentMousePosition.y) {
this.objects.push(this.createdObject);
this.focus(this.createdObject);
} else if (this.menuSettings.shape !== "path") {
this.svg.removeChild(this.createdObject);
}
// TODO: If path, compress
this.originalMousePosition = this.lastMousePosition = this.currentMousePosition = null;
this.createdObject = null;
}
private focus(object: SVGElement) {
}
private updateCurrentObject() {
const topLeft: MousePosition = {
x: Math.min(this.currentMousePosition.x, this.originalMousePosition.x),
y: Math.min(this.currentMousePosition.y, this.originalMousePosition.y)
};
const bottomRight: MousePosition = {
x: Math.max(this.currentMousePosition.x, this.originalMousePosition.x),
y: Math.max(this.currentMousePosition.y, this.originalMousePosition.y)
};
switch (this.menuSettings.shape) {
case "line":
this.createdObject.setAttributeNS(null, "x1", String(this.originalMousePosition.x));
this.createdObject.setAttributeNS(null, "y1", String(this.originalMousePosition.y));
this.createdObject.setAttributeNS(null, "x2", String(this.currentMousePosition.x));
this.createdObject.setAttributeNS(null, "y2", String(this.currentMousePosition.y));
break;
case "rect":
this.createdObject.setAttributeNS(null, "x", String(topLeft.x));
this.createdObject.setAttributeNS(null, "y", String(topLeft.y));
this.createdObject.setAttributeNS(null, "width", String(bottomRight.x - topLeft.x));
this.createdObject.setAttributeNS(null, "height", String(bottomRight.y - topLeft.y));
break;
case "circle":
this.createdObject.setAttributeNS(null, "cx", String((topLeft.x + bottomRight.x) / 2));
this.createdObject.setAttributeNS(null, "cy", String((topLeft.y + bottomRight.y) / 2));
const radius = Math.max(bottomRight.x - topLeft.x, bottomRight.y - topLeft.y) / 2;
this.createdObject.setAttributeNS(null, "r", String(radius));
break;
case "ellipse":
this.createdObject.setAttributeNS(null, "cx", String((topLeft.x + bottomRight.x) / 2));
this.createdObject.setAttributeNS(null, "cy", String((topLeft.y + bottomRight.y) / 2));
this.createdObject.setAttributeNS(null, "rx", String((bottomRight.x - topLeft.x) / 2));
this.createdObject.setAttributeNS(null, "ry", String((bottomRight.y - topLeft.y) / 2));
break;
case "path":
let d = this.createdObject.getAttribute("d") + " l ";
d += this.currentMousePosition.x - this.lastMousePosition.x;
d += " ";
d += this.currentMousePosition.y - this.lastMousePosition.y;
this.createdObject.setAttributeNS(null, "d", d);
}
this.updateCode();
}
}
document.addEventListener("DOMContentLoaded", () => {
new SVGEditor({
element: document.body,
dimension: {
width: "100%",
height: "500px"
}
});
}, false);