-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
130 lines (106 loc) · 2.98 KB
/
index.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
const { createPath, pathsToPolylines } = require('canvas-sketch-util/penplot')
const simplify = require('simplify-js')
const GCodeFile = require('gcode-file')
class GCodeContext {
constructor({context, autoBind = true, gcodeSettings = {}}) {
this.ctx = context
this.gCode = new GCodeFile(gcodeSettings)
if (autoBind) {
this.addListeners()
}
this.clear()
}
get paths() {
return this.layers[this.layers.length - 1].paths
}
set paths(val) {
return this.layers[this.layers.length - 1].paths = val
}
get path() {
return this.paths[this.paths.length - 1]
}
addListeners() {
window.addEventListener('keydown', event => {
if ((event.ctrlKey || event.metaKey) && event.which == 83) {
event.preventDefault()
this.saveFile()
return false
}
})
}
addLayer(name = ''){
this.layers.push({
name,
paths: []
})
}
saveFile() {
this.gCode.clear()
this.layers.forEach( ({name, paths}, i) => {
if(i > 0){
this.gCode.addLayer(name)
}
console.log(`[File ${i}] Converting path to polylines...`)
const lines = pathsToPolylines(paths)
lines.forEach(l => simplify(l, 1, true))
console.log(`[File ${i}] Add data to gcode file...`)
this.gCode.addPolylines(lines)
})
console.log(`Download files...`)
this.gCode.downloadFile()
}
clear() {
this.layers = []
this.addLayer()
this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height)
}
beginPath() {
this.ctx.beginPath()
this.paths.push(createPath())
}
closePath() {
this.ctx.closePath()
this.paths[this.paths.length - 1].closePath()
}
moveTo(x, y) {
this.ctx.moveTo(x, y)
this.path.moveTo(x, y)
}
lineTo(x, y) {
this.ctx.lineTo(x, y)
this.path.lineTo(x, y)
}
arc(x, y, r, sAngle, eAngle, counterclockwise) {
this.ctx.arc(x, y, r, sAngle, eAngle, counterclockwise)
this.path.arc(x, y, r, sAngle, eAngle, counterclockwise)
}
arcTo(x1, y1, x2, y2, radius) {
this.ctx.arcTo(x1, y1, x2, y2, radius)
this.path.arcTo(x1, y1, x2, y2, radius)
}
rect(x, y, width, height) {
this.ctx.rect(x, y, width, height)
this.path.rect(x, y, width, height)
}
quadraticCurveTo(cpx, cpy, x, y) {
this.ctx.quadraticCurveTo(cpx, cpy, x, y)
this.path.quadraticCurveTo(cpx, cpy, x, y)
}
bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y) {
this.ctx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)
this.path.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)
}
stroke() {
this.ctx.stroke()
}
setLineDash() {
console.warn('[GCodeContext] setLineDash is not available in this implementation and it will be skipped')
}
lineDashOffset() {
console.warn('[GCodeContext] lineDashOffset is not available in this implementation and it will be skipped')
}
fill() {
console.warn('[GCodeContext] fill is not available in this implementation and it will be skipped')
}
}
module.exports = GCodeContext