split saved piskel in chunks when serialization fails

This commit is contained in:
juliandescottes 2016-12-21 02:28:11 +01:00
parent 66c941dd25
commit 84419a1550
2 changed files with 44 additions and 4 deletions

View File

@ -221,8 +221,9 @@
var row = layout[i]; var row = layout[i];
for (var j = 0 ; j < row.length ; j++) { for (var j = 0 ; j < row.length ; j++) {
context.clearRect(0, 0 , frameWidth, frameHeight); context.clearRect(0, 0 , frameWidth, frameHeight);
context.drawImage(image, frameWidth * i, frameHeight * j, frameWidth, frameHeight, 0, 0, frameWidth, height); context.drawImage(image, frameWidth * i, frameHeight * j,
var frame = pskl.utils.FrameUtils.createFromCanvas(canvas, 0, 0, frameWidth, height); frameWidth, frameHeight, 0, 0, frameWidth, frameHeight);
var frame = pskl.utils.FrameUtils.createFromCanvas(canvas, 0, 0, frameWidth, frameHeight);
chunkFrames.push({ chunkFrames.push({
index : layout[i][j], index : layout[i][j],
frame : frame frame : frame

View File

@ -1,6 +1,21 @@
(function () { (function () {
var ns = $.namespace('pskl.utils.serialization'); var ns = $.namespace('pskl.utils.serialization');
var areChunksValid = function (chunks) {
return chunks.every(function (chunk) {
return chunk.base64PNG;
});
};
var createColumnLayout = function (size, offset) {
var layout = [[]];
for (var i = 0 ; i < size ; i++) {
layout[0].push(i + offset);
}
return layout;
};
ns.Serializer = { ns.Serializer = {
serialize : function (piskel) { serialize : function (piskel) {
var serializedLayers = piskel.getLayers().map(function (l) { var serializedLayers = piskel.getLayers().map(function (l) {
@ -26,8 +41,32 @@
opacity : layer.getOpacity(), opacity : layer.getOpacity(),
frameCount : frames.length frameCount : frames.length
}; };
var renderer = new pskl.rendering.FramesheetRenderer(frames);
layerToSerialize.base64PNG = renderer.renderAsCanvas().toDataURL(); var chunks = [];
while (!areChunksValid(chunks)) {
// Chunks are invalid, increase the number of chunks by one, and chunk the frames array.
var frameChunks = pskl.utils.Array.chunk(frames, chunks.length + 1);
// Reset chunks array.
chunks = [];
// After each chunk update the offset by te number of frames that have been processed.
var offset = 0;
for (var i = 0 ; i < frameChunks.length ; i++) {
var chunkFrames = frameChunks[i];
var renderer = new pskl.rendering.FramesheetRenderer(chunkFrames);
chunks.push({
// renderAsCanvas with 1 column
base64PNG : renderer.renderAsCanvas(1).toDataURL(),
// create a layout array, containing the indices of the frames extracted in this chunk
layout : createColumnLayout(chunkFrames.length, offset),
});
offset += chunkFrames.length;
}
}
layerToSerialize.chunks = chunks;
return JSON.stringify(layerToSerialize); return JSON.stringify(layerToSerialize);
} }
}; };