The canvas element for HTML provides scripts with a resolution-dependent bitmap canvas, which can be used for rendering graphs, game graphics, art, or other visual images on the fly.
<canvas attribute-name="attribute-value"></canvas>
<!doctype html>
<html>
<head>
<style>
canvas
{
background-color: black;
}
</style>
</head>
<body>
<canvas width="100">fallback content for browsers that do not support the canvas element</canvas>
</body>
</html>
<!doctype html>
<html>
<head>
<style>
canvas
{
background-color: black;
height: 100px;
width: 100px;
}
</style>
</head>
<body>
<canvas>fallback content for browsers that do not support the canvas element</canvas>
</body>
</html>
<!doctype html>
<html>
<head>
<style>
canvas
{
background-color: black;
}
</style>
</head>
<body>
<canvas>fallback content for browsers that do not support the canvas element</canvas>
<script>
const myelement = document.querySelector("canvas");
myelement.height = "100";
myelement.width = "100";
</script>
</body>
</html>
<!doctype html>
<html>
<head>
<style>
canvas
{
background-color: black;
}
</style>
</head>
<body>
<canvas>fallback content for browsers that do not support the canvas element</canvas>
<script>
const mystyle = document.querySelector("canvas").style;
mystyle.height = "100px";
mystyle.width = "100px";
</script>
</body>
</html>
<!doctype html>
<html>
<head>
<style>
canvas
{
background-color: black;
}
</style>
</head>
<body>
<canvas>fallback content for browsers that do not support the canvas element</canvas>
<script>
const canvas = document.querySelector("canvas");
const context = canvas.getContext("2d");
function myfunction(text, x = 0, y = 0)
{
context.clearRect(0, 0, canvas.width, canvas.height);
context.font = "100px cursive";
context.textBaseline = "top";
const metrics = context.measureText(text);
console.log(metrics);
context.fillStyle = "rgb(255 255 255 / 0.25)";
context.fillRect(
x - metrics.actualBoundingBoxLeft,
y - metrics.actualBoundingBoxAscent,
metrics.actualBoundingBoxLeft + metrics.actualBoundingBoxRight,
metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent
);
context.fillStyle = "rgb(255 255 255 / 0.75)";
context.fillText(
text,
x,
y
);
}
myfunction("hi", 25, 50);
myfunction("hello", 50, 25);
myfunction("text");
</script>
</body>
</html>