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