Skip to content

Commit

Permalink
Add P function to hydra, that is able to resolve strudel like pattern…
Browse files Browse the repository at this point in the history
…s for value control

Strudel needs to be loaded first, in order to be able to use the mini parser.
We also just access strudel via the window parent object, which might cause problems in some sandboxes?
  • Loading branch information
reckter committed Dec 23, 2024
1 parent 1b53ab6 commit fd15573
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/web/src/lib/hydra-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ declare global {
src: Function;
H: Function;
P5: Function;
P: Function;
}
}

Expand Down Expand Up @@ -64,6 +65,22 @@ export class HydraWrapper {

window.H = this._hydra;

// Enable using strudel style mini-patterns for argument control on Hydra.
// strudel needs to be loaded first, otherwise this will cause warnings, and rendering will not
// include the mini-pattern.
window.P = (pattern: any) => {
return () => {
// parse using the strudel mini parser
const reified = window.strudel.mini.minify(pattern)

const now = window.strudel.core.getTime()

// query the current value
const arc = reified.queryArc(now, now)
return arc[0].value;
}
}

this.initialized = true;
console.log("Hydra initialized");
}
Expand Down
1 change: 1 addition & 0 deletions packages/web/src/routes/frames/hydra.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export function Component() {
useAnimationFrame(
useCallback(() => {
window.m = window.parent?.mercury?.m;
window.strudel = window.parent?.strudel?.strudel;
}, [])
);

Expand Down
2 changes: 2 additions & 0 deletions packages/web/src/routes/frames/strudel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export function Component() {
}
};

window.strudel = instance;

window.addEventListener("message", handleWindowMessage);

return () => {
Expand Down

0 comments on commit fd15573

Please sign in to comment.