From c84067c4560430156b8b68a74ec1d8c6526ee79f Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Sun, 22 May 2022 12:43:26 +0200 Subject: [PATCH] canvas: add `onResize` script property --- CHANGELOG.md | 5 ++++- resources/docs/docs/widgets/canvas.md | 11 +++++++++++ src/client/widgets/pads/canvas.js | 28 +++++++++++++++++++++++++++ src/client/widgets/sliders/knob.js | 2 +- 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 545b65b2..89c2586f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,10 @@ - custom module: submodules not loading their own submodules with relative paths properly - custom module: issue with circular submodule requires - server: harmless error message when importing css files from the main theme file - - server: allow using folder names "client" and "assets" for user files (eg for images used in a session) + - server: allow using folder names "client" and "assets" for user files (eg for images used in a session) + +- widgets + - canvas: add `onResize` script property ## 1.16.2 diff --git a/resources/docs/docs/widgets/canvas.md b/resources/docs/docs/widgets/canvas.md index abcf21d5..0f772078 100644 --- a/resources/docs/docs/widgets/canvas.md +++ b/resources/docs/docs/widgets/canvas.md @@ -66,6 +66,17 @@ This script has access to the same variables and functions as the `script` prope - `ctx`: [canvas rendering context](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D) of the widget - `cssVars`: object containing the computed value of some of the widget's style properties such as `colorWidget`, `alphaFill`, `padding`, etc +### `onResize` + +This script is executed when the widget has resized, before it is redrawn. + +This script has access to the same variables and functions as the `script` property (except the event-specific ones), plus the following: + +- `value`: widget value +- `width`: canvas width in pixels +- `height`: canvas height in pixels +- `cssVars`: object containing the computed value of some of the widget's style properties such as `colorWidget`, `alphaFill`, `padding`, etc + ## Example: XY pad Let's create a simple xy pad, with a value made of two numbers between 0 and 1. We set `valueLength` to `2` to make sure the widget only accepts incoming messages with two values (x and y). diff --git a/src/client/widgets/pads/canvas.js b/src/client/widgets/pads/canvas.js index d0170e78..04788bee 100644 --- a/src/client/widgets/pads/canvas.js +++ b/src/client/widgets/pads/canvas.js @@ -33,6 +33,7 @@ class CanvasWidget extends Canvas { onValue: {type: 'script', value: '', editor: 'javascript', help: ['Script executed whenever the widget\'s value updates. See documentation.']}, onTouch: {type: 'script', value: '', editor:'javascript', help: ['Script executed when the widget is touched and released, and when the pointer moves when the widget is touched. See documentation.',]}, onDraw: {type: 'script', value: '', editor:'javascript', help: ['Script executed when the widget is redrawn. See documentation.']}, + onResize: {type: 'script', value: '', editor:'javascript', help: ['Script executed when the widget is resized. See documentation.']}, } return defaults @@ -49,6 +50,20 @@ class CanvasWidget extends Canvas { this.valueLength = Math.max(1, parseInt(this.getProp('valueLength')) || 1) this.value = this.valueLength > 1 ? Array(this.valueLength).fill(0) : 0 + if (this.getProp('onResize')) { + this.scripts.onResize = new Script({ + widget: this, + property: 'onResize', + code: this.getProp('onResize'), + context: { + value: 0, + width: 100, + height: 100, + cssVars: {} + } + }) + } + if (this.getProp('onDraw')) { this.scripts.onDraw = new Script({ widget: this, @@ -161,6 +176,19 @@ class CanvasWidget extends Canvas { } + extraResizeHandle(event) { + + if (!this.getProp('onResize')) return + + this.scripts.onDraw.run({ + value: this.value, + width: this.width, + height: this.height, + cssVars: this.cssVars + }, {sync: false, send: false}) + + } + onRemove() { clearInterval(this.drawInterval) diff --git a/src/client/widgets/sliders/knob.js b/src/client/widgets/sliders/knob.js index 1e0b81d6..2a3c2e62 100644 --- a/src/client/widgets/sliders/knob.js +++ b/src/client/widgets/sliders/knob.js @@ -160,7 +160,7 @@ module.exports = class Knob extends Slider { } - extraResizeHandle() { + extraResizeHandle(event) { super.extraResizeHandle(event)