canvas: big canvas perf improvement on chrome v69 and above using offscreen canvas

This commit is contained in:
jean-emmanuel 2019-05-14 23:12:56 +02:00
parent 425fd1282c
commit 557a667ee6
2 changed files with 38 additions and 10 deletions

View file

@ -1,5 +1,9 @@
# Changelog
## next
- improved canvas rendering speed on chrome v69 and above
## 0.46.1
- bug fixes

View file

@ -66,7 +66,16 @@ class Canvas extends Widget {
super(options)
this.canvas = DOM.get(this.widget, 'canvas')[0]
this.ctx = this.canvas.getContext('2d')
this.offscreen = this.canvas.transferControlToOffscreen ?
this.canvas.transferControlToOffscreen() : false
this.ctx = this.offscreen ? this.offscreen.getContext('2d',{
singleBuffered: true,
lowLatency: true,
desynchronized: true,
alpha: true
}) : this.canvas.getContext('2d')
this.height = undefined
this.width = undefined
@ -82,10 +91,12 @@ class Canvas extends Widget {
this.ctx.arc = (x, y, r, s, e, c)=>{
CanvasRenderingContext2D.prototype.arc.call(this.ctx, x, y, Math.max(0,r), s, e, c)
this.ctx.__proto__.arc.call(this.ctx, x, y, Math.max(0,r), s, e, c)
}
if (this.offscreen) this.ctx.curve = CanvasRenderingContext2D.prototype.curve.bind(this.ctx)
}
resizeHandleProxy() {
@ -105,6 +116,11 @@ class Canvas extends Widget {
this.canvas.setAttribute('width', width * ratio)
this.canvas.setAttribute('height', height * ratio)
if (this.offscreen) {
this.offscreen.width = width * ratio
this.offscreen.height = height * ratio
}
this.clearRect = []
if (ratio != 1) {
@ -147,24 +163,32 @@ class Canvas extends Widget {
clear() {
if (!this.clearRect.length) {
this.ctx.clearRect(0, 0, this.width, this.height)
return
}
if (typeof this.clearRect[0] == 'object') {
this.ctx.clearRect(0, 0, this.width, this.height)
} else if (typeof this.clearRect[0] == 'object') {
for (let i in this.clearRect) {
this.ctx.clearRect(...this.clearRect[i])
}
return
}
this.ctx.clearRect(...this.clearRect)
} else {
this.ctx.clearRect(...this.clearRect)
}
}
batchDraw() {
if (this.visible) canvasQueue.push(this)
if (this.visible) {
if (this.offscreen) {
this.draw()
} else {
canvasQueue.push(this)
}
}
}