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)
86 lines
2.7 KiB
JavaScript
86 lines
2.7 KiB
JavaScript
(function () {
|
|
var ns = $.namespace('pskl.utils');
|
|
|
|
ns.FrameUtils = {
|
|
merge : function (frames) {
|
|
var merged = null;
|
|
if (frames.length) {
|
|
merged = frames[0].clone();
|
|
var w = merged.getWidth(), h = merged.getHeight();
|
|
for (var i = 1 ; i < frames.length ; i++) {
|
|
pskl.utils.FrameUtils.mergeFrames_(merged, frames[i]);
|
|
}
|
|
}
|
|
return merged;
|
|
},
|
|
|
|
mergeFrames_ : function (frameA, frameB) {
|
|
frameB.forEachPixel(function (p, col, row) {
|
|
if (p != Constants.TRANSPARENT_COLOR) {
|
|
frameA.setPixel(col, row, p);
|
|
}
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Create a pskl.model.Frame from an Image object.
|
|
* Transparent pixels will either be converted to completely opaque or completely transparent pixels.
|
|
* @param {Image} image source image
|
|
* @return {pskl.model.Frame} corresponding frame
|
|
*/
|
|
createFromImage : function (image) {
|
|
var w = image.width,
|
|
h = image.height;
|
|
var canvas = pskl.CanvasUtils.createCanvas(w, h);
|
|
var context = canvas.getContext('2d');
|
|
|
|
context.drawImage(image, 0,0,w,h,0,0,w,h);
|
|
var imgData = context.getImageData(0,0,w,h).data;
|
|
return pskl.utils.FrameUtils.createFromImageData(imgData);
|
|
},
|
|
|
|
createFromImageData : function (imageData, width, height) {
|
|
// Draw the zoomed-up pixels to a different canvas context
|
|
var frame = [];
|
|
for (var x = 0 ; x < width ; x++){
|
|
frame[x] = [];
|
|
for (var y = 0 ; y < height ; y++){
|
|
// Find the starting index in the one-dimensional image data
|
|
var i = (y * width + x)*4;
|
|
var r = imageData[i ];
|
|
var g = imageData[i+1];
|
|
var b = imageData[i+2];
|
|
var a = imageData[i+3];
|
|
if (a < 125) {
|
|
frame[x][y] = Constants.TRANSPARENT_COLOR;
|
|
} else {
|
|
frame[x][y] = pskl.utils.FrameUtils.rgbToHex(r,g,b);
|
|
}
|
|
}
|
|
}
|
|
return frame;
|
|
},
|
|
|
|
/**
|
|
* Convert a rgb(Number, Number, Number) color to hexadecimal representation
|
|
* @param {Number} r red value, between 0 and 255
|
|
* @param {Number} g green value, between 0 and 255
|
|
* @param {Number} b blue value, between 0 and 255
|
|
* @return {String} hex representation of the color '#ABCDEF'
|
|
*/
|
|
rgbToHex : function (r, g, b) {
|
|
return "#" + this.componentToHex(r) + this.componentToHex(g) + this.componentToHex(b);
|
|
},
|
|
|
|
/**
|
|
* Convert a color component (as a Number between 0 and 255) to its string hexa representation
|
|
* @param {Number} c component value, between 0 and 255
|
|
* @return {String} eg. '0A'
|
|
*/
|
|
componentToHex : function (c) {
|
|
var hex = c.toString(16);
|
|
return hex.length == 1 ? "0" + hex : hex;
|
|
}
|
|
};
|
|
})();
|