From 6478d3362ba8a5e437a12375a2596790688b4f70 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Mon, 6 Dec 2021 12:20:33 +0100 Subject: [PATCH] canvas widget: call sync/script before send because we might need to reupdate the value before sending occurs --- resources/docs/docs/widgets/canvas.md | 35 +++++++++++++++------------ src/client/widgets/pads/canvas.js | 8 ++---- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/resources/docs/docs/widgets/canvas.md b/resources/docs/docs/widgets/canvas.md index 14e61317..45e78624 100644 --- a/resources/docs/docs/widgets/canvas.md +++ b/resources/docs/docs/widgets/canvas.md @@ -45,22 +45,16 @@ First, we use the `touch` property to store the touch coordinates in the `locals // store normalized coordinates if (event.type == "start") { - locals.dx = event.offsetX / width - locals.dy = event.offsetY / height + locals.x = event.offsetX / width + locals.y = event.offsetY / height } else { // when the pointer is moving, increment coordinates // because offsetX and offsetY may not be relevant // if the pointer hovers a different widgets - locals.dx += event.movementX / width - locals.dy += event.movementY / height + locals.x += event.movementX / width + locals.y += event.movementY / height } -// locals.dx and locals.dy may be under 0 or above 1 -// but we want the widget's value and the drawing to -// stay between these limits -locals.x = Math.max(0, Math.min(1, locals.dx)) -locals.y = Math.max(0, Math.min(1, locals.dy)) - // update widget value and send set("this", [locals.x, locals.y]) ``` @@ -71,7 +65,7 @@ Then, we use the `draw` property to draw a circle at the touch coordinates. // draw property // draw an arc at touch coordinates -ctx.arc(locals.x * width, locals.y * height, 6, 0, Math.PI * 2) +ctx.arc(value[0] * width, value[1] * height, 6, 0, Math.PI * 2) // use colorFill property as stroke color ctx.strokeStyle = cssVars.colorFill // draw stroke @@ -83,10 +77,21 @@ Finally, we use the `script` property to make the widget send it's values, and t ```js // script property -// update locals.x and locals.y -// required if we want to update the drawing when receiving osc messages +var x, y if (value.length == 2) { - locals.x = value[0] - locals.y = value[1] + // if number of values is correct, use incomming value + x = value[0] + y = value[1] +} else { + // else: use last valid coordinates + x = locals.x + y = locals.y } + +// apply limits +x = Math.max(0, Math.min(1, value[0])) +y = Math.max(0, Math.min(1, value[1])) + +// re-update inner value without retriggering script or sending message +set("this", [x, y], {sync: false, send: false}) ``` diff --git a/src/client/widgets/pads/canvas.js b/src/client/widgets/pads/canvas.js index b282fbae..78a4e036 100644 --- a/src/client/widgets/pads/canvas.js +++ b/src/client/widgets/pads/canvas.js @@ -15,19 +15,15 @@ class CanvasWidget extends Canvas { return super.defaults().extend({ class_specific: { - // value: {type: 'number', value: 1, help: [ - // - // ]}, autoClear: {type: 'boolean', value: true, help: [ 'If set to `false`, the canvas context won\'t be cleared automatically and `ctx.clear` will need to be called in `draw`.' ]}, continuous: {type: 'boolean|number', value: false, help: [ 'If set to `true`, `draw` will be called at each frame, otherwise it will be called only when the widget is touched and when it receives a value.', - 'Can be a number between 1 and 60 to specify the framerate.' + 'Can be a number between 1 and 60 to specify the framerate (default: 30 fps).' ]}, touch: {type: 'string', value: '', help: [ 'Script executed when the widget is touched', - 'Exposed ' ]}, draw: {type: 'string', value: '', help: [ 'Script executed when the widget is redrawn.' @@ -116,8 +112,8 @@ class CanvasWidget extends Canvas { this.batchDraw() - if (options.send) this.sendValue() if (options.sync) this.changed(options) + if (options.send) this.sendValue() if (options.defaultInit) this.script.setValue(this.value, {id: this.getProp('id')}) }