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) => { dialog.showSaveDialog({title:"Save to .grid"},(fileName) => {
if (fileName === undefined){ return; } if (fileName === undefined){ return; }
fs.writeFileSync(fileName+'.grid', this.tool.export()); fs.writeFileSync(fileName+'.grid', content);
this.guide.refresh() 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) => { dialog.showSaveDialog({title:"Render to .png"},(fileName) => {
if (fileName === undefined){ return; } if (fileName === undefined){ return; }
fs.writeFileSync(fileName+'.png', this.renderer.to_png()); fs.writeFileSync(fileName+'.png', ready);
this.guide.refresh()
}); });
} }
this.export = function() this.export = function(content = this.renderer.to_svg())
{ {
dialog.showSaveDialog({title:"Export to .svg"},(fileName) => { dialog.showSaveDialog({title:"Export to .svg"},(fileName) => {
if (fileName === undefined){ return; } if (fileName === undefined){ return; }
fs.writeFileSync(fileName+".svg", this.renderer.to_svg()); fs.writeFileSync(fileName+".svg", content);
this.guide.refresh() this.guide.refresh()
}); });
} }

View File

@ -20,7 +20,7 @@ function Renderer()
this.el.width = 1280; this.el.width = 1280;
this.el.height = 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("width",dotgrid.tool.settings.size.width+"px");
this.svg_el.setAttribute("height",dotgrid.tool.settings.size.height+"px"); this.svg_el.setAttribute("height",dotgrid.tool.settings.size.height+"px");
@ -55,27 +55,34 @@ function Renderer()
this.to_png = function() this.to_png = function()
{ {
this.update(); this.refresh();
var xml = new XMLSerializer().serializeToString(this.svg_el); var xml = new XMLSerializer().serializeToString(this.svg_el);
var svg64 = btoa(xml); var svg64 = btoa(xml);
var b64Start = 'data:image/svg+xml;base64,'; var b64Start = 'data:image/svg+xml;base64,';
var image64 = b64Start + svg64; 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; 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'); this.to_png_ready = function(buffer)
var data = this.el.toDataURL('image/png').replace(/^data:image\/\w+;base64,/, ""); {
var buf = new Buffer(data, 'base64'); dotgrid.render(null,buffer)
return buf;
} }
this.to_svg = function() this.to_svg = function()
{ {
this.update(); this.refresh();
return this.svg_el.outerHTML; return this.svg_el.outerHTML;
} }