pure-canvas
Advanced tools
Comparing version 0.5.2 to 0.5.3
@@ -76,2 +76,29 @@ "use strict"; | ||
}; | ||
Stage.prototype.renderAsynchronous = function (index, maxBatchTime) { | ||
var _this = this; | ||
if (index === void 0) { index = true; } | ||
if (maxBatchTime === void 0) { maxBatchTime = 10; } | ||
var _a = this.renderDeferred(), steps = _a.steps, commit = _a.commit; | ||
if (this.nonBlockingTimeoutId) { | ||
window.clearTimeout(this.nonBlockingTimeoutId); | ||
} | ||
var i = 0; | ||
var repeat = function () { | ||
_this.nonBlockingTimeoutId = window.setTimeout(batch, 0); | ||
}; | ||
var batch = function () { | ||
var deadline = Date.now() + maxBatchTime; | ||
while (i < steps.length && Date.now() < deadline) { | ||
steps[i++](); | ||
} | ||
commit(false); | ||
if (i < steps.length) { | ||
repeat(); | ||
} | ||
else { | ||
commit(index); | ||
} | ||
}; | ||
repeat(); | ||
}; | ||
Stage.prototype.renderDeferred = function () { | ||
@@ -78,0 +105,0 @@ var _this = this; |
{ | ||
"name": "pure-canvas", | ||
"version": "0.5.2", | ||
"version": "0.5.3", | ||
"description": "TODO", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -17,2 +17,4 @@ import {EventEmitter} from 'eventemitter3'; | ||
private nonBlockingTimeoutId?: number; | ||
constructor(canvas: HTMLCanvasElement) { | ||
@@ -75,2 +77,3 @@ super(); | ||
const {steps, commit} = this.renderDeferred(); | ||
for (const step of steps) { | ||
@@ -80,2 +83,3 @@ step(); | ||
commit(index); | ||
// this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); | ||
@@ -86,2 +90,28 @@ // this.internalLayer.draw(this.context); | ||
renderAsynchronous(index: boolean = true, maxBatchTime: number = 10): void { | ||
const {steps, commit} = this.renderDeferred(); | ||
if (this.nonBlockingTimeoutId) { | ||
window.clearTimeout(this.nonBlockingTimeoutId); | ||
} | ||
let i = 0; | ||
const repeat = () => { | ||
this.nonBlockingTimeoutId = window.setTimeout(batch, 0); | ||
}; | ||
const batch = () => { | ||
const deadline = Date.now() + maxBatchTime; | ||
while (i < steps.length && Date.now() < deadline) { | ||
steps[i++](); | ||
} | ||
commit(false); | ||
if (i < steps.length) { | ||
repeat(); | ||
} else { | ||
commit(index); | ||
} | ||
}; | ||
repeat(); | ||
} | ||
renderDeferred(): {steps: Array<() => void>, commit: (index: boolean) => void} { | ||
@@ -88,0 +118,0 @@ const stepAccumulator: Array<() => void> = []; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
209140
2654