maybe slight performance improvement

This commit is contained in:
Zutatensuppe 2020-11-08 12:56:54 +01:00
parent 36e5f8437d
commit 1f3eb41b4e
4 changed files with 408 additions and 344 deletions

View file

@ -3,6 +3,7 @@ import BoundingRectangle from './BoundingRectangle.js'
export default class CanvasAdapter {
constructor(canvas) {
this._canvas = canvas
/** @type {CanvasRenderingContext2D} */
this._ctx = this._canvas.getContext('2d')
this._w = this._canvas.width
this._h = this._canvas.height
@ -11,6 +12,8 @@ export default class CanvasAdapter {
this._imageData = this._ctx.createImageData(this._w, this._h)
this._data = this._imageData.data
this._dirty = false
this._dirtyRect = {x0: 0, x1: 0, y0: 0, y1: 0}
this.width = this._w
this.height = this._h
}
@ -18,15 +21,16 @@ export default class CanvasAdapter {
clear() {
this._imageData = this._ctx.createImageData(this._w, this._h)
this._data = this._imageData.data
this.apply()
this._dirty = false
}
clearRect(rect) {
for (let x = rect.x0; x< rect.x1; x++) {
for (let y = rect.y0; y< rect.y1; y++) {
this.putPix(x, y, [0,0,0,0])
clearRect(rects) {
for (let rect of rects) {
for (let x = rect.x0; x< rect.x1; x++) {
for (let y = rect.y0; y< rect.y1; y++) {
this.putPix(x, y, [0,0,0,0])
}
}
}
this.apply()
}
getPix(x, y, out) {
@ -47,6 +51,7 @@ export default class CanvasAdapter {
if (x < 0 || y < 0 || x >= this._w || y >= this._h) {
return null;
}
x = Math.round(x)
y = Math.round(y)
const idx = (y * 4 * this._w) + (x * 4)
@ -54,6 +59,21 @@ export default class CanvasAdapter {
this._data[idx + 1] = rgba[1]
this._data[idx + 2] = rgba[2]
this._data[idx + 3] = rgba[3]
if (this._dirty) {
// merge
this._dirtyRect.x0 = Math.min(this._dirtyRect.x0, x)
this._dirtyRect.x1 = Math.max(this._dirtyRect.x1, x)
this._dirtyRect.y0 = Math.min(this._dirtyRect.y0, y)
this._dirtyRect.y1 = Math.max(this._dirtyRect.y1, y)
} else {
// set
this._dirty = true
this._dirtyRect.x0 = x
this._dirtyRect.x1 = x
this._dirtyRect.y0 = y
this._dirtyRect.y1 = y
}
}
getBoundingRect() {
@ -61,6 +81,19 @@ export default class CanvasAdapter {
}
apply() {
this._ctx.putImageData(this._imageData, 0, 0)
if (this._dirty) {
this._ctx.putImageData(
this._imageData,
0,
0,
this._dirtyRect.x0,
this._dirtyRect.y0,
this._dirtyRect.x1 - this._dirtyRect.x0,
this._dirtyRect.y1 - this._dirtyRect.y0
)
this._dirty = null
} else {
this._ctx.putImageData(this._imageData, 0, 0)
}
}
}