Skip to main content

HTML5 Canvas Basic Tutorial

HTML5 Canvas, HTML5 Canvas Tutorial, HTML5 Canvas Tutorial step by step, canvas basic , canvas example to draw circle, canvas example to draw line, canvas example to draw rectangle,fill canvas example

The canvas element is an element defined in HTML5 code using width and height attributes

 It allows bitmap (2D) drawing which is controlled using JavaScript 

 The HTML <canvas> element is used to draw graphics, on the fly, via scripting (usually JavaScript).

 

Browser support  Following latest Browser

To Learn Canvas you Must Know JavaScript  

How to find wether your browser support convas or Not :

Execute following code if message display

your browser doesn't support canvas! 

Code :

<canvas id="myCanvas" width="500" height="300">
  your browser doesn't support canvas!
</canvas>

 Canvas Tag :

 we are going to defined canvas tag as follows (it is paired tag)

 <canvas id="mycanvas" height="500px" width="500px">

</canvas>


HTML Code  SAVE as Basic.html

<!DOCTYPE html>
<html>
<body>
    <canvas id="myCanvas" width="200" height="100" >
          Your browser does not support the HTML5 canvas tag.
    </canvas>
</body>
</html>

in this code we are going to use three attributes of canvas that is id, height and width .

if you execute this code using browser it display nothing .it is because canvas tag provide only container or area to draw graphics . 

 

 What is canvas Element : 

it is a tag which provide a area to draw 2D graphics .

Remember To Draw graphics on canvas it should be accessible to access it give unique id to it as shown in code i have used <canvas id="myCanvas " >  

By using this id using javascript you can draw graphics on the canvas 


 

To identify Canvas element you can use CSS or style tag property such as border, background-color  

Code

<!DOCTYPE html>
<html>
    <head>
    <style>
    #myCanvas{

                         border:2px solid black;
                         background-color:pink;

                         }
    </style>
</head>
<body>   
<canvas id="myCanvas" width="200" height="100" >   
     Your browser does not support the HTML5 canvas tag. 
  </canvas>
</body>
</html>

Output:


you can also use external CSS to control Style of your canvas Element .


Lets we take 

Basic canvas example to draw circle with explanation of each step .

i am using same HTML and css previous code just i need 500px height and width as well as i am going to use JavaScript code to draw circle shown below in code.

<!DOCTYPE html>
<html>
    <head>
    <style>
    #myCanvas{

                         border:2px solid black;
                         background-color:pink;

                         }
    </style>
</head>
<body>   
<canvas id="myCanvas" width="500" height="500" >   
     Your browser does not support the HTML5 canvas tag. 
  </canvas>

    <script>
      var c=document.getElementById("myCanvas");
       var ctx=c.getContext("2d");
        ctx.arc(100,70,50,0,2*Math.PI);
       ctx.stroke(); 

</script>
</body>
</html>

Output:

Lets Understand JavaScript code step by step

code is 

<script>
      var c=document.getElementById("myCanvas");
       var ctx=c.getContext("2d");
        ctx.arc(100,70,50,0,2*Math.PI);
       ctx.stroke(); 

</script>

 ______________________________ 


 1. var c=document.getElementById("myCanvas");

  to understand this line lets see our tag

<canvas id="myCanvas" width="500" height="500" >   
     Your browser does not support the HTML5 canvas tag. 
  </canvas>

we will get reference of our canvas in variable c so that we can use it to draw graphics.

 2.var ctx=c.getContext("2d"); 

getContext() method return object ctx which makes properties  and methods available to use in our program. 

by using ctx.  we can use avilable canvas method to draw shapes  

examples are 

To Draw Circle  We will use

 ctx.arc(x, y, r, srart_angl, end_angl); 

To Draw Rectangle 

ctx.rect(x1,y1,width,height);

  

IMPORTANT

to make drawing visible on canvas use

  ctx.stroke();

 it is compulsory if you don't use stroke then your drawing will not display on canvas


To download canvas tutorial to draw circle click below link

Click here to download

HTML5 Canvas, HTML5 Canvas Tutorial, HTML5 Canvas Tutorial step by step, canvas basic , canvas example to draw circle, canvas example to draw line, canvas example to draw rectangle,fill canvas example

Popular posts from this blog

Design Marathi website Using HTML CSS

          Design Marathi website Using HTML CSS,मराठी वेबसाईट designing HTML ,step by step Marathi website, how to display Marathi content in your website, Design Marathi website step by step , Marathi website using HTML and CSS, How To Design website using different Languages  Design Marathi website Using HTML CSS, Design Marathi website Using PHP, Design Marathi website Using JSP, Design Marathi website Using ASP, Design Marathi website Using C# Dot Net, Design Marathi website Using Vb Dot Net, मराठी वेबसाईट मराठी वेबसाईट बनवन अगदी सोप आहे मी या ब्लॉग मध्ये दिलेल्या स्टेप्स वापरून तुम्ही काही क्षणात मराठी वेब पेज तयार करू शकता  Design Marathi website Using HTML CSS,मराठी वेबसाईट designing HTML ,step by step Marathi website, how to display Marathi content in your website, Design Marathi website step by step , Marathi website using HTML and CSS, How To Design website using different Languages    Design Marathi website U

what is Blogger

what is Blogger, what is Blogger short info, Blogger hindi information,  Blogger.com एक फ्री Blog Publishing Platform है. जिसकी सहायता से Free Blog बनाकर Texts, Images, Videos आदि आसानी से शेयर की जा सकती है. और दुनिया को अपने लेखन कौशल से अवगत कराया जा सकता है. Blogger की गिनती Content Management Systems में होती है. क्योंकि यह Content को Yearly, Monthly, Weekly, Daily, Catigarically, Labels आदि के द्वारा Manage करता है. और ये सभी Entry पाठकों के लिए उपलब्ध रहती है. Google Blogger Kya Hai in Hindi Blogger पर जो ब्लॉग बनाया जाता है. वह blogspot.com का Sub-Domain होता है. जो Google Server पर Hosted रहता है. Blogger Users गूगल सर्वर को Access नही कर पाते है. लेकिन गूगल अकाउंट से उन्हे Blogger Dashboard पर अनुमति मिल जाती है. इसलिए वे अपना Blog Manage आसानी से कर सकते. Blogspot.com के अलावा Users को Country Specifit Domain Name भी उपलब्ध करवाया जाता है. मिसाल के तौर पर एक Indian Blogger Users अगर अपना ब्लॉग बनाता है तो वह blogspot.com के स्थान पर blogspot.in का चुनाव कर

how to add a youtube video to your website html

embed youtube video in html without iframe,html embed youtube video,how to insert a video in html from computer,iframe video autoplay in html,embed video html,youtube embed code generator,youtube refused to connect html,how to insert mp4 video in html, Use following steps 1. Create basic html page 2. Login to your Google account 3. Visit  www.youtube.com 4. Search video you want to add in your website 5. Select video  6. Click on share button at bottom  you can see share option   7. Click on embed as shown  8. Copy code and paste in html page save and run html page  embed youtube video in html without iframe,html embed youtube video,how to insert a video in html from computer,iframe video autoplay in html,embed video html,youtube embed code generator,youtube refused to connect html,how to insert mp4 video in html,