Get Canvas Element - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Canvas

Introduction

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

The Canvas object represents an HTML <canvas> element.

You can access a <canvas> element by using getElementById():

Demo Code

ResultView the demo 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() {//from  w  ww. j a  va2 s  .  c  o m
    var c = document.getElementById("myCanvas");
    var ctx = c.getContext("2d");
    ctx.fillStyle = "red";
    ctx.fillRect(20, 20, 150, 100);
}
</script>

</body>
</html>

Related Tutorials