-
Notifications
You must be signed in to change notification settings - Fork 261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Control flow primitives: if() #77
Comments
Hydra does support these because it is just javascript..... or am I missing something? |
Ah, I see. I was thrown off by reading the documentation list of commands and not finding any such keywords in there -- I thought those were the only keywords that would work in the web editor window. I see now the "Written in javascript and compatible with other javascript libraries" on the page https://hydra-editor.glitch.me/. Perhaps I am being confused by an unrelated issue with the hydra web editor, specifically. For context, here is a simple sketch in the p5.js web editor (https://editor.p5js.org/). Move mouse left for blue, right for red.
By comparison, I now tried putting this into the hydra editor (https://hydra-editor.glitch.me/):
Moving the mouse left and right does nothing. The screen does update based on the current mouse location each time I evaluate with Option-Enter, but it isn't reactive. Is this a specific limitation of the web editor, and would you recommend the Atom plugin or Electron app for this instead? Or am I not doing this in the right way? |
you can pass functions as parameters example: osc(
8,
() => {
if( mouse.x > 400 ){
return -0.5;
} else {
return 0.5;
}
},
1
)
.out(o0) and for brevity, you could use a ternary operator to change those values example: osc(
8,
() => mouse.x > 400 ? -0.5 : 0.5,
1
)
.out(o0) |
Thanks, @zachkrall -- these are great examples of using Is it possible to use For example, this does not work.
Although this does, just not with live updating:
|
I think that when you are evaluating those if statements it falls outside of the render loop that hydra uses so it only returns a value once. https://github.com/ojack/hydra-synth/blob/5519e14198f9b367fcd09f15bbf54f851f5cc543/example/index.js#L59 |
Yes, exactly what @zachkrall says. You need to continuously call the function (i.e. what is happening in Processing's draw function) in order to continue to evaluate it. For example:
(In this example, make sure you only call update() once, otherwise you will be adding more and more continuously-called functions) |
Actually I should add a caveat to the above! It can be slower because the shader is re-compiled each time each chain of hydra functions is called. So I still would recommend finding a way to do things more idiomatically "hydra" because it will be better in terms of performance. All of your examples can be recreated using multiple outputs and .blend(). For example,
|
Thanks for this, all. If I'm understanding the gist, it is:
|
It would be nice if hydra supported one or more control-flow primitives.
Just the single primitive
if()
(even withoutelse
) could be very powerful. Pseudocode example:interactive multi-sketch selection:
or a cycling timed sketch sequence:
The text was updated successfully, but these errors were encountered: