From 6f6d0ed12104e274035ef3556dde580139bdc9a4 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Sat, 19 Feb 2022 11:42:32 +0100 Subject: [PATCH] canvas: prevent flickering on canvas based widgets when they are recreated --- CHANGELOG.md | 5 ++ src/client/widgets/common/canvas.js | 20 ++++--- src/client/widgets/pads/multixy.js | 5 +- src/client/widgets/pads/xy.js | 18 +++---- src/client/widgets/sliders/fader.js | 81 ++++++++++++----------------- src/client/widgets/sliders/knob.js | 4 +- src/client/widgets/sliders/range.js | 4 +- 7 files changed, 64 insertions(+), 73 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 30c1d20b..e6a1447b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/client/widgets/common/canvas.js b/src/client/widgets/common/canvas.js index 1c63b371..c25fcf15 100644 --- a/src/client/widgets/common/canvas.js +++ b/src/client/widgets/common/canvas.js @@ -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 } diff --git a/src/client/widgets/pads/multixy.js b/src/client/widgets/pads/multixy.js index d21f9cb2..a34bc117 100644 --- a/src/client/widgets/pads/multixy.js +++ b/src/client/widgets/pads/multixy.js @@ -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() diff --git a/src/client/widgets/pads/xy.js b/src/client/widgets/pads/xy.js index f0b79050..44126c9a 100644 --- a/src/client/widgets/pads/xy.js +++ b/src/client/widgets/pads/xy.js @@ -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 + } } diff --git a/src/client/widgets/sliders/fader.js b/src/client/widgets/sliders/fader.js index 3abfd07b..7afa1cf4 100644 --- a/src/client/widgets/sliders/fader.js +++ b/src/client/widgets/sliders/fader.js @@ -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() - } - - }) - + } } diff --git a/src/client/widgets/sliders/knob.js b/src/client/widgets/sliders/knob.js index 9f2c554b..1e0b81d6 100644 --- a/src/client/widgets/sliders/knob.js +++ b/src/client/widgets/sliders/knob.js @@ -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) diff --git a/src/client/widgets/sliders/range.js b/src/client/widgets/sliders/range.js index 0ca58509..e66cbc1a 100644 --- a/src/client/widgets/sliders/range.js +++ b/src/client/widgets/sliders/range.js @@ -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