BlueFlux Graphic Design

BlueFlux Graphic Design
My Home

Thursday, 5 April 2012

Using 'Canvas' For Drawing Buttons, Images & Text - The Reasons Behind The New HTML Standards And The Welcome Introduction Of CSS3

HTML5 Canvas - fillText
CSS Background Gradient
Scaling an Image using drawImage

With the growth of the internet, more mobile devices and the increased amount of traffic, there are some new methods such as css3 and HTML5 which can be used to minimise the amount of requests sent by the users' browser.

This, in turn, reduces the amount of information needed to be sent over both phone lines and mobile networks and increases the amount of work done by the users' computer.

Less requests for things like images also reduces the time needed to download a web page, most noticeably when the user is downloading using their networks' data connection.

Let's say, for example, you have a website that has been branded and you have the same logo more than once on either a single page or every page.
Once the logo image has been downloaded on the first visit, any subsequent visit to another page can have the image placed by using the HTML 'drawImage' method. That's one less request!

You can see this in action here CWC. The original image has been requested, downloaded and placed in the top right corner.

But I've also used the image in the header of the price list and scaled it down to fit. Without the new HTML5 I would've had to use a separate image that has been scaled down using my imaging software.

Now, I know that you might think that I can just place the image as is, and then adjust the size of the image properties using my html editor. This is ok but the images rarely get scaled down properly and you lose the original quality.

All this adds up to less traffic to and from one user, less data usage for mobiles and leaves more bandwidth for other users as more and more people get connected to the internet.

I'll give an example of something I'm sure most people have, or will have, experienced on one New Years' Eve.
Do you remember sending a text message wishing your friends or family a happy new year?
Did you notice that it seemed to take a long time to deliver, or that you received one several hours after midnight?
It's all based on the computer and the software used to send out those messages. If you send a text, somewhere along the line it will be placed in a queue.
If there's only ten in that queue, you probably wouldn't have noticed the delay even if you were at the bottom of that queue.
Now imagine the same thing but with a queue that's suddenly grown enormously due the amount of people sending a message at midnight. Obviously you'll have a quite a while to wait if you're near the bottom.

Now apply this example to the amount of requests to a server, the less requests sent the smaller the queue and the faster yours and everyone elses requests get dealt with.

Using a lot of images is one of the obvious file size concerns when designing a website. So using gradients, drawing a few shapes and possibly drawing some text can save on webpage download time and bandwidth.

Below is an example of using a canvas and drawImage to redraw the logo:

I'll assume that you already have your web page set up and open for editing, so the first thing you need to do is insert a the canvas on to the page where you're going to draw the image.

As I said, mine is getting placed at the top of the price list. If you're using an old version of Dreamweaver like I am, you can just insert an Ap Div and then type in 'canvas' in place of 'div:

<div id="logo"></div> will become:-

<canvas id="logo"></canvas>

Then you need to insert the code to draw the image inside your canvas:

<script type = "text/javascript">
var c.getElementById("logo");
var ctx = c.getContext("2d");
var image = new Image();
img.onload = function(){
ctx.drawImage (img, 0, 0, 320, 150);  /* values are: x, y, width, height*/
img.src = "../images/myLogo.png";

If you're placing more than on instance, don't forget to use different variable and function names for each one.

I've created a page as a test using HTML5, the latest css and some jquery.

Take a look at the source code ( to see the different technique needed for drawing within the same canvas, and the problems that arise by using this technique. Such as the conflict between fill styles and the inability to apply rounded corners to any svg/HTML5 type gradients...(currently,working with IE9 and Opera )

**Some of the syntax has changed recently for things like gradients with css and I'll be updating the HTML5 page very soon.** 

I've also used some of the new css techniques, some of which don't yet work in all of the most popular browsers. However they all currently work in the latest Firefox and Chrome browsers

For a nice gradient as a background image I've had to use a very large image up to, and over 2000 pixels wide to cover wide screens as well.
Now you can use css to do it. The following example will create a gradient background from black to white starting from the top of the page: ( gradients not supported in IE9- )


background-image: linear-gradient(top, #000 35%, #fff);
background-image:-moz- linear-gradient(top, #000 35%, #fff); /*Firefox*/

background-image:-webkit- linear-gradient(top, #000 35%, #fff);/*Chrome and Safari*/

background-image:-o-linear-gradient(top, #000 35%, #fff);/*Opera*/
background-image:-ms-linear-gradient(top, #000 35%, #fff);/*Internet Explorer 10*/


One thing I like to use when designing a site is the use of drop shadows. This adds some depth and texture to any page.
By using the css box-shadow method to add drop shadows to images and layers, you can use an image without any shadow effect and have it applied to the image by the users' browser.
This will reduce the size of the image file that needs to be sent. Although this really only applies to the complete image.
Try it for yourself by adding the drop shadow to your design, then compare the file sizes.

If you want to have drop shadow effects within the image itself, then these will need to be created along with the image during the design process.

Remember, that the drop shadow will be applied to the entire div or image that you are using.
Let's say you have an image of a single upper case letter, perhaps as a logo. You can't apply the drop shadow to the letter itself, instead you'll have a shadow applied as if it's a square or rectangle surrounding the letter.

I know, why don't I just use  css: text-shadow? Well, this still doesn't work with IE9.
What about some java plug-ins? Maybe, if I can find one that works yes.....with all browsers!!
So, is it design everything as an image and just use that, or try and achieve the same effect with some css and possibly two, maybe even three different plug-ins for one thing?

I have thought about creating a design, like a logo using all of the newly available goodies. From what I can see, this will involve an enormous amount of layers.

Let's go back to the drop shadow on the text:

To start with I'll need a canvas to draw some text like 'HTML5' using 'fillText' and I'll make that text red using 'fillStyle'.
Now all I need to do is create another canvas, draw the same text but this time in grey and position it at the next lowest z-index with the left and top offset, to make it look like a drop shadow.

So now I've created a simple letter/logo without using any images.

If I elaborate on this whole idea and decide to draw using svg as well. I can most probably make a very nice little design that, when finished, will all be drawn by the browser once the html web page file has been downloaded. Total page requests - 1.

Now there is a reason why I'm not posting an example of such a design..........with the amount of time and the amount of layers that will be put onto the html editor design page to achieve this ( not forgetting drawing with an svg editor as opposed to my usual design software ), I figure that I may as well design the whole thing as an image and place that on my web page anyway!

It's always going to be up to you with how you intend to design your site and finding a balance between the time it's going to take and the real difference in download times for each individual page.

My primary concern is graphic design to be used on the internet and animating that design for certain projects. So sitting down and tearing through each draft of an idea, amending and re-designing it when things don't work out is a damn sight easier with the software I'm using now in comparison to doing everything manually.

 I haven't even attempted to fully animate anything I've designed using css/html because of the difficulties I've already encountered just by having more than one thing at a time running in one layer. Rotating, moving and perspective are all good but will only take you so far if you're interested in better things.

For all the good stuff, I'll be using Adobes' Flash technology as it is an excellent piece of software that already has very efficient communication between layers/pixels within layers i.e. motion and shape tweens to name but a few. 
No right-click & 'Save as' on my designs either..........

I also love to use gradients, but instead of creating a rectangular image and applying gradients with my imaging software, I can now use either of the HTML5/Java or SVG techniques.

Here's an example, I'd like to create a header for my web page that has a title of - 'HTML5 & CSS3'. I would like to have a drop shadow on the text, and I want a gradient for the header background:

This is the image.

Here's the css and html:

<style type="text/css">
#bkngd{/* The main layer being used to hold the other canvas' and have the gradient fill */
background-image: linear-gradient(top, #8b0202, #fff 50%, #fff 10%, #8b0202);
background-image:-moz- linear-gradient(top, #8b0202, #fff 50%, #fff 10%, #8b0202); /*Firefox*/
background-image:-webkit- linear-gradient(top, #8b0202, #fff 50%, #fff 10%, #8b0202); /*Chrome and Safari*/
background-image:-o-linear-gradient(top, #8b0202, #fff 50%, #fff 10%, #8b0202); /*Opera*/
background-image:-ms-linear-gradient(top, #8b0202, #fff 50%, #fff 10%, #8b0202); /*Internet Explorer 10*/
#text{ /* The canvas containing the red text at the front*/
#textShadow{ /*The canvas containing the dark grey text being used to create the text-shadow*/
left:3px; /*offset to create a drop shadow effect*/
<div id="bkgnd">
<canvas id="textShadow"></canvas>
<canvas id="text"></canvas>

<script type="text/javascript">
var c=document.getElementById("text");
var ctx=c.getContext("2d");
var d=document.getElementById("textShadow");
var dtx=d.getContext("2d");
ctx.font="42px Napa SF";  /*use standard fonts if you don't know to use font kits. e.g. Arial*/
ctx.fillText("HTML5 & CSS3", 680, 40);
dtx.font="42px Napa SF";
dtx.fillText("HTML5 & CSS3", 680,40);

Using these methods may seem a little tedious at first, but once you've got a few frequently used gradients perfected, you can just re-use them for any other site and change the colouring very easily.

Using transparency effects is a common method for applying some texture to a graphic.Laying a slightly transparent white (or any other colour) shape over the top of a black shape can create a button with some highlighting.
I would usually use an image created using my drawing software, however these can now be created using the latest HTML techniques.

N.B. After running a few tests: On IE & opera you can't see the button highlight that I've added. From what I can tell, Firefox and Chrome are working fine.

I have also used some of the opacity techniques in the main title, the outer glow is created by applying a css box-shadow at co-ordinates x,y (0,0).

The title text has been drawn using the fillText method.

There's still more to be added to my dedicated HTML5/CSS web page, as well as this blog. Such as some more advanced animation techniques like making a div follow a certain path across the page.

Conclusions so far: Some methods are available to use that work in the most popular browsers,  it's well worth while taking the time to start implementing some of these methods and save bandwidth and download times for everyone.

When I have completed the objectives of this blog, I''l be comparing quality, practicality and the reasons for using flash on your site when it comes to having the edge over your competitors on the world wide web!

No comments:

Post a Comment