From f30cbc58e237cb19377b826eef472943ec6f37cc Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Fri, 11 May 2018 11:08:51 +1200 Subject: [PATCH] Export icons --- desktop/sources/scripts/dotgrid.js | 43 ++++++++++++++++++++++++++++- desktop/sources/scripts/renderer.js | 22 +++++++-------- 2 files changed, 52 insertions(+), 13 deletions(-) diff --git a/desktop/sources/scripts/dotgrid.js b/desktop/sources/scripts/dotgrid.js index 4ad82ba..a3a9a96 100644 --- a/desktop/sources/scripts/dotgrid.js +++ b/desktop/sources/scripts/dotgrid.js @@ -38,6 +38,7 @@ function Dotgrid(width,height,grid_x,grid_y,block_x,block_y) this.controller.add("default","File","Save(.grid)",() => { dotgrid.save(); },"CmdOrCtrl+S"); this.controller.add("default","File","Render(.png)",() => { dotgrid.render(); },"CmdOrCtrl+R"); this.controller.add("default","File","Export(.svg)",() => { dotgrid.export(); },"CmdOrCtrl+E"); + this.controller.add("default","File","Build Icons",() => { dotgrid.build(); },"CmdOrCtrl+B"); this.controller.add("default","Edit","Copy",() => { document.execCommand('copy'); },"CmdOrCtrl+C"); this.controller.add("default","Edit","Cut",() => { document.execCommand('cut'); },"CmdOrCtrl+X"); @@ -144,12 +145,13 @@ function Dotgrid(width,height,grid_x,grid_y,block_x,block_y) }); } - this.render = function(content = this.renderer.to_png(), ready = null) + this.render = function(content = this.renderer.to_png(), ready = null, size = null) { if(!ready){return; } dialog.showSaveDialog({title:"Render to .png"},(fileName) => { if (fileName === undefined){ return; } + console.log(`Rendered ${size.width}x${size.height}`) fs.writeFileSync(fileName+'.png', ready); }); } @@ -163,6 +165,45 @@ function Dotgrid(width,height,grid_x,grid_y,block_x,block_y) }); } + this.bundle = {} + + this.build = function() + { + this.bundle = {} + + var sizes = [ + {width:16,height:16}, + {width:32,height:32}, + {width:52,height:52}, + {width:64,height:64}, + {width:72,height:72}, + {width:96,height:96}, + {width:128,height:128}, + {width:256,height:256}, + {width:512,height:512} + ] + + for(id in sizes){ + this.renderer.to_png(sizes[id],dotgrid.package) + } + } + + this.package = function(n = null, ready,size) + { + dotgrid.bundle[`${size.width}x${size.height}`] = ready + + console.log(`Rendered ${size.width}x${size.height}`,`${Object.keys(dotgrid.bundle).length}/9`) + + if(Object.keys(dotgrid.bundle).length == 9){ + dialog.showSaveDialog({title:"Export to Icons"},(fileName) => { + if (fileName === undefined){ return; } + for(id in dotgrid.bundle){ + fs.writeFileSync(`${fileName}.${id}.png`, dotgrid.bundle[id]); + } + }); + } + } + // Cursor this.mouse_down = function(e) diff --git a/desktop/sources/scripts/renderer.js b/desktop/sources/scripts/renderer.js index 7028237..1ec4a58 100644 --- a/desktop/sources/scripts/renderer.js +++ b/desktop/sources/scripts/renderer.js @@ -14,11 +14,6 @@ function Renderer() this.svg_el.appendChild(this.layer_3); this.svg_el.appendChild(this.layer_2); this.svg_el.appendChild(this.layer_1); - - // Etc - this.el = document.createElement("canvas"); - this.el.width = 1280; - this.el.height = 1280; this.refresh = function() { @@ -53,7 +48,7 @@ function Renderer() this.layer_1.setAttribute("d",paths[2]) } - this.to_png = function() + this.to_png = function(size = {width:1280,height:1280},callback = dotgrid.render) { this.refresh(); @@ -63,21 +58,24 @@ function Renderer() var image64 = b64Start + svg64; var img = new Image; - var canvas = this.el; + var canvas = document.createElement("canvas"); + + canvas.width = size.width; + canvas.height = size.height; + var ctx = canvas.getContext('2d'); img.onload = function(){ - ctx.clearRect(0, 0, 1280, 1280); - ctx.drawImage(img, 0, 0, 1280, 1280); + ctx.drawImage(img, 0, 0, size.width, size.height); var data = canvas.toDataURL('image/png').replace(/^data:image\/\w+;base64,/, ""); - dotgrid.renderer.to_png_ready(new Buffer(data, 'base64')) + dotgrid.renderer.to_png_ready(callback, new Buffer(data, 'base64'),size) }; img.src = image64; } - this.to_png_ready = function(buffer) + this.to_png_ready = function(callback, buffer, size) { - dotgrid.render(null,buffer) + callback(null,buffer,size) } this.to_svg = function()