pointvec/desktop/sources/scripts/manager.js

89 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-10-03 19:27:40 -04:00
'use strict'
2018-08-28 00:34:17 -04:00
2019-01-08 22:09:20 -05:00
// Manages the SVG file
function Manager (dotgrid) {
2018-05-10 17:05:46 -04:00
// Create SVG parts
2019-01-08 21:45:20 -05:00
this.el = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
this.el.setAttribute('xmlns', 'http://www.w3.org/2000/svg')
this.el.setAttribute('baseProfile', 'full')
this.el.setAttribute('version', '1.1')
this.el.style.fill = 'none'
this.layers = []
this.install = function () {
2019-01-08 22:12:18 -05:00
this.el.appendChild(this.layers[2] = document.createElementNS('http://www.w3.org/2000/svg', 'path'))
this.el.appendChild(this.layers[1] = document.createElementNS('http://www.w3.org/2000/svg', 'path'))
this.el.appendChild(this.layers[0] = document.createElementNS('http://www.w3.org/2000/svg', 'path'))
2019-01-08 21:45:20 -05:00
}
2018-10-03 19:27:40 -04:00
this.update = function () {
2019-01-08 23:51:10 -05:00
this.el.setAttribute('width', (DOTGRID.tool.settings.size.width + 15) + 'px')
this.el.setAttribute('height', (DOTGRID.tool.settings.size.height + 15) + 'px')
this.el.style.width = (DOTGRID.tool.settings.size.width + 15)
this.el.style.height = DOTGRID.tool.settings.size.height + 15
2019-01-08 21:45:20 -05:00
const styles = DOTGRID.tool.styles
const paths = DOTGRID.tool.paths()
for (const id in this.layers) {
2019-01-09 17:43:17 -05:00
let style = styles[id]
let path = paths[id]
let layer = this.layers[id]
// Easter Egg
if (DOTGRID.tool.settings.crest === true) {
style = styles[0]
path = paths[0]
2019-01-09 22:50:15 -05:00
layer.setAttribute('transform', `rotate(${parseInt(id) * 120} ${(DOTGRID.tool.settings.size.width / 2) + 7.5} ${(DOTGRID.tool.settings.size.height / 2) + 7.5})`)
2019-01-09 17:43:17 -05:00
}
2019-01-08 21:45:20 -05:00
layer.style.strokeWidth = style.thickness
layer.style.strokeLinecap = style.strokeLinecap
layer.style.strokeLinejoin = style.strokeLinejoin
layer.style.stroke = style.color
layer.style.fill = style.fill
2019-01-09 17:43:17 -05:00
layer.setAttribute('d', path)
2019-01-08 21:45:20 -05:00
}
2018-05-10 17:05:46 -04:00
}
2018-10-10 16:09:00 -04:00
this.svg64 = function () {
2019-01-08 21:45:20 -05:00
let xml = new XMLSerializer().serializeToString(this.el)
2018-10-03 19:27:40 -04:00
let svg64 = btoa(xml)
let b64Start = 'data:image/svg+xml;base64,'
2018-10-10 16:09:00 -04:00
return b64Start + svg64
}
2019-01-08 21:48:03 -05:00
// Exporters
2019-01-08 21:36:26 -05:00
this.toPNG = function (size = DOTGRID.tool.settings.size, callback) {
2019-01-08 21:48:03 -05:00
this.update()
2018-10-10 16:09:00 -04:00
let image64 = this.svg64()
2018-10-03 19:27:40 -04:00
let img = new Image()
let canvas = document.createElement('canvas')
canvas.width = (size.width) * 2
2019-01-08 21:36:26 -05:00
canvas.height = (size.height) * 2
2018-10-03 19:27:40 -04:00
img.onload = function () {
2019-01-08 21:48:03 -05:00
canvas.getContext('2d').drawImage(img, 0, 0, (size.width) * 2, (size.height) * 2)
callback(canvas.toDataURL('image/png'), 'export.png')
2018-10-03 19:27:40 -04:00
}
img.src = image64
2018-05-10 18:37:10 -04:00
}
2019-01-08 21:36:26 -05:00
this.toSVG = function (callback) {
2019-01-08 21:48:03 -05:00
this.update()
2018-10-10 16:09:00 -04:00
const image64 = this.svg64()
callback(image64, 'export.svg')
2018-08-17 17:34:24 -04:00
}
2019-01-08 21:36:26 -05:00
this.toGRID = function (callback) {
2019-01-08 21:48:03 -05:00
this.update()
2018-10-10 16:09:00 -04:00
const text = DOTGRID.tool.export()
const file = new Blob([text], { type: 'text/plain' })
callback(URL.createObjectURL(file), 'export.grid')
2018-05-10 16:46:50 -04:00
}
2018-10-03 19:27:40 -04:00
}