pointvec/desktop/sources/scripts/renderer.js

90 lines
3.1 KiB
JavaScript
Raw Normal View History

2018-10-03 19:27:40 -04:00
'use strict'
2018-08-28 00:34:17 -04:00
2018-10-10 16:09:00 -04:00
DOTGRID.Renderer = function () {
2018-05-10 17:05:46 -04:00
// Create SVG parts
2018-10-03 19:27:40 -04:00
this.svg_el = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
this.svg_el.setAttribute('xmlns', 'http://www.w3.org/2000/svg')
this.svg_el.setAttribute('baseProfile', 'full')
this.svg_el.setAttribute('version', '1.1')
this.svg_el.style.fill = 'none'
this.layer_1 = document.createElementNS('http://www.w3.org/2000/svg', 'path')
this.layer_2 = document.createElementNS('http://www.w3.org/2000/svg', 'path')
this.layer_3 = document.createElementNS('http://www.w3.org/2000/svg', 'path')
this.svg_el.appendChild(this.layer_3)
this.svg_el.appendChild(this.layer_2)
this.svg_el.appendChild(this.layer_1)
this.update = function () {
2018-10-04 19:52:17 -04:00
this.svg_el.setAttribute('width', (DOTGRID.tool.settings.size.width - (5)) + 'px')
this.svg_el.setAttribute('height', (DOTGRID.tool.settings.size.height + (10)) + 'px')
this.svg_el.style.width = (DOTGRID.tool.settings.size.width - (5))
this.svg_el.style.height = DOTGRID.tool.settings.size.height + (10)
this.svg_el.style.strokeWidth = DOTGRID.tool.style().thickness
2018-05-10 17:05:46 -04:00
2018-10-04 19:52:17 -04:00
let styles = DOTGRID.tool.styles
let paths = DOTGRID.tool.paths()
2018-05-10 17:05:46 -04:00
2018-10-03 19:27:40 -04:00
this.layer_1.style.strokeWidth = styles[0].thickness
this.layer_1.style.strokeLinecap = styles[0].strokeLinecap
this.layer_1.style.strokeLinejoin = styles[0].strokeLinejoin
this.layer_1.style.stroke = styles[0].color
this.layer_1.style.fill = styles[0].fill
this.layer_1.setAttribute('d', paths[0])
this.layer_2.style.strokeWidth = styles[1].thickness
this.layer_2.style.strokeLinecap = styles[1].strokeLinecap
this.layer_2.style.strokeLinejoin = styles[1].strokeLinejoin
this.layer_2.style.stroke = styles[1].color
this.layer_2.style.fill = styles[1].fill
this.layer_2.setAttribute('d', paths[1])
this.layer_3.style.strokeWidth = styles[2].thickness
this.layer_3.style.strokeLinecap = styles[2].strokeLinecap
this.layer_3.style.strokeLinejoin = styles[2].strokeLinejoin
this.layer_3.style.stroke = styles[2].color
this.layer_3.style.fill = styles[2].fill
this.layer_3.setAttribute('d', paths[2])
2018-05-10 17:05:46 -04:00
}
2018-10-10 16:09:00 -04:00
this.svg64 = function () {
2018-10-03 19:27:40 -04:00
this.update()
2018-05-10 17:05:46 -04:00
2018-10-03 19:27:40 -04:00
let xml = new XMLSerializer().serializeToString(this.svg_el)
let svg64 = btoa(xml)
let b64Start = 'data:image/svg+xml;base64,'
2018-10-10 16:09:00 -04:00
return b64Start + svg64
}
this.to_png = function (size = DOTGRID.tool.settings.size, callback) {
let image64 = this.svg64()
2018-10-03 19:27:40 -04:00
let img = new Image()
2017-11-09 14:47:06 -05:00
2018-10-03 19:27:40 -04:00
let canvas = document.createElement('canvas')
2018-05-10 19:08:51 -04:00
2018-10-03 19:27:40 -04:00
canvas.width = (size.width) * 2
canvas.height = (size.height + 30) * 2
2018-05-10 19:08:51 -04:00
2018-10-03 19:27:40 -04:00
let ctx = canvas.getContext('2d')
2017-11-09 14:47:06 -05:00
2018-10-03 19:27:40 -04:00
img.onload = function () {
ctx.drawImage(img, 0, 0, (size.width) * 2, (size.height + 30) * 2)
2018-10-10 16:09:00 -04:00
let data = canvas.toDataURL('image/png')
callback(data, 'export.png')
2018-10-03 19:27:40 -04:00
}
img.src = image64
2018-05-10 18:37:10 -04:00
}
2018-10-10 16:09:00 -04:00
this.to_svg = function (callback) {
const image64 = this.svg64()
callback(image64, 'export.svg')
2018-08-17 17:34:24 -04:00
}
2018-10-10 16:09:00 -04:00
this.to_grid = function (callback) {
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
}