search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

Table of Contents
Set up the canvas and context
Draw a simple moving shape
Use requestAnimationFrame for smooth animation
Add interactivity or timing control
Home Web Front-end H5 Tutorial How to Animate Graphics on an HTML5 Canvas? (A Simple Example)

How to Animate Graphics on an HTML5 Canvas? (A Simple Example)

Dec 31, 2025 am 04:25 AM

Use requestAnimationFrame() to implement HTML5 Canvas animation: first obtain the canvas and 2D context, and then loop to clear, update properties, and redraw; you can add collision detection, user interaction, and timing control.

How to Animate Graphics on an HTML5 Canvas? (A Simple Example)

To animate graphics on an HTML5 <canvas></canvas> , you repeatedly clear the canvas, update object properties (like position or color), and redraw—typically using requestAnimationFrame() for smooth, efficient frame timing.

Set up the canvas and context

Start by getting a reference to the canvas element and its 2D rendering context. This is your drawing surface and toolset.

  • Use document.getElementById() or querySelector() to grab the canvas
  • Call getContext('2d') to enable drawing commands
  • Optionally set canvas width/height explicitly (avoid CSS scaling—it blurs graphics)

Draw a simple moving shape

For example, animate a red circle moving across the screen:

  • Define variables like x , y , and dx (horizontal speed)
  • In each frame: clear the canvas with clearRect() , update x = dx , then draw the circle with arc() and fill()
  • Bounce it by reversing dx when x hits the left or right edge

Use requestAnimationFrame for smooth animation

Instead of setTimeout or setInterval , use requestAnimationFrame(callback) . It syncs with the browser's refresh rate (~60fps) and pauses when the tab is inactive.

  • Call it at the end of your draw function to schedule the next frame
  • Pass the same function recursively: function animate() { /* draw logic */ requestAnimationFrame(animate); }
  • Start it once with animate() — no loops needed

Add interactivity or timing control

You can make animation respond to user input or run for a set duration:

  • Listen for keydown to change speed or direction
  • Use performance.now() to track elapsed time for easing, delays, or state changes
  • Stop animation by skipping the next requestAnimationFrame call — no need to “cancel” it

The above is the detailed content of How to Animate Graphics on an HTML5 Canvas? (A Simple Example). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Popular tool

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to center an image vertically in HTML5? (Layout techniques) How to center an image vertically in HTML5? (Layout techniques) Mar 07, 2026 am 02:05 AM

Flexbox is the most stable for centered images. The key is to set display:flex and align-items:center in the parent container and specify the height; using place-items:center for Grid is more concise; absolute positioning requires top:50% with transform:translateY(-50%); vertical-align is invalid for block-level centering.

How to use SVG graphics directly in HTML5? (Inline SVG) How to use SVG graphics directly in HTML5? (Inline SVG) Mar 07, 2026 am 01:40 AM

SVG tags can be written directly into HTML without any external reference. The core of InlineSVG is to use it as an ordinary HTML element. The browser supports it natively. It does not require additional loading, does not trigger HTTP requests, and can be directly controlled by CSS and JS. A common mistake is to insert it as an image - this way you lose the advantage of inlining, the style cannot penetrate, and JS cannot get inside. Directly copy the SVG source code (exported from Figma, or handwritten), and paste it into an HTML file or any container. Make sure the beginning is, the end is, and there is no DOCTYPE in the middle. Delete useless attributes such as xmlns="http://www.

Related articles