Monday, December 18, 2017

Old skool plasma effect

This time I wanted to investigate how to share code between a JavaFX project and a project which uses ScalaJS.
 Plasma Application Screenshot (Scala.js version)
The screenshot above shows a still of the Javascript version of this project, below you see the JavaFX version.

Plasma effect in JavaFX


I'm quite fond of demos, in fact those brought me into programming initially. As such I always wanted to implement a plasma effect, and here you are. I was surprised that it didn't really take much to generated graphics like the above, as such it always pays off to try out new things.

In hindsight, for me the most interesting part is how to setup sbt in order to enable code sharing between JavaFX and Scala.js.  Also, I had to apply some tricks in order to share code which accesses Arrays both in JavaFX and Scala.js. Implementation wise, it is interesting how the color values for each pixel get calculated. Here, we have literally unlimited ways to manipulate this data, you should really download the project and try out new variations of the algorithm yourself.

Regarding performance. the algorithm updates every pixel at every frame, you can imagine this is not very performant neither in Scala.js nor in JavaFX. Something like this would be done in a shader I suppose. But to be fair it is not so bad, either.

Have a look at the code on github, it should be easy to set up and experiment yourself.