From 09bfaab9f22cef08f49946a688b87e7c26e09e52 Mon Sep 17 00:00:00 2001 From: VladimirTechMan Date: Sat, 2 Feb 2019 20:17:27 +0300 Subject: [PATCH] Fix for the performance regression in the updated batchDraw() The code was checking the "waiting" flags, but they were not set. This patch corrects that part and makes a few other tweaks to the updated logic on top of requestAnimationFrame(). --- src/BaseLayer.ts | 12 ++++++------ src/Util.ts | 16 ++++++---------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/BaseLayer.ts b/src/BaseLayer.ts index 3d44f450..74abc148 100644 --- a/src/BaseLayer.ts +++ b/src/BaseLayer.ts @@ -249,13 +249,13 @@ export abstract class BaseLayer extends Container { * @return {Konva.Layer} this */ batchDraw() { - if (this._waitingForDraw) { - return; + if (!this._waitingForDraw) { + this._waitingForDraw = true; + Util.requestAnimFrame(() => { + this.draw(); + this._waitingForDraw = false; + }); } - Util.requestAnimFrame(() => { - this._waitingForDraw = false; - this.draw(); - }); return this; } diff --git a/src/Util.ts b/src/Util.ts index 07164bdf..e51b3411 100644 --- a/src/Util.ts +++ b/src/Util.ts @@ -542,20 +542,16 @@ export const Util = { return -1; } }, - _waiting: false, animQueue: [], requestAnimFrame(callback) { Util.animQueue.push(callback); - if (Util._waiting) { - return; - } - requestAnimationFrame(() => { - Util.animQueue.forEach(cb => { - cb(); + if (Util.animQueue.length === 1) { + requestAnimationFrame(function () { + const queue = Util.animQueue; + Util.animQueue = []; + queue.forEach(function (cb) { cb(); }); }); - Util.animQueue = []; - Util._waiting = false; - }); + } }, createCanvasElement() { var canvas = isBrowser