diff --git a/CHANGELOG.md b/CHANGELOG.md index 3be43997..69dd790c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - widgets - encoder: expose angle in script (as `locals.angle`) - new textarea widget (multi line input) + - expose the computed dimensions of canvas-based widgets in `css` (as `--widget-width` and `--widget-height`) and `script` (as `locals.width` and `locals.height`) ## 1.9.14 diff --git a/src/client/widgets/common/canvas.js b/src/client/widgets/common/canvas.js index 0bdcdce6..46ce34e9 100644 --- a/src/client/widgets/common/canvas.js +++ b/src/client/widgets/common/canvas.js @@ -1,10 +1,28 @@ var Widget = require('./widget'), resize = require('../../events/resize'), - canvasQueue = require('./queue') + canvasQueue = require('./queue'), + html = require('nanohtml') class Canvas extends Widget { + static defaults() { + + var defaults = super.defaults(Canvas) + defaults.style.css.help.push( + 'Canvas-based widget have their computed width and height available as css variables (read-only):', + '- `--widget-width`', + '- `--widget-height`', + ) + defaults.scripting.script.help.push( + 'Canvas-based widget have their computed width and height available as local variables:', + '- `locals.width`', + '- `locals.height`', + ) + return defaults + + } + constructor(options) { super(options) @@ -54,6 +72,11 @@ class Canvas extends Widget { 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.parsersLocalScope.height = this.height + this.parsersLocalScope.width = this.width + this.clearRect = [] if (ratio != 1) {