Introduction to <canvas>tag

The <canvas>tag in HTML is a powerful element used to <draw graphics>, animations, and images directly on a web page using <javascript>. It provides a dynamic and interactive way to create visual content without relying on external plugins like Flash. in-depth look at the <canvas>tag, its uses, <attributes>, and methods.

Copy to clipboard

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>title of <canvas> tag</title>
        <link rel="stylesheet" href="styles.css">
    </head>
    <body>
        <!-- Canvas element -->
        <canvas id="animationCanvas" width="600" height="400"></canvas>
    
        <script src="script.js"></script>
    </body>
    </html>
Copy to clipboard

    /* Styling the canvas */
    body {
        background-color: #f0f0f0;
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
        margin: 0;
    }
    
    #animationCanvas {
        border-radius: 15px;
        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        background: linear-gradient(to bottom right, #1a2335, #243c57);
        display: block;
    }

Copy to clipboard

const canvas = document.getElementById('animationCanvas');
const ctx = canvas.getContext('2d');

// Ball properties
let x = canvas.width / 2;
let y = canvas.height - 30;
let dx = 3;
let dy = -3;
let ballRadius = 12;

// Create gradient for the ball
function createBallGradient() {
let ballGradient = ctx.createRadialGradient(x, y, ballRadius/2, x, y, ballRadius);
ballGradient.addColorStop(0, 'rgb(62, 75, 254)');
ballGradient.addColorStop(1, 'rgb(62, 75, 254)');
return ballGradient;
}

// Function to draw the ball
function drawBall() {
ctx.beginPath();
ctx.arc(x, y, ballRadius, 0, Math.PI * 2);
ctx.fillStyle = createBallGradient();
ctx.shadowBlur = 20;
ctx.shadowColor = 'rgba(0, 0, 0, 0.2)';
ctx.fill();
ctx.closePath();
}

// Function to draw and animate the ball
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBall();

// Bounce the ball off the walls
if (x + dx > canvas.width - ballRadius || x + dx < ballRadius) {
dx = -dx;
}
if (y + dy > canvas.height - ballRadius || y + dy < ballRadius) {
dy = -dy;
}

x += dx;
y += dy;

requestAnimationFrame(draw);
}

draw(); // Start the animation

Global Attributes

AttributesDescription
accesskeySpecifies a shortcut key to activate or focus an element.
autocapitalizeControls whether and how text input is automatically capitalized.
classSpecifies one or more class names for the element, allowing CSS styling and JavaScript manipulation.
contenteditableSpecifies whether the content of an element is editable or not.
contextmenuSpecifies a context menu for the element.
dirDefines the text direction. Possible values are "ltr" (left-to-right), "rtl" (right-to-left), and "auto".
draggableSpecifies whether an element is draggable. Possible values are "true" or "false".
enterkeyhintSpecifies the action label or icon to be shown for the enter key on virtual keyboards.
hiddenIndicates that the element is not yet, or is no longer, relevant. The browser does not display elements that have the `hidden` attribute set.
idSpecifies a unique identifier for the element.
inputmodeProvides a hint to browsers for which virtual keyboard configuration to use when editing this element or its descendants.
isAllows you to specify the type of custom element.
langSpecifies the language of the element's content.
nonceA cryptographic nonce ("number used once") that can be used by Content Security Policy (CSP) to determine whether or not a given fetch will be allowed to proceed.
partSpecifies the element’s part in the shadow DOM.
slotAssigns a slot in a shadow DOM shadow tree.
spellcheckIndicates whether the element is subject to spell checking.
styleProvides inline CSS styling for the element.
tabindexSpecifies the tab order of the element.
titleAdds extra information about the element, displayed as a tooltip when hovering over the element.
translateSpecifies whether the content of the element should be translated. Possible values are "yes" or "no".
HTML AttributesGlobal AttributesEvent Attributes
To view the full list
To view the full list
To view the full list
elementsChrome BrowsersMicrosoft Edge BrowserFirefox BrowsersSafari BrowserOpera Browser
<canvas>4.09.02.03.19.0