Jump to content
Slate Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Marble
Slate Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Marble
Sign in to follow this  
Rss Bot

Get started with HTML canvas

Recommended Posts

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:

View the full article

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×