pointvec/desktop/sources/scripts/renderer.js

79 lines
2.5 KiB
JavaScript
Raw Normal View History

2018-10-03 23:27:40 +00:00
'use strict'
2018-08-28 04:34:17 +00:00
2018-10-10 20:09:00 +00:00
DOTGRID.Renderer = function () {
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 () {
this.layers[0] = document.createElementNS('http://www.w3.org/2000/svg', 'path')
this.layers[1] = document.createElementNS('http://www.w3.org/2000/svg', 'path')
this.layers[2] = document.createElementNS('http://www.w3.org/2000/svg', 'path')
this.el.appendChild(this.layers[2])
this.el.appendChild(this.layers[1])
this.el.appendChild(this.layers[0])
}
2018-10-03 23:27:40 +00:00
this.update = function () {
2019-01-09 02:45:20 +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
const styles = DOTGRID.tool.styles
const paths = DOTGRID.tool.paths()
for (const id in this.layers) {
const style = styles[id]
const path = paths[id]
const layer = this.layers[id]
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
layer.setAttribute('d', paths[id])
}
2018-05-10 21:05:46 +00:00
}
2018-10-10 20:09:00 +00:00
this.svg64 = function () {
2018-10-03 23:27:40 +00:00
this.update()
2019-01-09 02:45:20 +00:00
let xml = new XMLSerializer().serializeToString(this.el)
2018-10-03 23:27:40 +00:00
let svg64 = btoa(xml)
let b64Start = 'data:image/svg+xml;base64,'
2018-10-10 20:09:00 +00:00
return b64Start + svg64
}
2019-01-09 02:36:26 +00:00
this.toPNG = function (size = DOTGRID.tool.settings.size, callback) {
2018-10-10 20:09:00 +00:00
let image64 = this.svg64()
2018-10-03 23:27:40 +00:00
let img = new Image()
let canvas = document.createElement('canvas')
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
let ctx = canvas.getContext('2d')
img.onload = function () {
2019-01-09 02:36:26 +00:00
ctx.drawImage(img, 0, 0, (size.width) * 2, (size.height) * 2)
2018-10-10 20:09:00 +00:00
let data = canvas.toDataURL('image/png')
callback(data, '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) {
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) {
2018-10-10 20:09:00 +00:00
const text = DOTGRID.tool.export()
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
}