From 9aba931b0aefbf46001ee27d39d3476229439fc7 Mon Sep 17 00:00:00 2001 From: juliandescottes Date: Thu, 20 Sep 2012 01:10:13 +0200 Subject: [PATCH] Cleanup of SpritesheetRenderer --- js/rendering/SpritesheetRenderer.js | 53 +++++++++++++++-------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/js/rendering/SpritesheetRenderer.js b/js/rendering/SpritesheetRenderer.js index 805da137..b3581c5a 100644 --- a/js/rendering/SpritesheetRenderer.js +++ b/js/rendering/SpritesheetRenderer.js @@ -2,11 +2,10 @@ var ns = $.namespace("pskl.rendering"); - ns.SpritesheetRenderer = function (framesheet, renderingOptions) { + ns.SpritesheetRenderer = function (framesheet) { this.framesheet = framesheet; }; - /** * Will open a new window displaying the spritesheet as a png */ @@ -14,31 +13,14 @@ var canvas = this.createCanvas_(); for (var i = 0 ; i < this.framesheet.getFrameCount() ; i++) { var frame = this.framesheet.getFrameByIndex(i); - var offsetWidth = i * this.framesheet.getWidth(); - var offsetHeight = 0; - this.drawFrameInCanvas_(frame, canvas, offsetWidth, offsetHeight); - } - var options = [ - "dialog=yes", "scrollbars=no", "status=no", - "width=" + this.framesheet.getWidth() * this.framesheet.getFrameCount(), - "height="+this.framesheet.getHeight() - ].join(","); - - window.open(canvas.toDataURL("image/png"), "piskel-export", options); - }; - - ns.SpritesheetRenderer.prototype.createCanvas_ = function () { - var frameCount = this.framesheet.getFrameCount(); - if (frameCount > 0){ - var width = frameCount * this.framesheet.getWidth(); - var height = this.framesheet.getHeight(); - var canvas = pskl.CanvasUtils.createCanvas(width, height); - return canvas; - } else { - throw "Cannot render empty Spritesheet" + this.drawFrameInCanvas_(frame, canvas, i * this.framesheet.getWidth(), 0); } + this.openCanvasAsPNGInWindow_(canvas); }; + /** + * TODO : (Julz) Mutualize with code already present in FrameRenderer + */ ns.SpritesheetRenderer.prototype.drawFrameInCanvas_ = function (frame, canvas, offsetWidth, offsetHeight) { var context = canvas.getContext('2d'); for(var col = 0, width = frame.getWidth(); col < width; col++) { @@ -50,5 +32,26 @@ } } } - } + }; + + ns.SpritesheetRenderer.prototype.createCanvas_ = function () { + var frameCount = this.framesheet.getFrameCount(); + if (frameCount > 0){ + var width = frameCount * this.framesheet.getWidth(); + var height = this.framesheet.getHeight(); + return pskl.CanvasUtils.createCanvas(width, height); + } else { + throw "Cannot render empty Spritesheet" + } + }; + + ns.SpritesheetRenderer.prototype.openCanvasAsPNGInWindow_ = function (canvas) { + var options = [ + "dialog=yes", "scrollbars=no", "status=no", + "width=" + this.framesheet.getWidth() * this.framesheet.getFrameCount(), + "height=" + this.framesheet.getHeight() + ].join(","); + + window.open(canvas.toDataURL("image/png"), "piskel-export", options); + }; })(); \ No newline at end of file