forked from TomasHubelbauer/svg-3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmount.js
76 lines (68 loc) · 2.25 KB
/
mount.js
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
export default function mount(canvasSvg, shapes) {
// Mount new mesh
if (canvasSvg.children.length === 0) {
const fragment = document.createDocumentFragment();
for (const shape of shapes) {
switch (shape.type) {
case 'edge': {
const line = document.createElementNS('http://www.w3.org/2000/svg', 'line');
line.setAttribute('x1', shape.from[0]);
line.setAttribute('y1', shape.from[1]);
line.setAttribute('x2', shape.to[0]);
line.setAttribute('y2', shape.to[1]);
line.setAttribute('stroke', 'black');
line.setAttribute('fill', 'none');
fragment.append(line);
break;
}
case 'face': {
const polyline = document.createElementNS('http://www.w3.org/2000/svg', 'polyline');
polyline.setAttribute('points', shape.points);
polyline.setAttribute('stroke', 'black');
polyline.setAttribute('fill', 'none');
fragment.append(polyline);
break;
}
default: {
throw new Error(`Invalid shape type ${shape.type}.`);
}
}
}
canvasSvg.append(fragment);
return;
}
// Reconcile the DOM changes
let index = 0;
for (const shape of shapes) {
const element = canvasSvg.children[index];
if (!element) {
throw new Error('The DOM has changed unexpectedly.');
}
switch (shape.type) {
case 'edge': {
if (element.tagName !== 'line') {
throw new Error('The DOM has changed unexpectedly.');
}
element.setAttribute('x1', shape.from[0]);
element.setAttribute('y1', shape.from[1]);
element.setAttribute('x2', shape.to[0]);
element.setAttribute('y2', shape.to[1]);
break;
}
case 'face': {
if (element.tagName !== 'polyline') {
throw new Error('The DOM has changed unexpectedly.');
}
element.setAttribute('points', shape.points);
break;
}
default: {
throw new Error(`Invalid shape type ${shape.type}.`);
}
}
index++;
}
if (canvasSvg.children.length !== index) {
throw new Error('The DOM has changed unexpectedly.');
}
}