<canvas>

Description

The <canvas> element is a drawing surface that we drive using JavaScript. The canvas element is simple. All of its functionality is exposed through a JavaScript object.

Example

The width and height attributes specify the size of the canvas.


<!DOCTYPE HTML>
<html>
<head>
<style>
canvas {<!--  w  w w .j  av  a  2s .  c o m-->
      border: medium double black;
      margin: 4px
}
</style>
</head>
<body>
      <canvas width="500" height="200"> 
           Your browser doesn't support the <code>canvas</code> element 
      </canvas>
</body>
</html>

Click to view the demo

The code above generates the following result.

canvas

Draw with canvas

The following code creates a canvas tag and use Javascript to draw a rectangle on it.


<!DOCTYPE HTML>
<html>
<head>
<style>
canvas {<!--from   www.j  a  va2  s. c om-->
      border: medium double black;
      margin: 4px
}
</style>
</head>
<body>
      <canvas id="canvas" width="500" height="100"> 
            Your browser doesn't support the <code>canvas</code> element 
    </canvas>
      <script>
            var ctx = document.getElementById("canvas").getContext("2d");
            ctx.fillRect(10, 10, 50, 50);
      </script>
</body>
</html>

Click to view the demo

The code above generates the following result.

canvas




















Home »
  HTML CSS »
    HTML CSS Reference »




HTML Tag Reference
CSS Reference
CSS Selector Reference
Encoding Reference