Temp commit

This commit is contained in:
jdescottes
2014-04-17 01:27:49 +02:00
parent b712a497e2
commit 0cecdc74eb
19 changed files with 423 additions and 112 deletions

View File

@@ -31,37 +31,49 @@
this.layersToLoad_ = piskelData.layers.length;
piskelData.layers.forEach(function (serializedLayer) {
var layer = this.deserializeLayer(serializedLayer);
this.piskel_.addLayer(layer);
this.deserializeLayer(serializedLayer);
}.bind(this));
};
ns.Deserializer.prototype.deserializeLayer = function (layerString) {
var layerData = JSON.parse(layerString);
var layerData = typeof layerString === "string" ? JSON.parse(layerString) : layerString;
var layer = new pskl.model.Layer(layerData.name);
// 1 - create an image to load the base64PNG representing the layer
var base64PNG = layerData.base64PNG;
var image = new Image();
var isCompressedLayer = !!layerData.base64PNG;
// 2 - attach the onload callback that will be triggered asynchronously
image.onload = function () {
// 5 - extract the frames from the loaded image
var frames = pskl.utils.LayerUtils.createFromImage(image, layerData.frameCount);
if (isCompressedLayer) {
// 1 - create an image to load the base64PNG representing the layer
var base64PNG = layerData.base64PNG;
var image = new Image();
// 6 - add each image to the layer
frames.forEach(layer.addFrame.bind(layer));
// 2 - attach the onload callback that will be triggered asynchronously
image.onload = function () {
// 5 - extract the frames from the loaded image
var frames = pskl.utils.LayerUtils.createFromImage(image, layerData.frameCount);
// 6 - add each image to the layer
this.addFramesToLayer(frames, layer);
}.bind(this);
this.onLayerLoaded_();
}.bind(this);
// 3 - set the source of the image
image.src = base64PNG;
// 3 - set the source of the image
image.src = base64PNG;
} else {
var frames = layerData.grids.map(function (grid) {
return pskl.model.Frame.fromPixelGrid(grid);
});
this.addFramesToLayer(frames, layer);
}
// 4 - return a pointer to the new layer instance
return layer;
};
ns.Deserializer.prototype.addFramesToLayer = function (frames, layer) {
frames.forEach(layer.addFrame.bind(layer));
this.piskel_.addLayer(layer);
this.onLayerLoaded_();
};
ns.Deserializer.prototype.onLayerLoaded_ = function () {
this.layersToLoad_ = this.layersToLoad_ - 1;
if (this.layersToLoad_ === 0) {

View File

@@ -2,9 +2,9 @@
var ns = $.namespace('pskl.utils');
ns.Serializer = {
serializePiskel : function (piskel) {
serializePiskel : function (piskel, compressed) {
var serializedLayers = piskel.getLayers().map(function (l) {
return pskl.utils.Serializer.serializeLayer(l);
return pskl.utils.Serializer.serializeLayer(l, compressed);
});
return JSON.stringify({
modelVersion : Constants.MODEL_VERSION,
@@ -16,16 +16,23 @@
});
},
serializeLayer : function (layer) {
serializeLayer : function (layer, compressed) {
if (compressed !== false) {
compressed = true;
}
var frames = layer.getFrames();
var renderer = new pskl.rendering.FramesheetRenderer(frames);
var base64PNG = renderer.renderAsCanvas().toDataURL();
return JSON.stringify({
var layerToSerialize = {
name : layer.getName(),
base64PNG : base64PNG,
frameCount : frames.length
});
};
if (compressed) {
layerToSerialize.base64PNG = renderer.renderAsCanvas().toDataURL();
return JSON.stringify(layerToSerialize);
} else {
layerToSerialize.grids = frames.map(function (f) {return f.pixels;});
return layerToSerialize;
}
}
};
})();