pointvec/desktop/sources/scripts/manager.js

90 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-10-03 23:27:40 +00:00
'use strict'
2018-08-28 04:34:17 +00:00
2019-01-09 03:09:20 +00:00
// Manages the SVG file
function Manager (dotgrid) {
2018-05-10 21:05:46 +00:00
// Create SVG parts
2019-01-09 02:45:20 +00: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-09 03:12:18 +00: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-09 02:45:20 +00:00
}
2018-10-03 23:27:40 +00:00
this.update = function () {
2019-04-22 01:09:42 +00:00
this.el.setAttribute('width', (dotgrid.tool.settings.size.width) + 'px')
this.el.setAttribute('height', (dotgrid.tool.settings.size.height) + 'px')
this.el.style.width = (dotgrid.tool.settings.size.width)
this.el.style.height = dotgrid.tool.settings.size.height
2019-01-09 02:45:20 +00:00
2019-04-21 23:49:47 +00:00
const styles = dotgrid.tool.styles
const paths = dotgrid.tool.paths()
2019-01-09 02:45:20 +00:00
for (const id in this.layers) {
2019-01-09 22:43:17 +00:00
let style = styles[id]
let path = paths[id]
2019-11-03 18:36:58 +00:00
const layer = this.layers[id]
2019-01-09 22:43:17 +00:00
// Easter Egg
2019-04-21 23:49:47 +00:00
if (dotgrid.tool.settings.crest === true) {
2019-01-09 22:43:17 +00:00
style = styles[0]
path = paths[0]
2019-04-21 23:49:47 +00: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 22:43:17 +00:00
}
2019-01-09 02:45:20 +00: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-04-21 23:36:12 +00:00
2019-01-09 22:43:17 +00:00
layer.setAttribute('d', path)
2019-01-09 02:45:20 +00:00
}
2018-05-10 21:05:46 +00:00
}
2018-10-10 20:09:00 +00:00
this.svg64 = function () {
2019-11-03 18:36:58 +00:00
const xml = new XMLSerializer().serializeToString(this.el)
const svg64 = btoa(xml)
const b64Start = 'data:image/svg+xml;base64,'
2018-10-10 20:09:00 +00:00
return b64Start + svg64
}
2019-01-09 02:48:03 +00:00
// Exporters
2019-04-21 23:49:47 +00:00
this.toPNG = function (size = dotgrid.tool.settings.size, callback) {
2019-01-09 02:48:03 +00:00
this.update()
2019-11-03 18:36:58 +00:00
const image64 = this.svg64()
const img = new Image()
const canvas = document.createElement('canvas')
2018-10-03 23:27:40 +00:00
canvas.width = (size.width) * 2
2019-01-09 02:36:26 +00:00
canvas.height = (size.height) * 2
2018-10-03 23:27:40 +00:00
img.onload = function () {
2019-01-09 02:48:03 +00:00
canvas.getContext('2d').drawImage(img, 0, 0, (size.width) * 2, (size.height) * 2)
callback(canvas.toDataURL('image/png'), 'export.png')
2018-10-03 23:27:40 +00:00
}
img.src = image64
2018-05-10 22:37:10 +00:00
}
2019-01-09 02:36:26 +00:00
this.toSVG = function (callback) {
2019-01-09 02:48:03 +00:00
this.update()
2018-10-10 20:09:00 +00:00
const image64 = this.svg64()
callback(image64, 'export.svg')
2018-08-17 21:34:24 +00:00
}
2019-01-09 02:36:26 +00:00
this.toGRID = function (callback) {
2019-01-09 02:48:03 +00:00
this.update()
2019-04-21 23:49:47 +00:00
const text = dotgrid.tool.export()
2018-10-10 20:09:00 +00:00
const file = new Blob([text], { type: 'text/plain' })
callback(URL.createObjectURL(file), 'export.grid')
2018-05-10 20:46:50 +00:00
}
2018-10-03 23:27:40 +00:00
}