Animated website background with HTML5
After fooling around with some of the basic HTML5 Canvas features for my previous post, I decided to also work out some other ideas I had. Today I started implementing a simple animated background for a website.
Using a Canvas Object as a background
Before actually getting to the animating of the background, I first had to setup Canvas as a background object. In order to do this. I have added the Canvas element in an absolute positioned div that is aligned to the top of the page. The reason for using a div around the Canvas is because the browsers don’t seem to want to put the Canvas in page at 100% without creating scrollbars.
After placing the background a div can be placed on top. When this div is created with a max-height of 100% and overflow set to auto. This layer will automatically give a scrollbar when necessary.
So that leaves us with the following HTML and CSS code:
<body> <div id="backgroundHolder"> <canvas id="backgroundCanvas" width="800" height="600"></canvas> </div> <div id="realbody"> <!-- Content --> </div> </body>
CSS:
#backgroundHolder { position: absolute; height: 100%; width: 100%; overflow: hidden; background-color: #999; } #realbody { position: absolute; top: 0; width: 100%; overflow: auto; max-height: 100%; } canvas { width: 100%; height: 100%; }
Let the browser resize the Canvas
When I created the first setup for this demo. I started by setting the height and width of the Canvas to the height and width of the page. This soon turned out to be quite heavy on the CPU usage at higher resolutions. So I made the Canvas a static size and let the browser resize the area to 100%. This works and performs way better. And because the animated content is abstract it does not really matter that we loose the aspect ratio.
Once you have setup the Canvas behind the page. You can draw whatever you want in this Canvas and it will render behind the normal content in the browser.
How to animate the background
Now that we have done the setup for the page, we can start to animate. I created some really simple code which doesn’t do very proper checking on the boundaries. It basicly switches 3 variables around to make the background animated: move the bars from right to left, change the rotation of the Canvas context from (π / 32) to -(π / 32), and fade the color between an array of colors.
Drawing the bars
The bars are just drawn rectangles which are drawn a little bit above the canvas till a little bit below the Canvas, this is because of the rotation. In order to draw the bars I use the following code:
for (var i = leftOffset; i < canvas.width + 600; i += (BAR_WIDTH * 2)) { context.fillRect(i - (canvas.width / 2), 0 - (canvas.height / 2) - 200, BAR_WIDTH, canvas.height + 400); } leftOffset -= BAR_SPEED; if (leftOffset + BAR_WIDTH * 2 + 300 < 0) { leftOffset += BAR_WIDTH * 2; }
The BAR_SPEED is to move the initial leftOffset to the left all the time. Up till it reaches a certain value. Then the leftOffset is scaled up a little so that we don’t create more and more bars over time. The starting point of the context.fillRect might seem a bit weird. This is because the Canvas is translated to the center. So the starting point for drawing a bar is minus the width of the Canvas area.
Rotating the bars
Rotating the bars is even simpler then the drawing of them. We can just use the context.rotate function to set the rotation of the content that we are drawing. Keep in mind that you might want to save the context state before rotating so you can restore it after the rotation. The JavaScript code for rotating is the following:
context.save(); context.rotate(angle += angleAdjust); // Draw your content here if (angle > MAX_ANGLE || angle < 0 - MAX_ANGLE) { angleAdjust = 0 - angleAdjust; }
In the example above MAX_ANGLE is set to π / 32. The variable angleAdjust is used to make sure that we either increase the angle or decrease the angle.
Switching the colors
The effect to finish the whole thing of, is the switching of colors. For this demo I am using 6 colors that I just guessed some RGB values for. Blending the colors is done with this Color object in JavaScript:
function Color(r,g,b) { this.r = r; this.g = g; this.b = b; this.step = COLOR_STEP; this.fadeTo = function(color) { this.r = this.r + ((color.r - this.r) * this.step); this.g = this.g + ((color.g - this.g) * this.step); this.b = this.b + ((color.b - this.b) * this.step); this.step += COLOR_STEP; if (this.r == color.r && this.g == color.g && this.b == color.b) { this.step = COLOR_STEP; return true; } return false; } this.getRGB = function() { return Math.round(this.r) + "," + Math.round(this.g) + "," + Math.round(this.b); } }
Every time the fadeTo method is called, the color that is used will blend more to the color provided as an argument to the fadeTo method. If color provided as a parameter is the same as the color. Then this method will return true so the rest of the code can start providing another color to blend to.
The code to start running through the array of colors then becomes something like this:
if (activeColor.fadeTo(colors[nextColorIndex])) { activeColor = colors[nextColorIndex++]; if (nextColorIndex >= colors.length) { nextColorIndex = 0; } } context.fillStyle = 'rgb(' + activeColor.getRGB() + ')';
After setting this demo up today, I have to conclude that doing animations like this is really not that difficult. I think within the next few months we will see more and more awesome website implementations with Canvas.


That is a very neat effect. I just discovered your blog, but will definately peruse it for more helpful tools.
Nice work
Thank you!
I’ve been reading a few posts and i’m adding your blog to my rss reader , thanks ! solo case
I had been just considering about Javier Chutsenko and you have surely aided out. Thanks!
How are you?. I appreciate your website. Just wondering if you wanted to profit from it? I have signed up to a network that pays out 1 dollars every time one of my visitors completes a quick quiz to get access to premium content. Check out my website for more info. Think it is perfect for this blog. Kindest Regards
Thanks for some good points there. I am kind of new to the internet , so I printed this off to put in my file, any better way to go about keeping track of it then printing?
Superb post – and good domain by the way!
I for something completely different, but found your ! And have to say . Nice read. I m bookmarking it!.
Your blog is so informative – keep up the good work!!!!
Sometimes I think all this aerobic stuff can actually be bad for you. It puts such a strain on the body. I am not sure what I think about all this exercise craze yet.
Thank you for all the information, great resource for a newbie.
Yes, couldn’t agree greater. And I’d prefer to add that you have received a tremendous colour plan in your website, I endure with colour blindness and countless webmasters do not give us a 2nd believed!
Nice website! I adore a few of the articles which have been written, and especially the comments posted! I am going to definately be returning!
I’ve just stumbled upon your site while searching for a tutorial on an related subject. Glad I did too. There’s a lot I like. Anyway, you’ve been bookmarked and I’ll be back soon.
Not positive if any of your other visitors have asked this…but what sort of theme are you using for your blog? I really like the style. I am running a blog too however cannot ever seem to find my perfect theme. This one is really close …however I might perhaps tweak some things within the Header for my style. thx.
Great tutorial…just starting to play with the canvas element and was glad to find some good info on using it as a background. I submitted your page to StumbleUpon so others can enjoy too
How did you create your blog template? I got a blog as well and my template looks kinda boring so people don’t stay on my blog very long :/. Anyway, Interesting post, thanks I am constantly amazed by this kind of thing.
Superb Content, Top Notch, and User Friendly are the best way to describe this post, reading this motivates me.
I was actually searching something different on Google, but found this post and i’m glad i did, thanks, this is definately a good read.
thanks for this usefull informations..
[...] radikalFX » Blog Archive » Animated website background with HTML5 [...]
Found you through CanvasDemos… excellent application of Canvas techniques! I really like the subtle color-fading technique you’re using.
One comment, which I also mentioned on Twitter: using CSS {height:100%;width:100%} to resize the canvas *kills* performance on some browser/OS configurations, e.g. Windows+Firefox 3.0.14, which I have at work. For this application it probably doesn’t matter that much, but just thought I’d point it out since I’ve run into it before in my experiments.
[...] the Animated Backgrounds demo. You can also read about how he did it here. (No Ratings [...]
Eyh Bertus, all is fine over here. Recently revamped the website, but now just have to find the motivation to keep updating it! How are you doing now a days, still working for Sogyo?
Hey Berry! Nice site, I’ll put it in my brogroll
How’s life? Cheers, Bertus