2013-04-07 22:16:47 +04:00
|
|
|
(function () {
|
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
var ns = $.namespace("pskl.rendering");
|
2013-11-06 01:11:47 +04:00
|
|
|
ns.CanvasRenderer = function (frame, zoom) {
|
2013-08-10 14:11:16 +04:00
|
|
|
this.frame = frame;
|
2013-11-06 01:11:47 +04:00
|
|
|
this.zoom = zoom;
|
2013-10-09 01:44:06 +04:00
|
|
|
this.transparentColor_ = 'white';
|
2013-08-10 14:11:16 +04:00
|
|
|
};
|
2013-04-07 22:16:47 +04:00
|
|
|
|
2013-10-09 01:44:06 +04:00
|
|
|
/**
|
|
|
|
* Decide which color should be used to represent transparent pixels
|
|
|
|
* Default : white
|
|
|
|
* @param {String} color the color to use either as '#ABCDEF' or 'red' or 'rgb(x,y,z)' or 'rgba(x,y,z,a)'
|
|
|
|
*/
|
|
|
|
ns.CanvasRenderer.prototype.drawTransparentAs = function (color) {
|
|
|
|
this.transparentColor_ = color;
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.CanvasRenderer.prototype.render = function () {
|
2013-08-10 14:11:16 +04:00
|
|
|
var canvas = this.createCanvas_();
|
|
|
|
var context = canvas.getContext('2d');
|
2013-04-07 22:16:47 +04:00
|
|
|
|
fix : reduce piskel model size
- 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)
2013-11-08 03:44:24 +04:00
|
|
|
this.frame.forEachPixel(function (color, x, y) {
|
|
|
|
this.renderPixel_(color, x, y, context);
|
|
|
|
}.bind(this));
|
2013-04-07 22:16:47 +04:00
|
|
|
|
fix : reduce piskel model size
- 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)
2013-11-08 03:44:24 +04:00
|
|
|
return canvas;
|
|
|
|
};
|
2013-10-09 01:44:06 +04:00
|
|
|
|
fix : reduce piskel model size
- 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)
2013-11-08 03:44:24 +04:00
|
|
|
ns.CanvasRenderer.prototype.renderPixel_ = function (color, x, y, context) {
|
2013-08-10 14:11:16 +04:00
|
|
|
if(color == Constants.TRANSPARENT_COLOR) {
|
2013-10-09 01:44:06 +04:00
|
|
|
color = this.transparentColor_;
|
2013-08-10 14:11:16 +04:00
|
|
|
}
|
|
|
|
context.fillStyle = color;
|
2013-11-23 22:25:51 +04:00
|
|
|
context.fillRect(x * this.zoom, y * this.zoom, this.zoom, this.zoom);
|
2013-08-10 14:11:16 +04:00
|
|
|
};
|
2013-04-07 22:16:47 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
ns.CanvasRenderer.prototype.createCanvas_ = function () {
|
2013-11-06 01:11:47 +04:00
|
|
|
var width = this.frame.getWidth() * this.zoom;
|
|
|
|
var height = this.frame.getHeight() * this.zoom;
|
2013-08-10 14:11:16 +04:00
|
|
|
return pskl.CanvasUtils.createCanvas(width, height);
|
|
|
|
};
|
2013-05-28 01:42:53 +04:00
|
|
|
})();
|