In this article we are going to make a simple Bubble screen saver like animation in HTML5.
We are going to use p5.js, a Javascript library for rendering web animations in Canvas.
To get started let us download click here.
Select Single Files > p5.js.
After that we can now start coding this simple animation.
1. Place the downloaded p5.js file in our project folder, create two files in it named sketch.js and index.html
The folder structure should be like this:
index.html - Referenced the 2 js files inside the project
sketch.js
setup() and draw() are the two main functions in p5.js
- setup - in p5.js being called once here after the document has loaded.
- draw - is being called every frame per second(fps),
- createCanvas(windowWidth, windowHeight) - creates a canvas on our HTML, where we render our bubble animation with the size same as the window.
- background(0) - Renders black colored canvas
2. Creating bubbles
Let's add a bubble on our canvas, using the ellipse function
width / 2 and height / 2 makes our bubble be at the center of our screen
Let's open our index.html and try to see in browser, what our web animation currently looks like.
Cool, now we have our first bubble, the next step would be adding bubbles, every time the mouse is Pressed on the screen.
We are going to use p5.js function mousePressed(), to keep track of mouse clicks on the canvas.
Let's move the background(0), in the setup() for the mean time
Leave the draw() function as blank.
And let's create bubble every mouse click.
mouseX and mouseY is current mouse position in the screen after we clicked the canvas.
After running and playing with mouse clicks, we should see something like this.
Let's try playing with this some more, by using random() function of p5.js.
- random() accepts two parameters the min and max value range to randomize
- color() accepts the rgb values
Let's run it again and try playing with it.
Cool! now we have something like this, next step is to make it move.
Next article: http://onecompileman.com/blogs/2