The <canvas> element is used to draw graphics on a web page using JavaScript.
Canvas is only a container.
You must use JavaScript to draw shapes, text, images, etc.
Supported by all major browsers.
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.moveTo(0, 0);
ctx.lineTo(200, 100);
ctx.stroke();
ctx.beginPath();
ctx.arc(95, 50, 40, 0, 2 * Math.PI);
ctx.stroke();
ctx.font = "30px Arial";
ctx.fillText("Hello World", 10, 50);
ctx.font = "30px Arial";
ctx.strokeText("Hello World", 10, 50);
var grd = ctx.createLinearGradient(0, 0, 200, 0);
grd.addColorStop(0, "red");
grd.addColorStop(1, "white");
ctx.fillStyle = grd;
ctx.fillRect(10, 10, 150, 80);
var grd = ctx.createRadialGradient(75, 50, 5, 90, 60, 100);
grd.addColorStop(0, "red");
grd.addColorStop(1, "white");
ctx.fillStyle = grd;
ctx.fillRect(10, 10, 150, 80);
var img = document.getElementById("img1");
ctx.drawImage(img, 10, 10);
| Method | Purpose |
|---|---|
| moveTo() | Start position |
| lineTo() | Draw line |
| arc() | Draw circle |
| fillText() | Fill text |
| strokeText() | Border text |
| fillRect() | Draw filled rectangle |
| drawImage() | Draw image |