<canvas id="canvas" width="150" height="150"></canvas>Basic usage of canvas / notebook
Basic usage of canvas
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
<canvas id="stockGraph" width="150" height="150">
current stock price: $3.15 + 0.15
</canvas>
<canvas id="clock" width="150" height="150">
<img src="images/clock.png" width="150" height="150" alt="A clock" />
</canvas>const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");const canvas = document.getElementById("canvas");
if (canvas.getContext) {
const ctx = canvas.getContext("2d");
// drawing code here
} else {
// canvas-unsupported code here
}<!doctype html>
<html lang="en-US">
<head>
<meta charset="utf-8" />
<title>Canvas tutorial</title>
<style>
canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<canvas id="canvas" width="150" height="150"></canvas>
<script>
function draw() {
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
}
draw();
</script>
</body>
</html>