pads: per-point customization on the way

This commit is contained in:
jean-emmanuel 2025-03-12 15:44:03 +01:00
parent 542c0629fb
commit 848f591930
6 changed files with 173 additions and 105 deletions

View file

@ -12,7 +12,9 @@
- xy/multixy: add `auto` choice to `axisLock` (locks on the first moved axis)
- file: `extension` can now be an array of strings
- plot/eq: smoother curves
- sliders/pads: `doubleTap` can now be `"script"`, in which case `onTouch` will be called with `event.type` set to `doubleTap`
- sliders/pads: `doubleTap` can now be `"script"`, in which case `onTouch` will be called with `event.type` set to `doubleTap`
- xy: add `label` property
- multixy: add `padsAttr` property for per-pad customization
## 1.27.5

View file

@ -34,7 +34,7 @@ class CanvasQueue extends EventEmitter{
flush() {
for (var h in this.queue) {
if (this.queue[h].width > 0 && this.queue[h].height > 0) {
if (this.queue[h].width > 0 && this.queue[h].height > 0 && this.queue[h].visible) {
this.queue[h].draw()
}
}

View file

@ -8,7 +8,7 @@ var Pad = require('./pad'),
var xyDefaults = Xy.defaults()._props()
module.exports = class MultiXy extends Pad {
class MultiXy extends Pad {
static description() {
@ -31,6 +31,17 @@ module.exports = class MultiXy extends Pad {
'Defines the number of points on the pad',
'Can be an array of strings that will be used as labels for the points (ex: `[\'A\', \'B\']`)'
]},
pointsAttr: {type: 'object', value: [], help: [
'Defines per-point properties that are otherwise inherited from the multixy. Must be an array of objects (one per point) that may contain the following keys:',
'- visible (visibility and interactability)',
'- colorFill (background color)',
'- colorStroke (outline color)',
'- colorText (label color)',
'- color (shorthand for colorFill and colorStroke)',
'- alphaFillOn (background opacity)',
'- pointSize',
'- label'
]},
snap: {type: 'boolean', value: false, help: [
'By default, the points are dragged from their initial position.',
'If set to `true`, touching anywhere on the widget\'s surface will make them snap to the touching coordinates',
@ -64,7 +75,6 @@ module.exports = class MultiXy extends Pad {
super(options)
this.npoints = Array.isArray(this.getProp('points')) ? this.getProp('points').length : this.getProp('points') || 0
this.labels = Array.isArray(this.getProp('points'))
this.preventChildrenTouchState = true
this.pads = []
@ -83,7 +93,7 @@ module.exports = class MultiXy extends Pad {
logScaleY:this.getProp('logScaleY'),
axisLock:this.getProp('axisLock'),
pointSize: 'auto',
pips: this.getProp('pips') && i == this.npoints-1,
pips: false,
sensitivity: this.getProp('sensitivity'),
input:false
}, parent: this})
@ -92,6 +102,12 @@ module.exports = class MultiXy extends Pad {
}
// tiny hack for pips until there's a proper range refactoring
this.faders = this.pads[0].faders
this.pointsAttr = []
this.updatePadsAttr()
this.padsCoords = []
this.touchMap = {}
@ -108,6 +124,7 @@ module.exports = class MultiXy extends Pad {
for (var i in this.pads) {
if (Object.values(this.touchMap).indexOf(i) != -1) continue
if (!this.pads[i].getProp('visible')) continue
ndiff = Math.abs(e.offsetX - this.padsCoords[i][0]) + Math.abs(e.offsetY - (this.padsCoords[i][1] + this.height))
@ -116,6 +133,7 @@ module.exports = class MultiXy extends Pad {
diff = ndiff
}
}
this.touchMap[e.pointerId] = id
@ -132,8 +150,6 @@ module.exports = class MultiXy extends Pad {
this.pads[id].trigger('draginit', e)
this.pads[id].touched = 1
if (this.getProp('ephemeral')) this.batchDraw()
}, {element: this.widget, multitouch: true})
this.on('drag',(e)=>{
@ -163,8 +179,6 @@ module.exports = class MultiXy extends Pad {
delete this.touchMap[e.pointerId]
if (this.getProp('ephemeral')) this.batchDraw()
}, {element: this.widget, multitouch: true})
this.on('value-changed',(e)=>{
@ -241,8 +255,8 @@ module.exports = class MultiXy extends Pad {
super.cacheCanvasStyle(style)
for (var k in this.pads) {
this.pads[k].cacheCanvasStyle()
this.pads[k].batchDraw()
}
this.batchDraw()
}
@ -252,6 +266,7 @@ module.exports = class MultiXy extends Pad {
for (var k in this.pads) {
this.pads[k].setVisibility()
}
this.batchDraw()
}
@ -270,25 +285,7 @@ module.exports = class MultiXy extends Pad {
this.clear()
this.clearRect = []
for (var i=0;i<this.npoints;i++) {
if (this.pads[i].getProp('ephemeral') && !this.pads[i].active) continue
var margin = this.padPadding,
x = clip(this.pads[i].faders.x.percent,[0,100]) / 100 * (this.width - 2 * margin) + margin,
y = (100 - clip(this.pads[i].faders.y.percent,[0,100])) / 100 * (this.height - 2 * margin) + margin,
t = (this.labels ? this.getProp('points')[i] : i) + '',
length = this.fontSize * t.length
this.ctx.fillStyle = this.pads[i].cssVars.colorText
this.ctx.globalAlpha = this.cssVars.alphaPips
this.ctx.fillText(t, x + 0.5 * PXSCALE, y + PXSCALE)
this.clearRect.push([x - length / 2, y - this.fontSize / 2, length, this.fontSize + 2 * PXSCALE])
}
if (this.getProp('pips')) this.drawPips()
}
@ -319,7 +316,6 @@ module.exports = class MultiXy extends Pad {
}
this.updateHandlesPosition()
this.batchDraw()
for (let i=0;i<this.npoints * 2;i=i+2) {
[this.value[i],this.value[i+1]] = this.pads[i/2].getValue()
@ -330,12 +326,65 @@ module.exports = class MultiXy extends Pad {
}
updatePadsAttr(options) {
var attrs = this.getProp('pointsAttr')
if (!Array.isArray(attrs)) attrs = []
this.pointsAttr = []
var legacyLabels = Array.isArray(this.getProp('points'))
for (var i = 0; i < this.npoints; i++) {
var a = attrs[i] || {}
this.pointsAttr[i] = {}
// color alias
if (a.color !== undefined) {
if (a.colorFill === undefined) a.colorFill = a.color
if (a.colorStroke === undefined) a.colorStroke = a.color
}
if (a.label === undefined) {
a.label = legacyLabels ? this.getProp('points')[i] : String(i)
}
for (var n of ['visible', 'axisLock', 'spring', 'colorFill', 'colorStroke', 'colorText', 'alphaFillOn', 'pointSize', 'label']) {
this.pointsAttr[i][n] = a[n] === undefined ? this.getProp(n) : a[n]
if (this.pads[i].cachedProps[n] !== this.pointsAttr[i][n]) {
this.pads[i].cachedProps[n] = this.pointsAttr[i][n]
this.pads[i].onPropChanged(n, options)
var cssVar = this.constructor.cssVariables.find(x=>x.js==n)
if (cssVar) {
this.pads[i].canvas.style.setProperty(cssVar.css, this.pointsAttr[i][n] == 'auto' ? '' : this.pointsAttr[i][n])
}
if (n == 'visible') {
this.pads[i].canvas.style.setProperty('opacity', this.pointsAttr[i][n] ? 1 : 0)
}
}
}
}
}
onPropChanged(propName, options, oldPropValue) {
if (super.onPropChanged(...arguments)) return true
switch (propName) {
case 'padding':
for (let w of this.pads) {
w.onPropChanged(propName)
}
return
case 'colorText':
case 'colorWidget':
case 'colorFill':
@ -343,19 +392,11 @@ module.exports = class MultiXy extends Pad {
case 'alphaStroke':
case 'alphaFillOff':
case 'alphaFillOn':
case 'padding':
case 'lineWidth':
for (let w of this.pads) {
w.onPropChanged(propName)
}
return
case 'pointsAttr':
case 'spring':
case 'pointSize':
case 'axisLock':
for (let w of this.pads) {
w.cachedProps[propName] = this.getProp(propName)
w.onPropChanged(propName, options)
}
this.batchDraw()
this.updatePadsAttr()
return
}
@ -369,3 +410,10 @@ module.exports = class MultiXy extends Pad {
}
}
MultiXy.dynamicProps = MultiXy.prototype.constructor.dynamicProps.filter(n => n !== 'decimals').concat([
'pointsAttr',
])
module.exports = MultiXy

View file

@ -23,6 +23,61 @@ class Pad extends Canvas {
}
drawPips() {
var margin = this.padPadding,
pipTexts = margin >= this.fontSize * 1.5
this.ctx.lineWidth = PXSCALE
this.ctx.fillStyle = this.cssVars.colorText
this.ctx.strokeStyle = this.cssVars.colorStroke
this.ctx.globalAlpha = this.cssVars.alphaPips
this.ctx.beginPath()
for (let i in this.faders.x.rangeKeys) {
let pip = this.faders.x.rangeKeys[i],
px = Math.round(0.5 * this.faders.x.percentToCoord(pip)) * 2
if (parseInt(px) === px) px -= 0.5
if (pipTexts) {
this.ctx.textAlign = 'center'
let label = this.faders.x.rangeLabels[i]
this.ctx.fillText(label, px, this.height - margin / 2)
}
if (pip == 0 || pip == 100) continue
this.ctx.moveTo(px, margin - PXSCALE)
this.ctx.lineTo(px, this.height - margin + PXSCALE)
}
for (let i in this.faders.y.rangeKeys) {
let pip = this.faders.y.rangeKeys[i],
py = Math.round(0.5 * this.faders.y.percentToCoord(pip)) * 2
if (parseInt(py) === py) py -= 0.5
if (pipTexts) {
this.ctx.textAlign = 'right'
let label = this.faders.y.rangeLabels[i]
this.ctx.fillText(label, margin / 2 + this.fontSize / 2, py)
}
if (pip == 0 || pip == 100) continue
this.ctx.moveTo(margin - PXSCALE, py)
this.ctx.lineTo(this.width - margin + PXSCALE, py)
}
this.ctx.globalAlpha = this.cssVars.alphaFillOn
this.ctx.stroke()
}
onPropChanged(propName, options, oldPropValue) {
var ret = super.onPropChanged(...arguments)
@ -47,7 +102,8 @@ Pad.cssVariables = Pad.prototype.constructor.cssVariables.concat(
Pad.dynamicProps = Pad.prototype.constructor.dynamicProps.filter(n => n !== 'decimals').concat([
'spring',
'axisLock',
'pointSize'
'pointSize',
'label'
])
module.exports = Pad

View file

@ -21,6 +21,7 @@ module.exports = class Xy extends Pad {
pointSize: {type: 'integer', value: 20, help: 'Defines the points\' size'},
ephemeral: {type: 'boolean', value: false, help: 'When set to `true`, the point will be drawn only while touched.'},
pips: {type: 'boolean', value: true, help: 'Set to `false` to hide the scale'},
label: {type: 'string', value: '', help: 'Text displayed in the handle'}
},
class_specific: {
snap: {type: 'boolean', value: false, help: [
@ -219,6 +220,8 @@ module.exports = class Xy extends Pad {
draw() {
if (!this.visible) return
var pointSize = this.cssVars.pointSize,
margin = this.padPadding,
x = this.faders.x.percentToCoord(this.faders.x.percent),
@ -238,69 +241,11 @@ module.exports = class Xy extends Pad {
var pipsDrawn = false
if (this.getProp('pips')) {
var pipTexts = margin >= this.fontSize * 1.5
this.ctx.lineWidth = PXSCALE
this.ctx.fillStyle = this.cssVars.colorText
this.ctx.strokeStyle = this.cssVars.colorStroke
this.ctx.globalAlpha = this.cssVars.alphaPips
this.ctx.beginPath()
for (let i in this.faders.x.rangeKeys) {
let pip = this.faders.x.rangeKeys[i],
px = Math.round(0.5 * this.faders.x.percentToCoord(pip)) * 2
if (parseInt(px) === px) px -= 0.5
if (pipTexts) {
this.ctx.textAlign = 'center'
let label = this.faders.x.rangeLabels[i]
this.ctx.fillText(label, px, this.height - margin / 2)
pipsDrawn = true
}
if (pip == 0 || pip == 100) continue
this.ctx.moveTo(px, margin - PXSCALE)
this.ctx.lineTo(px, this.height - margin + PXSCALE)
pipsDrawn = true
}
for (let i in this.faders.y.rangeKeys) {
let pip = this.faders.y.rangeKeys[i],
py = Math.round(0.5 * this.faders.y.percentToCoord(pip)) * 2
if (parseInt(py) === py) py -= 0.5
if (pipTexts) {
this.ctx.textAlign = 'right'
let label = this.faders.y.rangeLabels[i]
this.ctx.fillText(label, margin / 2 + this.fontSize / 2, py)
pipsDrawn = true
}
if (pip == 0 || pip == 100) continue
this.ctx.moveTo(margin - PXSCALE, py)
this.ctx.lineTo(this.width - margin + PXSCALE, py)
pipsDrawn = true
}
this.ctx.globalAlpha = this.cssVars.alphaFillOn
this.ctx.stroke()
if (this.getProp('pips')) this.drawPips()
else {
// this.clearRect = [x - pointSize - 2 * PXSCALE, y - pointSize - 2 * PXSCALE, (pointSize + PXSCALE) * 4, (pointSize + PXSCALE) * 4]
}
if (!pipsDrawn) this.clearRect = [x - pointSize - 2 * PXSCALE, y - pointSize - 2 * PXSCALE, (pointSize + PXSCALE) * 4, (pointSize + PXSCALE) * 4]
if (!ephemeral || this.active) {
this.ctx.strokeStyle = this.cssVars.colorStroke
this.ctx.globalAlpha = this.active ? 1 : 0.75
@ -308,6 +253,20 @@ module.exports = class Xy extends Pad {
this.ctx.beginPath()
this.ctx.arc(x, y, pointSize, Math.PI * 2, false)
this.ctx.stroke()
var t = this.getProp('label')
if (t) {
this.ctx.fillStyle = this.cssVars.colorText
this.ctx.globalAlpha = 1
this.ctx.textAlign = 'center'
this.ctx.fillText(t, x + 0.5 * PXSCALE, y + PXSCALE)
if (!this.getProp('pips')) {
var length = this.fontSize * t.length
// this.clearRect.push([x - length / 2, y - this.fontSize / 2, length, this.fontSize + 2 * PXSCALE])
}
}
}
@ -320,6 +279,9 @@ module.exports = class Xy extends Pad {
switch (propName) {
case 'label':
this.batchDraw()
return
case 'spring':
if (this.getProp('spring') && !this.touched) this.setValue([this.faders.x.getSpringValue(),this.faders.y.getSpringValue()], {...options})
return

View file

@ -32,7 +32,7 @@
--point-size: 20rem;
canvas:not(:first-child) {
canvas {
position: absolute;
top: 0;
left: 0;
@ -139,7 +139,7 @@
.html {
pointer-events: auto;
}
inner {
position: relative;
flex: 1;