canvas widget: call sync/script before send because we might need to reupdate the value before sending occurs
This commit is contained in:
parent
3a872f6920
commit
6478d3362b
2 changed files with 22 additions and 21 deletions
|
|
@ -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})
|
||||
```
|
||||
|
|
|
|||
|
|
@ -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')})
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue