Logo
  Sunday, May 20, 2012
Sign-In  |  Sign-Up  |  Contact Us  | Bookmark |  RSS Feed

HTML5 Tutorial FeedBurner

HTML5 Gradient  
Applying gradient requires three steps to be completed.
  • 1. Creation of the gradient object
  • 2. Applying colors and stops where colors change. We need to call on addColorStop for identifying color stops.
  • 3. Call on fillStyle or strokeStyle on the context
Gradient can be specified from point to point. If you set starting point as (0,0) and ending as (0,50) than fill or stroke will shift gradually from one color to another as specified by the user.

We can apply transparency value to the gradient as well as transition. If you want to apply these two commands then you need to use RGBA functions that has alpha component.

Code explains how to apply gradient is presented below.

<script>

    function gradientExample(context) {

        var grad = context.createLinearGradient(-1, -10, 1, -10);

        grad.addColorStop(0, '#441100');

        grad.addColorStop(0.4, '#772200');

        context.fillStyle = grad;

        context.fillRect(-1, -10, 5, 10);

    }

</script>

This code will apply gradient to an object making it more appealing to the user. In addition, HTML5 provide capability to apply radial gradient that allows you to specify circular representation of colors.

creatRadialGradient(x1,y1,x2,y2,r1);

In the example above; x and y represent center of the circle and r is a radius.

Print HTML5 Gradient Bookmark HTML5 Gradient

Related Articles  
HTML5 canvas browser compatibility
HTML5 is not official and mandated version of HTML and not every browser vendor supports it. You need to be aware of ...
Working with HTML5 Paths
The HTML5 Paths are designed to represent any shape that you may want to render. You would use beginPath call to start ...
How to insert Images into a canvas in HTML5?
HTML5 allows adding and manipulating images inside a canvas. Images can be modified, stretched and stamped with the ...
Using HTML5 Stroke Styles
HTML5 provides with new styling capabilities when it comes to canvas. We can improve the way lines are drawn on canvas ...
Applying CSS to the HTML5 Canvas
CSS can be applied to the HTML5 canvas and you can do pretty much same things with canvas as with images and other ...
HTML5 Canvas Coordinates
The HTML5 canvas has X-Axis and Y-Axis and is two dimensional. It has starting point in the upper-left corner which ...
More