Get started with HTML canvas

The HTML <canvas> element is a powerful solution for creating pixel-based graphics on the web using JavaScript, and will allow you to create some remarkable effects with a bit of practice.

In this tutorial, we'll take a look at creating a canvas object, drawing simple visuals on it and animating those visuals.

01. Create a page

Canvas is fundamentally an HTML element that you place on the page, scale to cover the area you want and can then draw upon. To get started, we need to create a simple page with a canvas object.

02. Scale the canvas

To give us plenty of space to play with, we want our canvas to fill the whole screen. We'll also give it a black background so you know it's definitely there. This can actually be a bit tricky to do without ending up with scroll bars or white space. The CSS below should take care of it.

03. Initialise the canvas for use

Next we need to add some JavaScript to set up the canvas ready to use. We'll have it hook off a DOMContentLoaded event to ensure the script doesn't try to run before the canvas element is ready. We'll also have it set the canvas resolution to match the area it covers – otherwise the canvas will scale up without increasing resolution, leading to blurry or pixellated graphics.

04. Draw a shape

You'll notice that in the last step, we crated something called a ‘context'. This is how drawing on canvas occurs. It's easiest to think of the context as a paintbrush that we can use to draw different lines, arcs and basic shapes. 

What we can now do is put our context to use by writing a drawCircle function that will create a 360-degree arc – that is, a circle. We do this by telling the context to define an arc, set styles for the border and fill, then rise the fill() and stroke() functions to actually draw the shape (stroke draws the border).

05. Create many circles

Great. We have a function that can draw circles. Now we need something to draw. Let's extend the code from step 3 to create an array describing circle objects. It will store each circle's x and y co-ordinates, colour and a direction value.

We create this array structure rather than just drawing circles straight away because it will enable us to animate the circles by re-drawing the contents of the array later on.

06. Randomise the colour

In the last step, we've used a couple of new functions that haven't been defined yet. Let's start with randomColour(). This will be a utility function that returns a randomised hexadecimal string representing a colour. It's fairly straightforward to implement.

07. Draw the graphics on the page

Now we're ready to bring things together by implementing the draw() function. This will do two key things. Firstly, it will clear the canvas using the clearRect() function. This will be important when we come to animate our circles, to avoid drawing the new frame over the top of the old. It will then iterate through the array we constructed and draw each circle on the canvas in succession using our drawCircle function.

08. Animate the shapes

If you try it out now, you'll see some static circles painted on the page. But we want to animate them. To do this, we need to extend our draw() function in a couple of ways. Firstly, we'll use the circle.direction value we pushed to the array to calculate changes in x and y position for the circle. 

Next, we'll use a built-in function called requestAnimationFrame that recursively calls the draw() function. requestAnimationFrame allows the browser to decide when to call the function again, avoiding the need to implement timers to calculate when to draw the next frame.

09. Bounce at the edge of the page

There's one thing still missing, though. The circles now just disappear off the edge of the screen. Let's make them bounce back. To do this, we'll add a call to a new function, bounce(circle), within the forEach loop of the draw() function.

The bounce function will determine when a circle is at the edge of the screen, and adjust its direction value appropriately.

This article originally appeared in Web Designer issue 266. Buy it here.

Related articles:



Contributer : Creative Bloq
Get started with HTML canvas Get started with HTML canvas Reviewed by mimisabreena on Thursday, November 02, 2017 Rating: 5

No comments:

Sponsor

Powered by Blogger.