What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 7820 |  Welcome, Guest!   Register  Login
Home > Articles > HTML 5 > Introduction to Canvas in HTML5

Introduction to Canvas in HTML5

1 vote(s)
Rating: 5 out of 5
Article posted by Mallesh on 7/31/2012 | Views: 1405 | Category: HTML 5 | Level: Beginner | Points: 250 red flag


This article gives some brief introduction on Canvas element in HTML 5 that can exposes the fundas of how the canvas element works with an example.

Introduction and History


The Canvas concept was firstly introduced by Apple to be used in Mac OS X - to be in dashboard interfaces. Later it was implimented by other browsers and put the standards by the WHATWG (Web Hypertext Application Technology Working Group, http://whatwg.org/html ). 

Objective

The main objective of this article is to expose the fundas of how the canvas element works in HTML5.

What is Canvas? 

HTML5 defines the <canvas> element (drawing surface) is a rectangular area that allows you to render graphics and bitmaps dynamically inside web browsers.
When you use a canvas element in your web page, it creates some rectangular area on the page. And also offers to allow to control all the pixels of its content and manipulate them in many ways through the use of JavaScript code. By default this area is 300 pixels wide and 150 pixels height, but you can assign your own specified values and other attributes for your canvas element.

HTML 5 defines a set of functions for drawing shapes, defining paths, creating gradients, and applying transformations by using JavaScript and CSS cooperatively.


How Canvas works?

Understanding the Coordinates First:

To work with canvas you have to understand the Cartesian coordinates (2D, 3D-Geometry). As shown in below figure, coordinates in a canvas starts at x=0,y=0 in the upper left corner-called as 'origin'. The rate of change in pixels either in horizontal or in vertical manner the canvas effects in x-axis and y-axis respectively. The width attribute defaults to 300, and the height attribute defaults to 150.



Whenever the height and width attributes are changed, the Canvas is  and any associated contexts must be cleared back to their initial state and reinitialized with the newly specified coordinate space dimensions.

Making the Drawing - Context:

We are going to make everything happens with JavaScript and the Canvas API by using the object  context. Interfaces are defines the specifications of 2d context type using the CanvasRenderingContext2D interface. Briefly the 2D context represents a flat Cartesian surface whose origin is (0,0) at the top left corner, with the coordinate space having x values increasing when going right, and y values increasing when going down.


Canvas Declaration: 

The first step to using canvas is to add a canvas element to the page as follows:

<canvas id="simpCanvas"  width="500"   height="400">
Your browser not supports canvas !
</canvas>

Using the code


Example code to draw a simple canvas (rectangle) as follows:

<!DOCTYPE html>
<html>
<head>
    <title>Canvas Example</title>
    <style type="text/css">
        canvas { border: 2px solid Blue;  }
    </style>
    <script type="text/javascript">
        window.onload = function () {
            var canvas = document.getElementById("myCanvas");
            var context = canvas.getContext("2d");            
            context.strokeStyle = "red";
            context.fillStyle = "green";
            context.strokeRect(30, 30, 100, 100);
            context.fillRect(30, 30, 100, 100);
        }        
    </script>
</head>
<body>
<canvas id="myCanvas">
    Sorry your browser doesn't support. 
    Update the browser..!
</canvas>
</body>
</html>
Output:


Explonation:

-- In the above code the css, let's add a simple style that adds a 2-pixel wide solid blue color border to the canvas. It is used to  dispaly the toal available canvas surface. 

-- The JavaScript code block the getElementById() method can gets the canvas id and is assigned to the variable 'canvas'.

-- The context variable is used to returned by the 'canvas' is assignedd to the context variable by getContext() method. 

Note: We need a '"2d" context from the canvas to for actual drawings.

-- The method strokeRect(30, 30, 100, 100) is used for just outline the rectangle by providing the 'strokeStyle' as red color.

-- The fillRect(30, 30, 100, 100) is used to fill the color by acquiring the 'fillStyle' attribute  as assignes green color here.

-- The Canvas id="myCanvas" is used to grab the element from the DOM.


Benifits of Canvas:

 1. With HTML5's Canvas API, we can improve the performance of our websites by avoiding the need to download images off the network.
 2. We can draw shapes and lines, arcs,text, gradients, patterns and many more graphical images.
 3. The most important feature canvas provides us the power to manipualte pixels in images and in video also.


Canvas 2D Context specification is supported in:

  • Safari 2.0+
  • Chrome 3.0+
  • Firefox 3.0+
  • Internet Explorer 9.0+
  • Opera 10.0+
  • iOS (Mobile Safari) 1.0+
  • Android 1.0+


Conclusion

It has to be implemented in 3D aspects along with HTML-5 family of Technologies. We will meet again with another HTML5 feature.

Reference

I have to use Head First HTML5 Programming Book for make this article mostly.


Thanks.

Regards, 
==  Mallesh ?('-')?

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Latest Articles from Mallesh
Experience:1 year(s)
Home page:
Member since:Thursday, May 17, 2012
Level:Starter
Status: [Member]
Biography:
 Responses
Posted by: Sheonarayan | Posted on: 07 Aug 2012 09:18:00 AM | Points: 25

Very good effort Mallesh, keep it up!

Posted by: The_Ryan | Posted on: 13 Aug 2012 09:08:24 AM | Points: 25

Thanks a lot, I never knew about canvas and the coding of it.

Social Network Developers: http://www.talentsfromindia.com/social-network-web-developers-programmers.html

>> Write Response - Respond to this post and get points
Related Posts

In this article, we are going to learn How to play audio in the HTML5 and how to control the audio play using in HTML element.

This article gives you a practical implementation on how to use HTML5 to create a simple registration form.

In this article you will learn HTML5 new descriptive elements.

In this article, we are going to learn new form elements introduced in HTML5.

In this Article you will learn the Basics of Razor and How to use it

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2013 3:28:22 PM