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 an HTML5 application.
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 is used for design purposes. CSS Script
  1. <style>
  2. body
  3. {
  4. margin: 0px;
  5. padding: 0px;
  6. }
  7. #myCanvas
  8. {
  9. border: 2px solid #9C9898;
  10. margin-top: 50px;
  11. margin-left: 50px;
  12. background-color: #00B2EE;
  13. box-shadow: 5px 5px 8px #222;
  14. }
  15. .title
  16. {
  17. text-align: center;
  18. font-family: Segoe UI Light, Arial, Helvetica;
  19. font-size: 2.2em;
  20. margin: 1em;
  21. }
  22. .info
  23. {
  24. text-align: center;
  25. font-family: Segoe UI Light, Arial, Helvetica;
  26. font-size: 1.2em;
  27. margin: 0.25em;
  28. }
  29. </style>
Step 3: In this part, we need to work on some JavaScript. To fully understanding how the JavaScript works, download the attached .rar file and run the LinearGradient application.
The whole JavaScript looks as in the following.
  1. <script>
  2. window.onload = function ()
  3. {
  4. var canvas = document.getElementById("myCanvas");
  5. var context = canvas.getContext("2d");
  6. context.beginPath();
  7. context.moveTo(170, 80);
  8. context.bezierCurveTo(130, 100, 130, 150, 230, 150);
  9. context.bezierCurveTo(250, 180, 320, 180, 340, 150);
  10. context.bezierCurveTo(420, 150, 420, 120, 390, 100);
  11. context.bezierCurveTo(430, 40, 370, 30, 340, 50);
  12. context.bezierCurveTo(320, 5, 250, 20, 250, 50);
  13. context.bezierCurveTo(200, 5, 150, 20, 170, 80);
  14. context.closePath();
  15. // add linear gradient
  16. var grd = context.createLinearGradient(230, 0, 370, 200);
  17. grd.addColorStop(0, "#CD2626");
  18. grd.addColorStop(1, "#FFF68F");
  19. context.fillStyle = grd;
  20. context.fill();
  21. // add stroke
  22. context.lineWidth = 5;
  23. context.strokeStyle = "#FFFFFE";
  24. context.stroke();
  25. };
  26. </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.
  1. <body style="background-color: #00CED1">
  2. <center>
  3. <h1>
  4. Linear Gradient Using Canvas
  5. </h1>
  6. </center>
  7. <hr />
  8. <canvas id="myCanvas" width="500" height="200">
  9. </canvas>
  10. </body>
Step 5: The complete code for the LinearGradient application.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Lineargradientpage.aspx.cs" Inherits="LinearGradient._Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <style>
  6. </style>
  7. <script>
  8. </script>
  9. </head>
  10. <body style="background-color: #00CED1">
  11. <center>
  12. <h1>
  13. Linear Gradient Using Canvas
  14. </h1>
  15. </center>
  16. <hr />
  17. <canvas id="myCanvas" width="500" height="200">
  18. </canvas>
  19. </body>
  20. </html>
Step 6: Output Press F5 Note: For the accurate output of HTML5 applications, you must have the Google Chrome browser on your PC. We will see the Linear gradient with canvas when we will run the application on the browser.
lineargradient.gif
Here are some useful resources