Linear Gradient With Canvas Using HTML 5

Introduction

This article is a very interesting piece related to designing with a Linear Gradient with a Canvas Using HTML 5. For this purpose we can use the createLinearGradient() method. In this section we will see the Linear gradient with canvas when we will run the application on the browser.

Here we will use some JavaScript and some styles along with HTML code. Just go through the steps to see how to create this application.

Let's see how the LinearGradient application can be created. To do so use the following steps.

Step 1 : Open a HTML editor or Visual Studio.
sd.gif

Open File menu ->select new ->Choose Website then.

0000.jpg

This is where we will create the HTML5 application.

  • Go to Solution Explorer
  • Right-click on the Application name
  • Select Add-->add new item
  • Now in the window that opens, select an HTML page or new Web form
  • Rename it to Lineargradientpage.aspx

lineargradient1.gif

Step 2 : In this section we will create the style for the media and create the .css on the media screen. Put the given script in the Head section of the HTML or between the <head>--</head> tags. Here the CSS are used for design purposes.

CSS Script

<style>
body
{
  margin: 0px;
  padding: 0px;
}
#myCanvas
{
   border: 2px solid #9C9898;
   margin-top: 50px;
   margin-left: 50px;
   background-color: #00B2EE;
   box-shadow: 5px 5px 8px #222;
 }
.title
{
   text-align: center;
   font-family: Segoe UI Light, Arial, Helvetica;
   font-size: 2.2em;
   margin: 1em;
}
.info
{
   text-align: center;
   font-family: Segoe UI Light, Arial, Helvetica;
   font-size: 1.2em;
   margin: 0.25em;
}
</style>

Step 3 : In this part we need to work on some JavaScript. For fully understanding how the JavaScript works, download the attached .rar file and run the LinearGradient application.

The whole JavaScript looks as in the following.

<
script>
        window.onload = function ()
        {
         var canvas = document.getElementById("myCanvas");
         var context = canvas.getContext("2d");
           context.beginPath();
           context.moveTo(170, 80);
           context.bezierCurveTo(130, 100, 130, 150, 230, 150);
           context.bezierCurveTo(250, 180, 320, 180, 340, 150);
           context.bezierCurveTo(420, 150, 420, 120, 390, 100);
           context.bezierCurveTo(430, 40, 370, 30, 340, 50);
           context.bezierCurveTo(320, 5, 250, 20, 250, 50);
           context.bezierCurveTo(200, 5, 150, 20, 170, 80);
           context.closePath();
           // add linear gradient
         var grd = context.createLinearGradient(230, 0, 370, 200);
           grd.addColorStop(0, "#CD2626");
           grd.addColorStop(1, "#FFF68F");
           context.fillStyle = grd;
           context.fill(); 
            // add stroke
           context.lineWidth = 5;
           context.strokeStyle = "#FFFFFE";
           context.stroke();
        };
</
script>

Step 4 : In this section we are going to become familiar with the body part of HTML scripting. Replace this script from the body section of the Lineargradientpage.aspx page. Here we pass a #myCanvas in the canvas tag.

<body style="background-color: #00CED1">
    <center>
        <h1>
            Linear Gradient Using Canvas
        </h1>
    </center>
    <hr />
    <canvas id="myCanvas" width="500" height="200">
    </canvas>
</body>

Step 5 : The complete code for the
LinearGradient application.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Lineargradientpage.aspx.cs" Inherits="LinearGradient._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <style>
   </style>
    <script>
   </script>
</head>
<
body style="background-color: #00CED1">
    <center>
        <h1>
            Linear Gradient Using Canvas
        </h1>
    </center>
    <hr />
    <canvas id="myCanvas" width="500" height="200">
    </canvas>
</body>
</
html>

Step 6 : Output Press F5

Note :
For the accurate output of HTML5 applications, you must have the Google Chrome browser in your PC.
We will see the Linear gradient with canvas when we will run the application on the browser.

lineargradient.gif

Here are the some useful resources

Working with Linear/Radial Gradients and Gradient Vector Transform in XAML Silverlight
Canvas Clipping Region Using HTML 5
LinearGradientBrush in Silverlight
Use Canvas Tag in HTML 5
WPF LinearGradientBrush using XAML

Up Next
    Ebook Download
    View all
    Learn
    View all