2018-10-03 19:27:40 -04:00
|
|
|
'use strict'
|
2018-08-28 00:34:17 -04:00
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
function Generator (layer, style) {
|
|
|
|
this.layer = layer
|
|
|
|
this.style = style
|
2018-05-09 17:56:55 -04:00
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
function operate (layer, offset, scale, mirror = 0, angle = 0) {
|
2018-10-01 15:38:14 -04:00
|
|
|
const l = copy(layer)
|
2018-05-09 17:56:55 -04:00
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
for (const k1 in l) {
|
|
|
|
const seg = l[k1]
|
|
|
|
for (const k2 in seg.vertices) {
|
2019-04-21 19:36:12 -04:00
|
|
|
if (mirror === 1 || mirror === 3) { seg.vertices[k2].x = (DOTGRID.tool.settings.size.width) - seg.vertices[k2].x + 15 }
|
|
|
|
if (mirror === 2 || mirror === 3) { seg.vertices[k2].y = (DOTGRID.tool.settings.size.height) - seg.vertices[k2].y + 15 }
|
2018-05-28 03:24:19 -04:00
|
|
|
|
|
|
|
// Offset
|
2018-05-09 17:56:55 -04:00
|
|
|
seg.vertices[k2].x += offset.x
|
|
|
|
seg.vertices[k2].y += offset.y
|
2018-05-28 03:24:19 -04:00
|
|
|
|
|
|
|
// Rotate
|
2018-10-04 19:52:17 -04:00
|
|
|
const center = { x: (DOTGRID.tool.settings.size.width / 2) + offset.x + (7.5), y: (DOTGRID.tool.settings.size.height / 2) + offset.y + 30 }
|
2019-01-08 23:53:28 -05:00
|
|
|
seg.vertices[k2] = rotatePoint(seg.vertices[k2], center, angle)
|
2018-05-28 03:24:19 -04:00
|
|
|
|
|
|
|
// Scale
|
|
|
|
seg.vertices[k2].x *= scale
|
2018-05-09 17:56:55 -04:00
|
|
|
seg.vertices[k2].y *= scale
|
|
|
|
}
|
|
|
|
}
|
2018-10-03 19:27:40 -04:00
|
|
|
return l
|
2018-05-09 17:56:55 -04:00
|
|
|
}
|
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
this.render = function (prev, segment, mirror = 0) {
|
|
|
|
const type = segment.type
|
|
|
|
const vertices = segment.vertices
|
|
|
|
let html = ''
|
|
|
|
let skip = 0
|
|
|
|
|
|
|
|
for (const id in vertices) {
|
|
|
|
if (skip > 0) { skip -= 1; continue }
|
2018-05-09 17:56:55 -04:00
|
|
|
|
2019-04-21 19:36:12 -04:00
|
|
|
let vertex = vertices[parseInt(id)]
|
2018-10-03 19:27:40 -04:00
|
|
|
let next = vertices[parseInt(id) + 1]
|
2019-01-08 23:53:28 -05:00
|
|
|
let afterNext = vertices[parseInt(id) + 2]
|
2018-05-09 17:56:55 -04:00
|
|
|
|
2019-04-21 19:36:12 -04:00
|
|
|
if (parseInt(id) === 0 && !prev || parseInt(id) === 0 && prev && (prev.x !== vertex.x || prev.y !== vertex.y)) {
|
2018-10-03 19:27:40 -04:00
|
|
|
html += `M${vertex.x},${vertex.y} `
|
2018-05-09 17:56:55 -04:00
|
|
|
}
|
2018-10-03 19:27:40 -04:00
|
|
|
|
2019-04-21 19:36:12 -04:00
|
|
|
if (type === 'line') {
|
2018-11-22 16:03:33 -05:00
|
|
|
html += this._line(vertex)
|
2019-04-21 19:36:12 -04:00
|
|
|
} else if (type === 'arc_c') {
|
2018-12-14 15:06:00 -05:00
|
|
|
let clock = mirror > 0 && mirror < 3 ? '0,0' : '0,1'
|
2018-11-22 16:03:33 -05:00
|
|
|
html += this._arc(vertex, next, clock)
|
2019-04-21 19:36:12 -04:00
|
|
|
} else if (type === 'arc_r') {
|
2018-12-14 15:06:00 -05:00
|
|
|
let clock = mirror > 0 && mirror < 3 ? '0,1' : '0,0'
|
2018-11-22 16:03:33 -05:00
|
|
|
html += this._arc(vertex, next, clock)
|
2019-04-21 19:36:12 -04:00
|
|
|
} else if (type === 'arc_c_full') {
|
2018-11-08 17:41:41 -05:00
|
|
|
let clock = mirror > 0 ? '1,0' : '1,1'
|
2018-11-22 16:03:33 -05:00
|
|
|
html += this._arc(vertex, next, clock)
|
2019-04-21 19:36:12 -04:00
|
|
|
} else if (type === 'arc_r_full') {
|
2018-11-08 17:41:41 -05:00
|
|
|
let clock = mirror > 0 ? '1,1' : '1,0'
|
2018-11-22 16:03:33 -05:00
|
|
|
html += this._arc(vertex, next, clock)
|
2019-04-21 19:36:12 -04:00
|
|
|
} else if (type === 'bezier') {
|
2019-01-08 23:53:28 -05:00
|
|
|
html += this._bezier(next, afterNext)
|
2018-06-03 17:52:19 -04:00
|
|
|
skip = 1
|
2018-05-09 17:56:55 -04:00
|
|
|
}
|
|
|
|
}
|
2018-05-10 17:13:01 -04:00
|
|
|
|
2019-04-21 19:36:12 -04:00
|
|
|
if (segment.type === 'close') {
|
2018-10-03 19:27:40 -04:00
|
|
|
html += 'Z '
|
2018-05-10 17:13:01 -04:00
|
|
|
}
|
2018-10-03 19:27:40 -04:00
|
|
|
|
2018-05-09 17:56:55 -04:00
|
|
|
return html
|
|
|
|
}
|
|
|
|
|
2018-11-22 16:03:33 -05:00
|
|
|
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} `
|
|
|
|
}
|
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
this.convert = function (layer, mirror, angle) {
|
|
|
|
let s = ''
|
2018-08-26 15:39:15 -04:00
|
|
|
let prev = null
|
2018-10-03 19:27:40 -04:00
|
|
|
for (const id in layer) {
|
2019-04-21 19:36:12 -04:00
|
|
|
const seg = layer[parseInt(id)]
|
2018-10-03 19:27:40 -04:00
|
|
|
s += `${this.render(prev, seg, mirror)}`
|
|
|
|
prev = seg.vertices ? seg.vertices[seg.vertices.length - 1] : null
|
2018-05-09 17:56:55 -04:00
|
|
|
}
|
2018-10-03 19:27:40 -04:00
|
|
|
return s
|
2018-05-10 17:37:47 -04:00
|
|
|
}
|
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
this.toString = function (offset = { x: 0, y: 0 }, scale = 1, mirror = this.style && this.style.mirror_style ? this.style.mirror_style : 0) {
|
|
|
|
let s = this.convert(operate(this.layer, offset, scale))
|
2018-05-10 17:47:09 -04:00
|
|
|
|
2019-04-21 19:36:12 -04:00
|
|
|
if (mirror === 1 || mirror === 2 || mirror === 3) {
|
2018-10-03 19:27:40 -04:00
|
|
|
s += this.convert(operate(this.layer, offset, scale, mirror), mirror)
|
2018-05-10 17:47:09 -04:00
|
|
|
}
|
2018-05-28 03:24:19 -04:00
|
|
|
|
2018-05-10 17:37:47 -04:00
|
|
|
return s
|
2018-05-09 17:56:55 -04:00
|
|
|
}
|
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
function copy (data) { return data ? JSON.parse(JSON.stringify(data)) : [] }
|
2019-01-08 23:53:28 -05:00
|
|
|
function rotatePoint (point, origin, angle) { angle = angle * Math.PI / 180.0; return { x: (Math.cos(angle) * (point.x - origin.x) - Math.sin(angle) * (point.y - origin.y) + origin.x).toFixed(1), y: (Math.sin(angle) * (point.x - origin.x) + Math.cos(angle) * (point.y - origin.y) + origin.y).toFixed(1) } }
|
2018-10-03 19:27:40 -04:00
|
|
|
}
|