Animation pauses when FPS slider at 0FPS + slight perf improvement

This commit is contained in:
jdescottes 2014-12-18 23:57:34 +01:00
parent e1ba57c92f
commit 1402394d07
7 changed files with 72 additions and 31 deletions

View File

@ -5,6 +5,7 @@ var Events = {
SELECT_TOOL : "SELECT_TOOL", SELECT_TOOL : "SELECT_TOOL",
TOOL_RELEASED : "TOOL_RELEASED", TOOL_RELEASED : "TOOL_RELEASED",
TOOL_PRESSED : "TOOL_PRESSED",
SELECT_PRIMARY_COLOR: "SELECT_PRIMARY_COLOR", SELECT_PRIMARY_COLOR: "SELECT_PRIMARY_COLOR",
SELECT_SECONDARY_COLOR: "SELECT_SECONDARY_COLOR", SELECT_SECONDARY_COLOR: "SELECT_SECONDARY_COLOR",
PRIMARY_COLOR_SELECTED : 'PRIMARY_COLOR_SELECTED', PRIMARY_COLOR_SELECTED : 'PRIMARY_COLOR_SELECTED',

View File

@ -1,5 +1,5 @@
(function () { (function () {
var ns = $.namespace("pskl.controller"); var ns = $.namespace('pskl.controller');
// Preview is a square of PREVIEW_SIZE x PREVIEW_SIZE // Preview is a square of PREVIEW_SIZE x PREVIEW_SIZE
var PREVIEW_SIZE = 200; var PREVIEW_SIZE = 200;
@ -11,6 +11,11 @@
this.elapsedTime = 0; this.elapsedTime = 0;
this.currentIndex = 0; this.currentIndex = 0;
this.renderFlag = true;
this.fpsRangeInput = $('#preview-fps');
this.fpsCounterDisplay = $('#display-fps');
this.setFPS(Constants.DEFAULT.FPS); this.setFPS(Constants.DEFAULT.FPS);
var frame = this.piskelController.getCurrentFrame(); var frame = this.piskelController.getCurrentFrame();
@ -22,10 +27,10 @@
// the oninput event won't work on IE10 unfortunately, but at least will provide a // the oninput event won't work on IE10 unfortunately, but at least will provide a
// consistent behavior across all other browsers that support the input type range // consistent behavior across all other browsers that support the input type range
// see https://bugzilla.mozilla.org/show_bug.cgi?id=853670 // see https://bugzilla.mozilla.org/show_bug.cgi?id=853670
$("#preview-fps").on('input change', this.onFPSSliderChange.bind(this)); this.fpsRangeInput.on('input change', this.onFPSSliderChange.bind(this));
document.querySelector(".right-column").style.width = Constants.ANIMATED_PREVIEW_WIDTH + 'px'; document.querySelector('.right-column').style.width = Constants.ANIMATED_PREVIEW_WIDTH + 'px';
this.toggleOnionSkinEl = document.querySelector(".preview-toggle-onion-skin"); this.toggleOnionSkinEl = document.querySelector('.preview-toggle-onion-skin');
this.toggleOnionSkinEl.addEventListener('click', this.toggleOnionSkin_.bind(this)); this.toggleOnionSkinEl.addEventListener('click', this.toggleOnionSkin_.bind(this));
pskl.app.shortcutService.addShortcut('alt+O', this.toggleOnionSkin_.bind(this)); pskl.app.shortcutService.addShortcut('alt+O', this.toggleOnionSkin_.bind(this));
@ -33,6 +38,9 @@
$.subscribe(Events.FRAME_SIZE_CHANGED, this.onFrameSizeChange_.bind(this)); $.subscribe(Events.FRAME_SIZE_CHANGED, this.onFrameSizeChange_.bind(this));
$.subscribe(Events.USER_SETTINGS_CHANGED, $.proxy(this.onUserSettingsChange_, this)); $.subscribe(Events.USER_SETTINGS_CHANGED, $.proxy(this.onUserSettingsChange_, this));
$.subscribe(Events.TOOL_RELEASED, this.setRenderFlag_.bind(this, true));
$.subscribe(Events.TOOL_PRESSED, this.setRenderFlag_.bind(this, false));
this.updateZoom_(); this.updateZoom_();
this.updateOnionSkinPreview_(); this.updateOnionSkinPreview_();
this.updateContainerDimensions_(); this.updateContainerDimensions_();
@ -78,14 +86,16 @@
}; };
ns.AnimatedPreviewController.prototype.onFPSSliderChange = function (evt) { ns.AnimatedPreviewController.prototype.onFPSSliderChange = function (evt) {
this.setFPS(parseInt($("#preview-fps")[0].value, 10)); this.setFPS(parseInt(this.fpsRangeInput[0].value, 10));
}; };
ns.AnimatedPreviewController.prototype.setFPS = function (fps) { ns.AnimatedPreviewController.prototype.setFPS = function (fps) {
if (fps) { if (typeof fps === 'number') {
this.fps = fps; this.fps = fps;
$("#preview-fps").val(this.fps); this.fpsRangeInput.val(this.fps);
$("#display-fps").html(this.fps + " FPS"); this.fpsRangeInput.blur();
this.fpsCounterDisplay.html(this.fps + ' FPS');
} }
}; };
@ -94,7 +104,24 @@
}; };
ns.AnimatedPreviewController.prototype.render = function (delta) { ns.AnimatedPreviewController.prototype.render = function (delta) {
if (this.renderFlag) {
this.elapsedTime += delta; this.elapsedTime += delta;
if (this.fps === 0) {
this._renderSelectedFrame();
} else {
this._renderCurrentAnimationFrame();
}
}
};
ns.AnimatedPreviewController.prototype._renderSelectedFrame = function (delta) {
// the selected frame is the currentFrame from the PiskelController perspective
var selectedFrameIndex = this.piskelController.getCurrentFrameIndex();
var selectedFrame = this.piskelController.getFrameAt(selectedFrameIndex);
this.renderer.render(selectedFrame);
};
ns.AnimatedPreviewController.prototype._renderCurrentAnimationFrame = function (delta) {
var index = Math.floor(this.elapsedTime / (1000/this.fps)); var index = Math.floor(this.elapsedTime / (1000/this.fps));
if (index != this.currentIndex) { if (index != this.currentIndex) {
this.currentIndex = index; this.currentIndex = index;
@ -112,9 +139,8 @@
*/ */
ns.AnimatedPreviewController.prototype.calculateZoom_ = function () { ns.AnimatedPreviewController.prototype.calculateZoom_ = function () {
var frame = this.piskelController.getCurrentFrame(); var frame = this.piskelController.getCurrentFrame();
var previewSize = 200, var hZoom = PREVIEW_SIZE / frame.getHeight(),
hZoom = previewSize / frame.getHeight(), wZoom = PREVIEW_SIZE / frame.getWidth();
wZoom = previewSize / frame.getWidth();
return Math.min(hZoom, wZoom); return Math.min(hZoom, wZoom);
}; };
@ -139,16 +165,21 @@
width = frame.getWidth() * zoom; width = frame.getWidth() * zoom;
} }
containerEl.style.height = height + "px"; containerEl.style.height = height + 'px';
containerEl.style.width = width + "px"; containerEl.style.width = width + 'px';
var horizontalPadding = (PREVIEW_SIZE - height) / 2; var horizontalPadding = (PREVIEW_SIZE - height) / 2;
containerEl.style.marginTop = horizontalPadding + "px"; containerEl.style.marginTop = horizontalPadding + 'px';
containerEl.style.marginBottom = horizontalPadding + "px"; containerEl.style.marginBottom = horizontalPadding + 'px';
var verticalPadding = (PREVIEW_SIZE - width) / 2; var verticalPadding = (PREVIEW_SIZE - width) / 2;
containerEl.style.marginLeft = verticalPadding + "px"; containerEl.style.marginLeft = verticalPadding + 'px';
containerEl.style.marginRight = verticalPadding + "px"; containerEl.style.marginRight = verticalPadding + 'px';
};
ns.AnimatedPreviewController.prototype.setRenderFlag_ = function (bool) {
console.log('setRenderFlag_', bool);
this.renderFlag = bool;
}; };
ns.AnimatedPreviewController.prototype.toggleOnionSkin_ = function () { ns.AnimatedPreviewController.prototype.toggleOnionSkin_ = function () {

View File

@ -31,8 +31,6 @@
"yOffset" : 0 "yOffset" : 0
}; };
console.log('DrawingController:getContainerWidth_', this.getContainerWidth_());
this.overlayRenderer = new pskl.rendering.frame.CachedFrameRenderer(this.container, renderingOptions, ["canvas-overlay"]); this.overlayRenderer = new pskl.rendering.frame.CachedFrameRenderer(this.container, renderingOptions, ["canvas-overlay"]);
this.renderer = new pskl.rendering.frame.CachedFrameRenderer(this.container, renderingOptions, ["drawing-canvas"]); this.renderer = new pskl.rendering.frame.CachedFrameRenderer(this.container, renderingOptions, ["drawing-canvas"]);
this.onionSkinRenderer = new pskl.rendering.OnionSkinRenderer(this.container, renderingOptions, piskelController); this.onionSkinRenderer = new pskl.rendering.OnionSkinRenderer(this.container, renderingOptions, piskelController);
@ -153,7 +151,7 @@
this.dragHandler.startDrag(event.clientX, event.clientY); this.dragHandler.startDrag(event.clientX, event.clientY);
} else { } else {
this.currentToolBehavior.hideHighlightedPixel(this.overlayFrame); this.currentToolBehavior.hideHighlightedPixel(this.overlayFrame);
$.publish(Events.TOOL_PRESSED);
this.currentToolBehavior.applyToolAt( this.currentToolBehavior.applyToolAt(
coords.x, coords.x,
coords.y, coords.y,

View File

@ -65,7 +65,7 @@
if (cache[cacheKey]) { if (cache[cacheKey]) {
processedFrame = cache[cacheKey]; processedFrame = cache[cacheKey];
} else { } else {
var frameAsString = JSON.stringify(frame.getPixels()); var frameAsString = pskl.utils.hashCode(JSON.stringify(frame.getPixels()));
if (cache[frameAsString]) { if (cache[frameAsString]) {
processedFrame = this.outputCloner(cache[frameAsString], frame); processedFrame = this.outputCloner(cache[frameAsString], frame);
} else { } else {

View File

@ -164,13 +164,6 @@
} }
}; };
ns.FrameRenderer.prototype.renderPixel_ = function (color, x, y, context) {
if(color != Constants.TRANSPARENT_COLOR) {
context.fillStyle = color;
context.fillRect(x, y, 1, 1);
}
};
/** /**
* Transform a screen pixel-based coordinate (relative to the top-left corner of the rendered * Transform a screen pixel-based coordinate (relative to the top-left corner of the rendered
* frame) into a sprite coordinate in column and row. * frame) into a sprite coordinate in column and row.
@ -264,4 +257,11 @@
} }
displayContext.restore(); displayContext.restore();
}; };
ns.FrameRenderer.prototype.renderPixel_ = function (color, x, y, context) {
if(color != Constants.TRANSPARENT_COLOR) {
context.fillStyle = color;
context.fillRect(x, y, 1, 1);
}
};
})(); })();

View File

@ -89,6 +89,18 @@ if (!Function.prototype.bind) {
} }
}; };
ns.hashCode = function(str) {
var hash = 0;
if (str.length !== 0) {
for (var i = 0, l = str.length; i < l; i++) {
var chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
}
return hash;
};
var entityMap = { var entityMap = {
"&": "&amp;", "&": "&amp;",
"<": "&lt;", "<": "&lt;",
@ -97,7 +109,6 @@ if (!Function.prototype.bind) {
"'": '&#39;', "'": '&#39;',
"/": '&#x2F;' "/": '&#x2F;'
}; };
ns.escapeHtml= function (string) { ns.escapeHtml= function (string) {
return String(string).replace(/[&<>"'\/]/g, function (s) { return String(string).replace(/[&<>"'\/]/g, function (s) {
return entityMap[s]; return entityMap[s];

View File

@ -10,6 +10,6 @@
data-placement="bottom" data-placement="bottom"
class="piskel-icon-onion preview-toggle-onion-skin"></div> class="piskel-icon-onion preview-toggle-onion-skin"></div>
<span id="display-fps" class="display-fps"></span> <span id="display-fps" class="display-fps"></span>
<input id="preview-fps" class="range-fps" type="range" min="1" max="24"/> <input id="preview-fps" class="range-fps" type="range" min="0" max="24"/>
</div> </div>
</div> </div>