fader/xy's: optimize canvas clearing (store drawed rect coordinates)

This commit is contained in:
jean-emmanuel 2017-04-27 14:41:30 +02:00
parent ca5132f63d
commit b20a9716b5
5 changed files with 50 additions and 12 deletions

View file

@ -12,12 +12,13 @@ module.exports = class _canvas_base extends _widgets_base {
this.height = undefined
this.width = undefined
this.clearRect = []
this.visible = false
this.colors = {}
this.textColor = getComputedStyle(document.documentElement).getPropertyValue('--color-text-fade')
this.canvas.on('resize',this.resizeHandleProxy.bind(this))
this.ctx.arc = (x, y, r, s, e, c)=>{
@ -48,8 +49,10 @@ module.exports = class _canvas_base extends _widgets_base {
this.canvas[0].setAttribute('width',width * ratio)
this.canvas[0].setAttribute('height',height * ratio)
this.clearRect = []
if (ratio != 1) this.ctx.scale(ratio, ratio)
}
if (!this.visible || checkColors) {
@ -67,6 +70,24 @@ module.exports = class _canvas_base extends _widgets_base {
}
clear() {
if (!this.clearRect.length) {
this.ctx.clearRect(0, 0, this.width, this.height)
return
}
if (typeof this.clearRect[0] == 'object') {
for (i in this.clearRect) {
this.ctx.clearRect(...this.clearRect[i])
}
return
}
this.ctx.clearRect(...this.clearRect)
}
draw(){
throw 'Calling unimplemented draw() method'
}

View file

@ -176,7 +176,7 @@ module.exports = class MultiXy extends _pads_base {
draw() {
this.ctx.clearRect(0,0,this.width,this.height)
this.clear()
this.ctx.textAlign = 'center'
this.ctx.textBaseline = 'middle'
@ -208,6 +208,8 @@ module.exports = class MultiXy extends _pads_base {
this.ctx.fillText(t, x, y)
this.ctx.strokeText(t, x, y)
this.clearRect[i] = [x - this.pointSize * PXSCALE, y - this.pointSize * PXSCALE, this.pointSize * 2 * PXSCALE, this.pointSize * 2 * PXSCALE]
}
}

View file

@ -205,10 +205,10 @@ module.exports = class Rgb extends _pads_base {
draw() {
var x = clip(this.faders.s.percent / 100 * this.width,[0,this.width]),
y = clip((1 - this.faders.b.percent / 100) * this.height,[0,this.height]),
color = this.hsb.b > 70 && this.hsb.s < 30 ? '#555' : 'white'
y = clip((1 - this.faders.b.percent / 100) * this.height,[0,this.height]),
color = this.hsb.b > 70 && this.hsb.s < 30 ? '#555' : 'white'
this.ctx.clearRect(0,0,this.width,this.height)
this.clear()
this.ctx.fillStyle = color
this.ctx.strokeStyle = color
@ -235,6 +235,11 @@ module.exports = class Rgb extends _pads_base {
this.ctx.arc(x, y, 4 * PXSCALE, Math.PI * 2, false)
this.ctx.fill()
this.clearRect = [
[x - 10 * PXSCALE,0, 20 * PXSCALE, this.height],
[0, y - 10 * PXSCALE, this.width, 20 * PXSCALE]
]
}
}

View file

@ -172,9 +172,9 @@ module.exports = class Xy extends _pads_base {
draw() {
var x = clip(this.faders.x.percent / 100 * this.width,[0,this.width]),
y = clip((1 - this.faders.y.percent / 100) * this.height,[0,this.height])
y = clip((1 - this.faders.y.percent / 100) * this.height,[0,this.height])
this.ctx.clearRect(0,0,this.width,this.height)
this.clear()
this.ctx.fillStyle = this.faders.x.colors.custom
this.ctx.lineWidth = PXSCALE
@ -200,6 +200,11 @@ module.exports = class Xy extends _pads_base {
this.ctx.beginPath()
this.ctx.arc(x, y, 4 * PXSCALE, Math.PI * 2, false)
this.ctx.fill()
this.clearRect = [
[x - 10 * PXSCALE,0, 20 * PXSCALE, this.height],
[0, y - 10 * PXSCALE, this.width, 20 * PXSCALE]
]
}
}

View file

@ -164,7 +164,7 @@ module.exports = class Fader extends _sliders_base {
o = Math.round(this.percentToCoord(this.valueToPercent(this.originValue))),
m = Math.round(this.widgetData.horizontal ? this.height / 2 : this.width / 2)
this.ctx.clearRect(0,0,this.width,this.height)
this.clear()
if (this.widgetData.horizontal) {
if (this.widgetData.compact) {
@ -174,8 +174,8 @@ module.exports = class Fader extends _sliders_base {
this.ctx.globalAlpha = 1
var x,
min = Math.min(d,o),
max = Math.max(d,o)
min = Math.min(d,o),
max = Math.max(d,o)
for (var i = 1;i < this.rangeKeys.length - 1;i++) {
x = Math.round(this.percentToCoord(this.rangeKeys[i])) + 0.5
@ -201,7 +201,6 @@ module.exports = class Fader extends _sliders_base {
this.ctx.rect(Math.min(d,this.width-PXSCALE), 0, PXSCALE, this.height)
this.ctx.fill()
} else {
this.ctx.lineWidth = Math.round(2 * PXSCALE)
@ -228,6 +227,9 @@ module.exports = class Fader extends _sliders_base {
this.ctx.beginPath()
this.ctx.arc(d + this.margin * PXSCALE, m, 4 * PXSCALE, Math.PI * 2, false)
this.ctx.fill()
this.clearRect = [0, this.height / 2 - 10 * PXSCALE, this.width, 20 * PXSCALE]
}
@ -295,6 +297,9 @@ module.exports = class Fader extends _sliders_base {
this.ctx.beginPath()
this.ctx.arc(m, d, 4 * PXSCALE, Math.PI * 2,false)
this.ctx.fill()
this.clearRect = [this.width / 2 - 10 * PXSCALE, 0, 20 * PXSCALE, this.height]
}
}