-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcode-plotter.js
49 lines (40 loc) · 1.19 KB
/
gcode-plotter.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
function plotter(options) {
/*
options.encoder
options.canvas
etc.
*/
options = options || {};
// internal state goes here
var ctx = options.canvas.getContext('2d');
ctx.fillStyle = '#000000';
ctx.fillRect(0, 0, options.pixelWidth, options.pixelHeight);
ctx.fillStyle = '#ffffff';
var xoffset = (options.width - options.d) / 2 * options.scale;
var yoffset = (options.height - options.l1 - options.l2) / 2 * options.scale;
var totalMS = 0;
var lastFrame = 0;
// return a sink-stream that plots points/vectors on a canvas
return function plot(read) {
read(null, function plotCallback(end, pos) {
if (end === true) {
options.encoder.addFrame(ctx);
options.encoder.finish();
return;
}
if (end) throw end;
if (pos) {
//console.log("plot", pos);
ctx.fillRect(pos.x*options.scale + xoffset, pos.y*options.scale + yoffset, 1, 1);
totalMS += pos.ms;
if (totalMS > Math.floor(lastFrame/1000)*1000 + 1000) {
lastFrame = totalMS;
options.encoder.addFrame(ctx);
console.log(totalMS);
}
}
read(null, plotCallback);
});
}
}
module.exports = plotter;