Gradient with HTML5!

Hello folks,

A couple of days ago I came up with one dilemma: I asked myself how can I put a gradient on the page with HTML and CSS. The only solution that I found is to use gradient pattern (say 1px line witch present the actual gradient) and simply clone it with a CSS.
But I found today other good solution with HTML5.

The code in HTML is very simple:

Basically it is a normal rectangle.

Then you need to write the following javascript.

var d_canvas = document.getElementById(“d”);
var context = d_canvas.getContext(“2d”);
var my_gradient = context.createLinearGradient(0, 0, 300, 0);
my_gradient.addColorStop(0, “black”);
my_gradient.addColorStop(1, “white”);
context.fillStyle = my_gradient;
context.fillRect(0, 0, 300, 225);
What we’ve done is got the picture by id, assign the parameters and returned it back as a gradient.
I hope that was helpful, for more information check the website diveintohtml5.org, I’ve found it quite helpful and descriptive!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.