expose the computed dimensions of canvas-based widgets in css (as --widget-width and --widget-height) and script (as locals.width and locals.height)

This commit is contained in:
jean-emmanuel 2021-09-29 09:37:37 +02:00
parent ce353d1b59
commit 9e16fff2fa
2 changed files with 25 additions and 1 deletions

View file

@ -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) {