HyperText Markup Language/Canvas

From Wikibooks, open books for an open world
Jump to navigation Jump to search

The <canvas> tag allows for scriptable rendering of 2D shapes. While a canvas is embedded in an HTML page, it can be rendered with JavaScript.

Rendering a Canvas:

<canvas id="e">This text will be displayed if the browser does not support canvas.</canvas>

JavaScript

var Canvas=document.getElementById("e");
var Context=Canvas.getContext("2d");
context.fillStyle="green";
context.fillRect(30,40,30,20);
context.strokeStyle="red";
context.strokeRect(30,40,30,20);
context.strokeText("Hello",30,40);
context.fillText("World",30,50);