<canvas> - HTML CSS HTML Tag

HTML CSS examples for HTML Tag:canvas

Description

The <canvas> element marks a region which can be used to draw graphics using JavaScript.

Summary

Placement Inline
Content Inline, and text
Start/End Tag Start tag: required, End tag: required
Version New in HTML5

Tag-Specific Attributes

The following table shows the attributes that are specific to the <canvas> tag.

Attribute Value Description
width pixels Sets the width of the canvas.
heightpixels Sets the height of the canvas.

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html lang="en">
 <head>
  <title>Example of HTML canvas Tag</title>
  <style type="text/css">
  canvas{<!-- w  w w  .  ja  v a 2  s .c o m-->
    border: 1px solid #000;
  }
</style>
 </head>
 <body>
  <canvas id="myCanvas" width="300" height="200"></canvas>
  <script type="text/javascript">
    window.onload = function(){
      var canvas = document.getElementById("myCanvas");
      var context = canvas.getContext("2d");
      context.moveTo(50, 150);
      context.lineTo(250, 50);
      context.stroke();
    };
  </script>
 </body>
</html>

Related Tutorials