Javascript DOM HTML Canvas Object get

Introduction

The HTML5 <canvas> tag is used to draw graphics with JavaScript.

We can access a <canvas> element via document.getElementById():

var x = document.getElementById("myCanvas");

Click the button to draw on the canvas.

View in separate window

<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>

<button onclick="myFunction()">Test</button>

<script>
function myFunction() {/*www  .j a  va  2 s.  co m*/
  var c = document.getElementById("myCanvas");
  var ctx = c.getContext("2d");
  ctx.fillStyle = "#FF0000";
  ctx.fillRect(20, 20, 150, 100);
}
</script>

</body>
</html>



PreviousNext

Related