2018-05-10 18:06:03 -04:00
|
|
|
function Renderer()
|
2017-11-09 14:47:06 -05:00
|
|
|
{
|
2018-05-10 17:05:46 -04:00
|
|
|
// Create SVG parts
|
|
|
|
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";
|
2017-11-09 14:47:06 -05:00
|
|
|
|
2018-05-10 17:05:46 -04:00
|
|
|
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");
|
2017-11-09 14:47:06 -05:00
|
|
|
|
2018-05-10 17:05:46 -04:00
|
|
|
this.svg_el.appendChild(this.layer_3);
|
|
|
|
this.svg_el.appendChild(this.layer_2);
|
|
|
|
this.svg_el.appendChild(this.layer_1);
|
|
|
|
|
2018-05-10 18:37:10 -04:00
|
|
|
this.refresh = function()
|
2017-11-09 14:47:06 -05:00
|
|
|
{
|
2018-08-02 21:48:16 -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);
|
2018-05-10 17:05:46 -04:00
|
|
|
this.svg_el.style.strokeWidth = dotgrid.tool.style().thickness;
|
|
|
|
|
|
|
|
var styles = dotgrid.tool.styles
|
|
|
|
var paths = dotgrid.tool.paths()
|
|
|
|
|
|
|
|
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;
|
2018-05-10 19:17:20 -04:00
|
|
|
this.layer_2.setAttribute("d",paths[1])
|
2018-05-10 17:05:46 -04:00
|
|
|
|
|
|
|
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;
|
2018-05-10 19:17:20 -04:00
|
|
|
this.layer_3.setAttribute("d",paths[2])
|
2018-05-10 17:05:46 -04:00
|
|
|
}
|
|
|
|
|
2018-05-12 17:50:19 -04:00
|
|
|
this.to_png = function(size = dotgrid.tool.settings.size,callback = dotgrid.render)
|
2018-05-10 17:05:46 -04:00
|
|
|
{
|
2018-08-17 17:34:24 -04:00
|
|
|
if(!dialog){ return this.to_png_web(size); }
|
|
|
|
|
2018-05-10 18:37:10 -04:00
|
|
|
this.refresh();
|
2018-05-10 17:05:46 -04:00
|
|
|
|
|
|
|
var xml = new XMLSerializer().serializeToString(this.svg_el);
|
2017-11-09 14:47:06 -05:00
|
|
|
var svg64 = btoa(xml);
|
|
|
|
var b64Start = 'data:image/svg+xml;base64,';
|
|
|
|
var image64 = b64Start + svg64;
|
2018-05-10 18:37:10 -04:00
|
|
|
var img = new Image;
|
2017-11-09 14:47:06 -05:00
|
|
|
|
2018-05-10 19:08:51 -04:00
|
|
|
var canvas = document.createElement("canvas");
|
|
|
|
|
2018-08-02 21:48:16 -04:00
|
|
|
canvas.width = (size.width)*2;
|
|
|
|
canvas.height = (size.height+30)*2;
|
2018-05-10 19:08:51 -04:00
|
|
|
|
2018-05-10 18:37:10 -04:00
|
|
|
var ctx = canvas.getContext('2d');
|
2017-11-09 14:47:06 -05:00
|
|
|
|
2018-05-10 18:37:10 -04:00
|
|
|
img.onload = function(){
|
2018-08-02 21:48:16 -04:00
|
|
|
ctx.drawImage(img, 0, 0, (size.width)*2, (size.height+30)*2);
|
2018-05-10 18:37:10 -04:00
|
|
|
var data = canvas.toDataURL('image/png').replace(/^data:image\/\w+;base64,/, "");
|
2018-05-10 19:08:51 -04:00
|
|
|
dotgrid.renderer.to_png_ready(callback, new Buffer(data, 'base64'),size)
|
2018-05-10 18:37:10 -04:00
|
|
|
};
|
|
|
|
img.src = image64;
|
|
|
|
}
|
|
|
|
|
2018-05-10 19:08:51 -04:00
|
|
|
this.to_png_ready = function(callback, buffer, size)
|
2018-05-10 18:37:10 -04:00
|
|
|
{
|
2018-05-10 19:08:51 -04:00
|
|
|
callback(null,buffer,size)
|
2017-11-09 14:47:06 -05:00
|
|
|
}
|
2018-05-10 16:46:50 -04:00
|
|
|
|
2018-08-17 17:34:24 -04:00
|
|
|
this.to_png_web = function(size)
|
|
|
|
{
|
|
|
|
this.refresh();
|
|
|
|
|
|
|
|
var xml = new XMLSerializer().serializeToString(this.svg_el);
|
|
|
|
var svg64 = btoa(xml);
|
|
|
|
var b64Start = 'data:image/svg+xml;base64,';
|
|
|
|
var image64 = b64Start + svg64;
|
|
|
|
|
|
|
|
var canvas = document.createElement("canvas");
|
|
|
|
var ctx = canvas.getContext('2d');
|
|
|
|
|
|
|
|
var win = window.open('about:blank','image from canvas');
|
|
|
|
var img = new Image;
|
|
|
|
|
|
|
|
canvas.width = size.width*2;
|
|
|
|
canvas.height = size.height*2;
|
|
|
|
|
|
|
|
img.onload = function(){
|
|
|
|
ctx.drawImage(img, 0, 0, size.width*2, size.height*2);
|
2018-08-17 17:56:22 -04:00
|
|
|
win.document.write(`<style>body { background:${dotgrid.theme.active.background}}</style><img width='${size.width/2}' height='${size.height/2}' src='${canvas.toDataURL("image/png")}' alt='from canvas'/>`);
|
2018-08-17 17:34:24 -04:00
|
|
|
};
|
|
|
|
img.src = image64;
|
|
|
|
}
|
|
|
|
|
2018-05-10 16:46:50 -04:00
|
|
|
this.to_svg = function()
|
|
|
|
{
|
2018-05-10 18:37:10 -04:00
|
|
|
this.refresh();
|
2018-05-10 16:46:50 -04:00
|
|
|
|
2018-05-10 17:05:46 -04:00
|
|
|
return this.svg_el.outerHTML;
|
2018-05-10 16:46:50 -04:00
|
|
|
}
|
2017-11-09 14:47:06 -05:00
|
|
|
}
|