2013-08-12 09:31:09 +04:00
|
|
|
(function () {
|
|
|
|
var ns = $.namespace("pskl.controller.settings");
|
2013-10-01 00:00:31 +04:00
|
|
|
|
2013-09-22 23:02:43 +04:00
|
|
|
ns.GifExportController = function (piskelController) {
|
|
|
|
this.piskelController = piskelController;
|
2013-09-07 19:50:43 +04:00
|
|
|
};
|
|
|
|
|
2013-10-01 00:00:31 +04:00
|
|
|
/**
|
|
|
|
* List of Resolutions applicable for Gif export
|
|
|
|
* @static
|
2013-11-06 01:11:47 +04:00
|
|
|
* @type {Array} array of Objects {zoom:{Number}, default:{Boolean}}
|
2013-10-01 00:00:31 +04:00
|
|
|
*/
|
|
|
|
ns.GifExportController.RESOLUTIONS = [
|
|
|
|
{
|
2013-11-06 01:11:47 +04:00
|
|
|
'zoom' : 1
|
2013-10-01 00:00:31 +04:00
|
|
|
},{
|
2013-11-06 01:11:47 +04:00
|
|
|
'zoom' : 5
|
2013-10-01 00:00:31 +04:00
|
|
|
},{
|
2013-11-06 01:11:47 +04:00
|
|
|
'zoom' : 10,
|
2013-10-01 00:00:31 +04:00
|
|
|
'default' : true
|
|
|
|
},{
|
2013-11-06 01:11:47 +04:00
|
|
|
'zoom' : 20
|
2013-10-01 00:00:31 +04:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2013-09-07 19:50:43 +04:00
|
|
|
ns.GifExportController.prototype.init = function () {
|
2013-10-24 01:34:09 +04:00
|
|
|
this.radioTemplate_ = pskl.utils.Template.get("gif-export-radio-template");
|
2013-09-07 19:50:43 +04:00
|
|
|
|
2013-10-24 01:34:09 +04:00
|
|
|
this.previewContainerEl = document.querySelectorAll(".gif-export-preview")[0];
|
2013-09-26 00:43:21 +04:00
|
|
|
this.radioGroupEl = document.querySelectorAll(".gif-export-radio-group")[0];
|
|
|
|
|
2013-10-01 00:00:31 +04:00
|
|
|
this.uploadForm = $("[name=gif-export-upload-form]");
|
|
|
|
this.uploadForm.submit(this.onUploadFormSubmit_.bind(this));
|
2013-09-25 02:11:12 +04:00
|
|
|
|
2013-10-01 00:00:31 +04:00
|
|
|
this.createRadioElements_();
|
2013-09-07 19:50:43 +04:00
|
|
|
};
|
|
|
|
|
2013-10-01 00:00:31 +04:00
|
|
|
ns.GifExportController.prototype.onUploadFormSubmit_ = function (evt) {
|
2013-09-07 19:50:43 +04:00
|
|
|
evt.originalEvent.preventDefault();
|
2013-11-06 01:11:47 +04:00
|
|
|
var selectedZoom = this.getSelectedZoom_(),
|
2013-10-01 00:00:31 +04:00
|
|
|
fps = this.piskelController.getFPS(),
|
2013-11-06 01:11:47 +04:00
|
|
|
zoom = selectedZoom;
|
2013-09-07 19:50:43 +04:00
|
|
|
|
2013-11-06 01:11:47 +04:00
|
|
|
this.renderAsImageDataAnimatedGIF(zoom, fps, this.onGifRenderingCompleted_.bind(this));
|
2013-10-01 00:00:31 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
ns.GifExportController.prototype.onGifRenderingCompleted_ = function (imageData) {
|
|
|
|
this.updatePreview_(imageData);
|
|
|
|
this.previewContainerEl.classList.add("preview-upload-ongoing");
|
|
|
|
pskl.app.imageUploadService.upload(imageData, this.onImageUploadCompleted_.bind(this));
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.GifExportController.prototype.onImageUploadCompleted_ = function (imageUrl) {
|
|
|
|
this.updatePreview_(imageUrl);
|
|
|
|
this.previewContainerEl.classList.remove("preview-upload-ongoing");
|
2013-09-07 19:50:43 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
ns.GifExportController.prototype.updatePreview_ = function (src) {
|
2013-10-24 01:34:09 +04:00
|
|
|
this.previewContainerEl.innerHTML = "<div><img style='max-width:240px;' src='"+src+"'/></div>";
|
2013-09-07 19:50:43 +04:00
|
|
|
};
|
|
|
|
|
2013-11-06 01:11:47 +04:00
|
|
|
ns.GifExportController.prototype.getSelectedZoom_ = function () {
|
|
|
|
var radiosColl = this.uploadForm.get(0).querySelectorAll("[name=gif-zoom-level]"),
|
2013-09-07 19:50:43 +04:00
|
|
|
radios = Array.prototype.slice.call(radiosColl,0);
|
|
|
|
var selectedRadios = radios.filter(function(radio) {return !!radio.checked;});
|
|
|
|
|
|
|
|
if (selectedRadios.length == 1) {
|
|
|
|
return selectedRadios[0].value;
|
|
|
|
} else {
|
2013-11-06 01:11:47 +04:00
|
|
|
throw "Unexpected error when retrieving selected zoom";
|
2013-09-07 19:50:43 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-01 00:00:31 +04:00
|
|
|
ns.GifExportController.prototype.createRadioElements_ = function () {
|
|
|
|
var resolutions = ns.GifExportController.RESOLUTIONS;
|
|
|
|
for (var i = 0 ; i < resolutions.length ; i++) {
|
|
|
|
var radio = this.createRadioForResolution_(resolutions[i]);
|
2013-09-26 00:43:21 +04:00
|
|
|
this.radioGroupEl.appendChild(radio);
|
2013-09-07 19:50:43 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-01 00:00:31 +04:00
|
|
|
ns.GifExportController.prototype.createRadioForResolution_ = function (resolution) {
|
2013-11-06 01:11:47 +04:00
|
|
|
var zoom = resolution.zoom;
|
|
|
|
var label = zoom*this.piskelController.getWidth() + "x" + zoom*this.piskelController.getHeight();
|
|
|
|
var value = zoom;
|
2013-10-01 00:00:31 +04:00
|
|
|
|
2013-09-25 02:11:12 +04:00
|
|
|
var radioHTML = pskl.utils.Template.replace(this.radioTemplate_, {value : value, label : label});
|
2013-10-01 00:00:31 +04:00
|
|
|
var radioEl = pskl.utils.Template.createFromHTML(radioHTML);
|
2013-09-29 01:52:51 +04:00
|
|
|
|
2013-10-01 00:00:31 +04:00
|
|
|
if (resolution['default']) {
|
|
|
|
var input = radioEl.getElementsByTagName("input")[0];
|
|
|
|
input.setAttribute("checked", "checked");
|
2013-09-07 19:50:43 +04:00
|
|
|
}
|
2013-08-12 09:31:09 +04:00
|
|
|
|
2013-10-01 00:00:31 +04:00
|
|
|
return radioEl;
|
2013-08-12 09:31:09 +04:00
|
|
|
};
|
|
|
|
|
2013-09-07 19:50:43 +04:00
|
|
|
ns.GifExportController.prototype.blobToBase64_ = function(blob, cb) {
|
|
|
|
var reader = new FileReader();
|
|
|
|
reader.onload = function() {
|
|
|
|
var dataUrl = reader.result;
|
|
|
|
cb(dataUrl);
|
|
|
|
};
|
|
|
|
reader.readAsDataURL(blob);
|
|
|
|
};
|
|
|
|
|
2013-11-06 01:11:47 +04:00
|
|
|
ns.GifExportController.prototype.renderAsImageDataAnimatedGIF = function(zoom, fps, cb) {
|
2013-09-07 19:50:43 +04:00
|
|
|
var gif = new window.GIF({
|
|
|
|
workers: 2,
|
|
|
|
quality: 10,
|
2013-11-06 01:11:47 +04:00
|
|
|
width: this.piskelController.getWidth()*zoom,
|
|
|
|
height: this.piskelController.getHeight()*zoom
|
2013-09-07 19:50:43 +04:00
|
|
|
});
|
|
|
|
|
2013-09-22 23:02:43 +04:00
|
|
|
for (var i = 0; i < this.piskelController.getFrameCount(); i++) {
|
|
|
|
var frame = this.piskelController.getFrameAt(i);
|
2013-11-23 22:25:51 +04:00
|
|
|
var canvasRenderer = new pskl.rendering.CanvasRenderer(frame, zoom);
|
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
|
|
|
var canvas = canvasRenderer.render();
|
|
|
|
gif.addFrame(canvas.getContext('2d'), {
|
2013-09-07 19:50:43 +04:00
|
|
|
delay: 1000 / fps
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
gif.on('finished', function(blob) {
|
|
|
|
this.blobToBase64_(blob, cb);
|
|
|
|
}.bind(this));
|
|
|
|
|
|
|
|
gif.render();
|
|
|
|
};
|
2013-08-12 09:31:09 +04:00
|
|
|
})();
|