fix: allow multiple visuals for the same pattern

+ using .tag function instead of single .id
This commit is contained in:
Felix Roos 2024-03-28 09:31:12 +01:00
parent ab015ff48a
commit b5720355f9
5 changed files with 15 additions and 11 deletions

View File

@ -106,23 +106,23 @@ function getCanvasWidget(id, options = {}) {
registerWidget('_pianoroll', (id, options = {}, pat) => {
const ctx = getCanvasWidget(id, options).getContext('2d');
return pat.id(id).pianoroll({ fold: 1, ...options, ctx, id });
return pat.tag(id).pianoroll({ fold: 1, ...options, ctx, id });
});
registerWidget('_punchcard', (id, options = {}, pat) => {
const ctx = getCanvasWidget(id, options).getContext('2d');
return pat.id(id).punchcard({ fold: 1, ...options, ctx, id });
return pat.tag(id).punchcard({ fold: 1, ...options, ctx, id });
});
registerWidget('_spiral', (id, options = {}, pat) => {
let _size = options.size || 275;
options = { width: _size, height: _size, ...options, size: _size / 5 };
const ctx = getCanvasWidget(id, options).getContext('2d');
return pat.id(id).spiral({ ...options, ctx, id });
return pat.tag(id).spiral({ ...options, ctx, id });
});
registerWidget('_scope', (id, options = {}, pat) => {
options = { width: 500, height: 60, pos: 0.5, scale: 1, ...options };
const ctx = getCanvasWidget(id, options).getContext('2d');
return pat.id(id).scope({ ...options, ctx, id });
return pat.tag(id).scope({ ...options, ctx, id });
});

View File

@ -91,6 +91,10 @@ export class Hap {
return this.whole != undefined && this.whole.begin.equals(this.part.begin);
}
hasTag(tag) {
return this.context.tags?.includes(tag);
}
resolveState(state) {
if (this.stateful && this.hasOnset()) {
console.log('stateful');

View File

@ -2477,13 +2477,13 @@ export const hsl = register('hsl', (h, s, l, pat) => {
});
/**
* Sets the id of the hap in, for filtering in the future.
* @name id
* Tags each Hap with an identifier. Good for filtering. The function populates Hap.context.tags (Array).
* @name tag
* @noAutocomplete
* @param {string} id anything unique
* @param {string} tag anything unique
*/
Pattern.prototype.id = function (id) {
return this.withContext((ctx) => ({ ...ctx, id }));
Pattern.prototype.tag = function (tag) {
return this.withContext((ctx) => ({ ...ctx, tags: (ctx.tags || []).concat([tag]) }));
};
//////////////////////////////////////////////////////////////////////

View File

@ -134,7 +134,7 @@ export function pianoroll({
let to = cycles * (1 - playhead);
if (id) {
haps = haps.filter((hap) => hap.context.id === id);
haps = haps.filter((hap) => hap.hasTag(id));
}
if (timeframeProp) {

View File

@ -73,7 +73,7 @@ function drawSpiral(options) {
} = options;
if (id) {
haps = haps.filter((hap) => hap.context.id === id);
haps = haps.filter((hap) => hap.hasTag(id));
}
const [w, h] = [ctx.canvas.width, ctx.canvas.height];