Fixed issue with png export

This commit is contained in:
Devine Lu Linvega 2018-05-11 10:37:10 +12:00
parent 7e5c965f4e
commit 7ec080cbc8
2 changed files with 26 additions and 18 deletions

View File

@ -135,29 +135,30 @@ function Dotgrid(width,height,grid_x,grid_y,block_x,block_y)
});
}
this.save = function()
this.save = function(content = this.tool.export())
{
dialog.showSaveDialog({title:"Save to .grid"},(fileName) => {
if (fileName === undefined){ return; }
fs.writeFileSync(fileName+'.grid', this.tool.export());
fs.writeFileSync(fileName+'.grid', content);
this.guide.refresh()
});
}
this.render = function()
this.render = function(content = this.renderer.to_png(), ready = null)
{
if(!ready){return; }
dialog.showSaveDialog({title:"Render to .png"},(fileName) => {
if (fileName === undefined){ return; }
fs.writeFileSync(fileName+'.png', this.renderer.to_png());
this.guide.refresh()
fs.writeFileSync(fileName+'.png', ready);
});
}
this.export = function()
this.export = function(content = this.renderer.to_svg())
{
dialog.showSaveDialog({title:"Export to .svg"},(fileName) => {
if (fileName === undefined){ return; }
fs.writeFileSync(fileName+".svg", this.renderer.to_svg());
fs.writeFileSync(fileName+".svg", content);
this.guide.refresh()
});
}

View File

@ -20,7 +20,7 @@ function Renderer()
this.el.width = 1280;
this.el.height = 1280;
this.update = function()
this.refresh = function()
{
this.svg_el.setAttribute("width",dotgrid.tool.settings.size.width+"px");
this.svg_el.setAttribute("height",dotgrid.tool.settings.size.height+"px");
@ -55,27 +55,34 @@ function Renderer()
this.to_png = function()
{
this.update();
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 img = document.createElement("img")
var img = new Image;
var canvas = this.el;
var ctx = canvas.getContext('2d');
img.onload = function(){
ctx.clearRect(0, 0, 1280, 1280);
ctx.drawImage(img, 0, 0, 1280, 1280);
var data = canvas.toDataURL('image/png').replace(/^data:image\/\w+;base64,/, "");
dotgrid.renderer.to_png_ready(new Buffer(data, 'base64'))
};
img.src = image64;
this.el.getContext('2d').clearRect(0, 0, 1280, 1280);
this.el.getContext('2d').drawImage(img, 0, 0, 1280, 1280);
}
var fs = require('fs');
var data = this.el.toDataURL('image/png').replace(/^data:image\/\w+;base64,/, "");
var buf = new Buffer(data, 'base64');
return buf;
this.to_png_ready = function(buffer)
{
dotgrid.render(null,buffer)
}
this.to_svg = function()
{
this.update();
this.refresh();
return this.svg_el.outerHTML;
}