-
Notifications
You must be signed in to change notification settings - Fork 1
/
gcode2dviewer.js
130 lines (119 loc) · 4.88 KB
/
gcode2dviewer.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
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
/*jslint todo: true, browser: true, continue: true, white: true*/
/*global THREE, GCodeToGeometry*/
/**
* Written by Alex Canales for ShopBotTools, Inc.
*/
var GCode2DViewer = {};
/**
* Previews in a canvas the G-Code.
* @param {string} gcodeStr The G-Code.
* @param {object} colors The colors defined by { G0, G1, G2G3 }, each field is
* a string of an hexadecimal color (ex: '#ff00ff). If one field is
* undefined, the corresponding G-Code command is not display.
* @param {object} canvas The DOM Element canvas.
*/
GCode2DViewer.preview = function(gcodeStr, colors, canvas) {
"use strict";
/**
* Calculates the ratio for the scale.
* @param {object} gcode The parsed G-Code.
* @param {object} canvas The DOM Element canvas.
* @return {number} The scale ratio.
*/
function calculateRatio(gcode, canvas) {
var pW = Math.abs(gcode.size.max.x - gcode.size.min.x);
var pH = Math.abs(gcode.size.max.y - gcode.size.min.y);
var cW = parseInt(canvas.width, 10), cH = parseInt(canvas.height, 10);
return Math.min(cW / pW, cH / pH);
}
/**
* Draws a straight line.
* @param {object} ctx The canvas 2D context.
* @param {number} ratio The scale ratio.
* @param {object} start The lowest point of the G-Code command.
* @param {object} line The line defined by the G-Code command.
* @param {number} height The canvas height.
* @param {string} color The hexadecimal color in string.
*/
function drawStraightLine(ctx, ratio, start, line, height, color) {
var startX = Math.round(ratio * (line.start.x - start.x));
var startY = Math.round(height - ratio * (line.start.y - start.y));
var endX = Math.round(ratio * (line.end.x - start.x));
var endY = Math.round(height - ratio * (line.end.y - start.y));
ctx.beginPath();
ctx.moveTo(startX, startY);
ctx.lineTo(endX, endY);
ctx.strokeStyle = color;
ctx.lineWidth = 2;
ctx.stroke();
ctx.closePath();
}
/**
* Draws a curved line.
* @param {object} ctx The canvas 2D context.
* @param {number} ratio The scale ratio.
* @param {object} start The lowest point of the G-Code command.
* @param {object} line The line defined by the G-Code command.
* @param {number} height The canvas height.
* @param {string} color The hexadecimal color in string.
*/
function drawCurvedLine(ctx, ratio, start, line, height, color) {
var i = 0;
var b = line.beziers, l = {};
ctx.beginPath();
for(i = 0 ; i < b.length; i++) {
l = b[i];
ctx.moveTo(ratio * (l.p0.x - start.x), height - ratio * (l.p0.y - start.y));
ctx.bezierCurveTo(
ratio * (l.p1.x - start.x), height - ratio * (l.p1.y - start.y),
ratio * (l.p2.x - start.x), height - ratio * (l.p2.y - start.y),
ratio * (l.p3.x - start.x), height - ratio * (l.p3.y - start.y)
);
}
ctx.strokeStyle = color;
ctx.lineWidth = 2;
ctx.stroke();
ctx.closePath();
}
if(colors === undefined) {
return;
}
var gcode = GCodeToGeometry.parse(gcodeStr);
if((gcode.size.max.x === gcode.size.min.x) &&
(gcode.size.max.y === gcode.size.min.y)) {
return;
}
var ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
var start = { x : gcode.size.min.x, y : gcode.size.min.y };
var i = 0, ratio = calculateRatio(gcode, canvas);
var cH = parseInt(canvas.height, 10);
for(i = 0; i < gcode.lines.length; i++) {
if(gcode.lines[i].type === "G0" && colors.G0 !== undefined) {
drawStraightLine(ctx, ratio, start, gcode.lines[i], cH, colors.G0);
} else if(gcode.lines[i].type === "G1" && colors.G1 !== undefined) {
drawStraightLine(ctx, ratio, start, gcode.lines[i], cH, colors.G1);
} else if((gcode.lines[i].type === "G2" ||
gcode.lines[i].type === "G3") && colors.G2G3 !== undefined)
{
drawCurvedLine(ctx, ratio, start, gcode.lines[i], cH, colors.G2G3);
}
}
};
/**
* Gets an image representing the G-Code.
* @param {string} gcodeStr The G-Code.
* @param {object} colors The colors defined by { G0, G1, G2G3 }, each field is
* a string of an hexadecimal color (ex: '#ff00ff). If one field is
* undefined, the corresponding G-Code command is not display.
* @param {number} width The width in pixel.
* @param {number} height The height in pixel.
* @return {string} The data URL of the image.
*/
GCode2DViewer.getImage = function(gcodeStr, colors, width, height) {
var canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
GCode2DViewer.preview(gcodeStr, colors, canvas);
return canvas.toDataURL("img/png");
};