diff --git a/desktop/sources/scripts/generator.js b/desktop/sources/scripts/generator.js index efcb3f7..14738fb 100644 --- a/desktop/sources/scripts/generator.js +++ b/desktop/sources/scripts/generator.js @@ -47,24 +47,22 @@ function Generator (layer, style) { } if (type == 'line') { - html += `L${vertex.x},${vertex.y} ` + html += this._line(vertex) } else if (type == 'arc_c') { let clock = mirror > 0 ? '0,0' : '0,1' - html += next ? `A${Math.abs(next.x - vertex.x)},${Math.abs(next.y - vertex.y)} 0 ${clock} ${next.x},${next.y} ` : '' + html += this._arc(vertex, next, clock) } else if (type == 'arc_r') { let clock = mirror > 0 ? '0,1' : '0,0' - html += next ? `A${Math.abs(next.x - vertex.x)},${Math.abs(next.y - vertex.y)} 0 ${clock} ${next.x},${next.y} ` : '' + html += this._arc(vertex, next, clock) } else if (type == 'arc_c_full') { let clock = mirror > 0 ? '1,0' : '1,1' - html += next ? `A${Math.abs(next.x - vertex.x)},${Math.abs(next.y - vertex.y)} 0 ${clock} ${next.x},${next.y} ` : '' + html += this._arc(vertex, next, clock) } else if (type == 'arc_r_full') { let clock = mirror > 0 ? '1,1' : '1,0' - html += next ? `A${Math.abs(next.x - vertex.x)},${Math.abs(next.y - vertex.y)} 0 ${clock} ${next.x},${next.y} ` : '' + html += this._arc(vertex, next, clock) } else if (type == 'bezier') { - html += next && after_next ? `Q${next.x},${next.y} ${after_next.x},${after_next.y} ` : '' + html += this._bezier(next, after_next) skip = 1 - } else { - console.warn(`unknown type:${type}`) } } @@ -75,6 +73,24 @@ function Generator (layer, style) { return html } + this._line = function (a) { + return `L${a.x},${a.y} ` + } + + this._arc = function (a, b, c) { + if (!a || !b || !c) { return '' } + + const offset = { x: b.x - a.x, y: b.y - a.y } + + if (offset.x === 0 || offset.y === 0) { return this._line(b) } + return `A${Math.abs(b.x - a.x)},${Math.abs(b.y - a.y)} 0 ${c} ${b.x},${b.y} ` + } + + this._bezier = function (a, b) { + if (!a || !b) { return '' } + return `Q${a.x},${a.y} ${b.x},${b.y} ` + } + this.convert = function (layer, mirror, angle) { let s = '' let prev = null diff --git a/desktop/sources/scripts/tool.js b/desktop/sources/scripts/tool.js index 7300f93..733037b 100644 --- a/desktop/sources/scripts/tool.js +++ b/desktop/sources/scripts/tool.js @@ -142,17 +142,20 @@ DOTGRID.Tool = function () { return null } + this.add_segment = function (type, vertices) { + let append_target = this.can_append({ type: type, vertices: vertices }) + if (append_target) { + this.layer()[append_target].vertices = this.layer()[append_target].vertices.concat(vertices) + } else { + this.layer().push({ type: type, vertices: vertices }) + } + } + this.cast = function (type) { if (!this.layer()) { this.layers[this.index] = [] } if (!this.can_cast(type)) { console.warn('Cannot cast'); return } - let append_target = this.can_append({ type: type, vertices: this.vertices.slice() }) - - if (append_target) { - this.layers[this.index][append_target].vertices = this.layers[this.index][append_target].vertices.concat(this.vertices.slice()) - } else { - this.layer().push({ type: type, vertices: this.vertices.slice() }) - } + this.add_segment(type, this.vertices.slice()) DOTGRID.history.push(this.layers)