mirror of
https://github.com/piskelapp/piskel.git
synced 2023-08-10 21:12:52 +03:00
4f54715f70
- Initial implementation : working but ... - MODEL_VERSION has been bumped to 2 - The loading process is now theoretically asynchronous (loading images to read the content of the layers), but for now, the asynchronous behaviour is hidden behind a nasty hack, which is somehow similar to lazy loading. When loading the piskel, a Piskel is created synchronously, with fake empty frames, and as the images will get loaded, the fake frames will be replaced by the actual frames. I really don't like this, and the asynchronous nature of the loading should be clearly expressed - There is no backward compatible deserializer for the previous version of the model (1) - The Serializer utils is just badly designed. Serialization and deserialization should be splitted into two different classes - Saving & loading are still done in app.js and should be moved to services BUT : the size of the piskels is now pretty small. A piskel which was using 890kB previously is now using only 10kB. Although it should be noted, that after gzip there is no significant difference between this version and the existing one. The only gains we can really expect with this are : less disk space used on appengine, ability to reuse the layers' pngs directly on piskel-website (but to be honest I can't see any valid use case for this)
43 lines
1.3 KiB
JavaScript
43 lines
1.3 KiB
JavaScript
(function () {
|
|
var ns = $.namespace('pskl.rendering');
|
|
|
|
/**
|
|
* Render an array of frames
|
|
* @param {Array.<pskl.model.Frame>} frames
|
|
*/
|
|
ns.FramesheetRenderer = function (frames) {
|
|
if (frames.length > 0) {
|
|
this.frames = frames;
|
|
} else {
|
|
throw 'FramesheetRenderer : Invalid argument : frames is empty';
|
|
}
|
|
};
|
|
|
|
ns.FramesheetRenderer.prototype.renderAsCanvas = function () {
|
|
var canvas = this.createCanvas_();
|
|
for (var i = 0 ; i < this.frames.length ; i++) {
|
|
var frame = this.frames[i];
|
|
this.drawFrameInCanvas_(frame, canvas, i * frame.getWidth(), 0);
|
|
}
|
|
return canvas;
|
|
};
|
|
|
|
ns.FramesheetRenderer.prototype.drawFrameInCanvas_ = function (frame, canvas, offsetWidth, offsetHeight) {
|
|
var context = canvas.getContext('2d');
|
|
frame.forEachPixel(function (color, x, y) {
|
|
if(color != Constants.TRANSPARENT_COLOR) {
|
|
context.fillStyle = color;
|
|
context.fillRect(x + offsetWidth, y + offsetHeight, 1, 1);
|
|
}
|
|
});
|
|
};
|
|
|
|
ns.FramesheetRenderer.prototype.createCanvas_ = function () {
|
|
var sampleFrame = this.frames[0];
|
|
var count = this.frames.length;
|
|
var width = count * sampleFrame.getWidth();
|
|
var height = sampleFrame.getHeight();
|
|
return pskl.CanvasUtils.createCanvas(width, height);
|
|
};
|
|
|
|
})(); |