canvas: prevent flickering on canvas based widgets when they are recreated

This commit is contained in:
jean-emmanuel 2022-02-19 11:42:32 +01:00
parent 1ae1937112
commit 6f6d0ed121
7 changed files with 64 additions and 73 deletions

View file

@ -1,5 +1,10 @@
# Changelog
## 1.15.1
- bug fixes
- prevent flickering on canvas based widgets when they are recreated
## 1.15.0
- bug fixes

View file

@ -95,18 +95,22 @@ class Canvas extends Widget {
this.ctx.scale(ratio, ratio)
}
if (!preventDraw) {
if (!this.hasSize) {
this.hasSize = true
this.setVisibility()
this.batchDraw()
} else {
this.draw()
}
this.extraResizeHandle(event)
if (!this.hasSize) {
this.hasSize = true
this.setVisibility()
}
this.draw()
})
}
extraResizeHandle(event) {
// to be overridden if subclass needs additional actions
// before redraw
}

View file

@ -175,15 +175,16 @@ module.exports = class MultiXy extends Pad {
}
resizeHandle(event){
extraResizeHandle(event){
super.resizeHandle(event)
super.extraResizeHandle(event)
for (var k in this.pads) {
this.pads[k].width = this.width
this.pads[k].height = this.height
this.pads[k].padPadding = this.padPadding
}
this.updateHandlesPosition()

View file

@ -153,19 +153,15 @@ module.exports = class Xy extends Pad {
}
resizeHandle(event){
extraResizeHandle(event){
super.resizeHandle(event)
super.extraResizeHandle(event)
fastdom.measure(()=>{
fastdom.mutate(()=>{
for (var k in this.faders) {
this.faders[k].width = this.width
this.faders[k].height = this.height
this.faders[k].gaugePadding = this.padPadding
}
})
})
for (var k in this.faders) {
this.faders[k].width = this.width
this.faders[k].height = this.height
this.faders[k].gaugePadding = this.padPadding
}
}

View file

@ -113,73 +113,58 @@ class Fader extends Slider {
}
resizeHandle(event) {
extraResizeHandle(event) {
var ratio = CANVAS_SCALING * this.scaling
event.preventDraw = true
super.extraResizeHandle(event)
super.resizeHandle(event)
if (this.getProp('horizontal')){
fastdom.mutate(()=>{
this.ctx.setTransform(1, 0, 0, 1, 0, 0)
this.ctx.rotate(-Math.PI/2)
this.ctx.translate(-this.height * ratio, 0)
if (this.getProp('horizontal')){
this.ctx.setTransform(1, 0, 0, 1, 0, 0)
this.ctx.rotate(-Math.PI/2)
this.ctx.translate(-this.height * ratio, 0)
if (ratio != 1) this.ctx.scale(ratio, ratio)
if (ratio != 1) this.ctx.scale(ratio, ratio)
}
}
if (this.getProp('gradient')) {
if (this.getProp('gradient')) {
var colors = this.getProp('gradient')
var colors = this.getProp('gradient')
if (
(Array.isArray(colors) && colors.length > 1) ||
(typeof colors === 'object' && Object.keys(colors).length > 1)
) {
if (
(Array.isArray(colors) && colors.length > 1) ||
(typeof colors === 'object' && Object.keys(colors).length > 1)
) {
var grad = this.ctx.createLinearGradient(0, this.getProp('horizontal') ? 0 : this.height, 0, this.getProp('horizontal') ? this.width : 0)
var grad = this.ctx.createLinearGradient(0, this.getProp('horizontal') ? 0 : this.height, 0, this.getProp('horizontal') ? this.width : 0)
try {
if (Array.isArray(colors)) {
for (var i in colors) {
grad.addColorStop(i / (colors.length - 1), colors[i])
}
this.gaugeGradient = grad
} else {
for (var k in colors) {
if (!isNaN(k)) {
grad.addColorStop(clip(k, [0, 1]), colors[k])
}
}
this.gaugeGradient = grad
try {
if (Array.isArray(colors)) {
for (var i in colors) {
grad.addColorStop(i / (colors.length - 1), colors[i])
}
} catch(err) {
this.errorProp('gradient', '', err)
}
this.gaugeGradient = grad
} else {
for (var k in colors) {
if (!isNaN(k)) {
grad.addColorStop(clip(k, [0, 1]), colors[k])
}
}
this.gaugeGradient = grad
}
} catch(err) {
this.errorProp('gradient', '', err)
}
}
if (!this.hasSize) {
this.hasSize = true
this.setVisibility()
this.batchDraw()
} else {
this.draw()
}
})
}
}

View file

@ -160,9 +160,9 @@ module.exports = class Knob extends Slider {
}
resizeHandle() {
extraResizeHandle() {
super.resizeHandle(...arguments)
super.extraResizeHandle(event)
this.minDimension = Math.min(this.width, this.height)

View file

@ -92,9 +92,9 @@ class Range extends Fader {
}
resizeHandle(event){
extraResizeHandle(event){
super.resizeHandle(event)
super.extraResizeHandle(event)
for (var f of this.faders) {
f.width = this.width