From 6b50e40aef246764afe64f58794ef0012fdddc10 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Fri, 21 Jan 2022 09:42:14 +0100 Subject: [PATCH] fastdom: canvas resize fix --- src/client/widgets/common/canvas.js | 47 ++++++++++---------- src/client/widgets/sliders/fader.js | 66 ++++++++++++++++------------- 2 files changed, 60 insertions(+), 53 deletions(-) diff --git a/src/client/widgets/common/canvas.js b/src/client/widgets/common/canvas.js index d9a9030d..a129c1f0 100644 --- a/src/client/widgets/common/canvas.js +++ b/src/client/widgets/common/canvas.js @@ -58,7 +58,9 @@ class Canvas extends Widget { resizeHandleProxy() { - this.resizeHandle(...arguments) + fastdom.measure(()=>{ + this.resizeHandle(...arguments) + }) } @@ -75,33 +77,32 @@ class Canvas extends Widget { this.clearRect = [] - if (ratio != 1) { - this.ctx.setTransform(1, 0, 0, 1, 0, 0) - this.ctx.scale(ratio, ratio) - } + this.cacheCanvasStyle(style) - fastdom.measure(()=>{ - this.cacheCanvasStyle(style) - fastdom.mutate(()=>{ + fastdom.mutate(()=>{ - this.canvas.setAttribute('width', width * ratio) - this.canvas.setAttribute('height', height * ratio) - this.container.style.setProperty('--widget-height', this.height + 'rem') - this.container.style.setProperty('--widget-width', this.width + 'rem') + this.canvas.setAttribute('width', width * ratio) + this.canvas.setAttribute('height', height * ratio) + this.container.style.setProperty('--widget-height', this.height + 'rem') + this.container.style.setProperty('--widget-width', this.width + 'rem') - this.ctx.font = this.fontWeight + ' ' + this.fontSize + 'px ' + this.fontFamily - this.ctx.textBaseline = 'middle' - this.ctx.textAlign = this.textAlign + this.ctx.font = this.fontWeight + ' ' + this.fontSize + 'px ' + this.fontFamily + this.ctx.textBaseline = 'middle' + this.ctx.textAlign = this.textAlign - if (!this.hasSize) { - this.hasSize = true - this.setVisibility() - this.batchDraw() - } else { - this.draw() - } + if (ratio != 1) { + this.ctx.setTransform(1, 0, 0, 1, 0, 0) + this.ctx.scale(ratio, ratio) + } + + if (!this.hasSize) { + this.hasSize = true + this.setVisibility() + this.batchDraw() + } else { + this.draw() + } - }) }) diff --git a/src/client/widgets/sliders/fader.js b/src/client/widgets/sliders/fader.js index fe01aac4..57854cc7 100644 --- a/src/client/widgets/sliders/fader.js +++ b/src/client/widgets/sliders/fader.js @@ -1,5 +1,6 @@ var {clip} = require('../utils'), - Slider = require('./slider') + Slider = require('./slider'), + fastdom = require('fastdom') class Fader extends Slider { @@ -118,50 +119,55 @@ class Fader extends Slider { super.resizeHandle(event) - 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) + fastdom.mutate(()=>{ - if (ratio != 1) this.ctx.scale(ratio, ratio) - } + 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 (this.getProp('gradient')) { + if (ratio != 1) this.ctx.scale(ratio, ratio) + } - var colors = this.getProp('gradient') + if (this.getProp('gradient')) { - if ( - (Array.isArray(colors) && colors.length > 1) || - (typeof colors === 'object' && Object.keys(colors).length > 1) - ) { + var colors = this.getProp('gradient') - var grad = this.ctx.createLinearGradient(0, this.getProp('horizontal') ? 0 : this.height, 0, this.getProp('horizontal') ? this.width : 0) + if ( + (Array.isArray(colors) && colors.length > 1) || + (typeof colors === 'object' && Object.keys(colors).length > 1) + ) { - try { - if (Array.isArray(colors)) { + var grad = this.ctx.createLinearGradient(0, this.getProp('horizontal') ? 0 : this.height, 0, this.getProp('horizontal') ? this.width : 0) - for (var i in colors) { - grad.addColorStop(i / (colors.length - 1), colors[i]) - } - this.gaugeGradient = grad + try { + if (Array.isArray(colors)) { - } else { - - for (var k in colors) { - if (!isNaN(k)) { - grad.addColorStop(clip(k, [0, 1]), colors[k]) + for (var i in colors) { + grad.addColorStop(i / (colors.length - 1), colors[i]) } - } - this.gaugeGradient = grad + 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) } - } catch(err) { - this.errors.gradient = err + } } - } + }) + }