This repository has been archived by the owner on Apr 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
plot-coords.js
82 lines (64 loc) · 1.98 KB
/
plot-coords.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
77
78
79
80
81
82
import createAxidraw from './axidraw';
import ProgressBar from './progress-bar';
import {renderSVGPaths} from './svg-tools';
const BOT_SCALE = {
ratio: 12000 / 8720,
factor: 14.2,
offset: 20
};
export default class Plotter {
constructor(coords = []) {
this.svgContainer = document.getElementById('preview');
this.progressBar = new ProgressBar(document.body);
this.coords = coords;
this.shouldAbortPrinting = false;
}
set coords(coords) {
this._coords = coords;
this.svgContainer.innerHTML = '';
this.svgPaths = renderSVGPaths(coords, {renderAs: 'nodes'});
this.svgPaths.forEach(path => {
this.svgContainer.appendChild(path);
});
}
setAnimatedCoords(coords) {
this._coords = coords;
this.svgPaths = renderSVGPaths(coords, {renderAs: 'nodes'});
this.svgPaths.forEach((path, index) => {
const oldPath = this.svgContainer.querySelector(`path:nth-child(${index + 1})`);
if (!oldPath) {
this.svgContainer.appendChild(path);
return;
}
oldPath.setAttribute('d', path.getAttribute('d'));
});
}
abort() {
const response = window.confirm('This will abort the printing!');
this.shouldAbortPrinting = response;
}
async print() {
if (!this.axidraw) {
this.axidraw = await createAxidraw();
}
this.svgPaths.forEach(path => {
path.setAttribute('class', 'pending');
});
for (let i = 0; i < this._coords.length; i++) {
if (this.shouldAbortPrinting) {
i = this._coords.length;
}
const line = this._coords[i];
const path = this.svgPaths[i];
path.setAttribute('class', 'current');
const relativeLine = line.map(p => [
p[0] / BOT_SCALE.factor + BOT_SCALE.offset,
p[1] / BOT_SCALE.factor * BOT_SCALE.ratio
]);
await this.axidraw.drawPath(relativeLine);
path.removeAttribute('class');
this.progressBar.progress = i / (this._coords.length - 1);
}
await this.axidraw.parkPen();
}
}