piskel/js/rendering/SpritesheetRenderer.js

63 lines
2.1 KiB
JavaScript
Raw Normal View History

(function () {
var ns = $.namespace("pskl.rendering");
2012-09-19 23:10:13 +00:00
ns.SpritesheetRenderer = function (framesheet) {
this.framesheet = framesheet;
};
ns.SpritesheetRenderer.prototype.renderAsImageDataSpritesheetPNG = function () {
var canvas = this.createCanvas_();
for (var i = 0 ; i < this.framesheet.getFrameCount() ; i++) {
var frame = this.framesheet.getFrameByIndex(i);
2012-09-19 23:10:13 +00:00
this.drawFrameInCanvas_(frame, canvas, i * this.framesheet.getWidth(), 0);
}
2013-06-13 15:56:44 +00:00
return canvas.toDataURL("image/png");
};
ns.SpritesheetRenderer.prototype.renderAsImageDataAnimatedGIF = function (fps) {
var encoder = new GIFEncoder(), dpi = 10;
encoder.setRepeat(0);
2013-06-13 15:56:44 +00:00
encoder.setDelay(1000/fps);
2013-06-13 16:00:16 +00:00
2013-06-13 15:56:44 +00:00
encoder.start();
encoder.setSize(this.framesheet.getWidth() * dpi, this.framesheet.getHeight() * dpi);
for (var i = 0 ; i < this.framesheet.frames.length ; i++) {
var frame = this.framesheet.frames[i];
var renderer = new pskl.rendering.CanvasRenderer(frame, dpi);
encoder.addFrame(renderer.render());
}
2013-06-13 16:00:16 +00:00
encoder.finish();
2013-06-13 15:56:44 +00:00
var imageData = 'data:image/gif;base64,' + encode64(encoder.stream().getData());
return imageData;
};
2012-09-19 23:10:13 +00:00
/**
2013-05-27 21:42:53 +00:00
* TODO(juliandescottes): Mutualize with code already present in FrameRenderer
2012-09-19 23:10:13 +00:00
*/
ns.SpritesheetRenderer.prototype.drawFrameInCanvas_ = function (frame, canvas, offsetWidth, offsetHeight) {
var context = canvas.getContext('2d');
for(var col = 0, width = frame.getWidth(); col < width; col++) {
for(var row = 0, height = frame.getHeight(); row < height; row++) {
var color = frame.getPixel(col, row);
if(color != Constants.TRANSPARENT_COLOR) {
context.fillStyle = color;
context.fillRect(col + offsetWidth, row + offsetHeight, 1, 1);
}
}
}
2012-09-19 23:10:13 +00:00
};
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 {
2013-05-27 21:42:53 +00:00
throw "Cannot render empty Spritesheet";
2012-09-19 23:10:13 +00:00
}
};
})();