Canvas animations are one of the most powerful ways to create dynamic, interactive visuals in the browser. From games and particle effects to complex data visualizations, HTML5 canvas is widely used by developers. But if you’ve ever built one, you’ve probably encountered a common issue: canvas flickering.
In this article, we’ll explore how to fix flickering canvas animation in JavaScript. We’ll walk through why flickering happens, how to prevent it, and best practices for smooth animations. This guide is ideal for beginners and intermediate developers who want clear, clean solutions.
Let’s dive in!
What Is Canvas Flickering?
Table of Contents
Canvas flickering refers to a visual glitch where the screen appears to flash, shake, or momentarily display artifacts during animations. This usually occurs when frames are not drawn correctly or when canvas elements are not cleared or updated efficiently.
This can ruin the user experience—especially in animations where smooth transitions are crucial.
Why Does Canvas Flickering Happen?
Before we fix the issue, we need to understand why flickering occurs in canvas animations. Here are some common causes:
-
Improper clearing of the canvas before redrawing each frame.
-
Multiple overlapping drawing operations on the same area without clearing.
-
Incorrect frame timing, causing tearing or partial renders.
-
CSS scaling or transformations affecting the rendering.
-
Mixing DOM updates and canvas rendering inefficiently.
All of these contribute to canvas not rendering cleanly, resulting in flickering.
Example of Flickering Canvas Animation
Here’s a basic example of a canvas animation that might flicker:
In this code, the red square moves to the right—but previous frames are never cleared. This causes the red square to smear across the canvas, which may appear as flickering or ghosting.
How to Fix Flickering Canvas Animation in JavaScript
Let’s walk through step-by-step how to fix flickering canvas animation in JavaScript.
1. Clear the Canvas on Every Frame
One of the most common and effective solutions is to clear the canvas before drawing the next frame.
Update your code like this:
By using ctx.clearRect(...)
, you erase the previous frame, ensuring no ghosting or visual artifacts remain. This alone can solve 80% of flickering issues.
2. Use requestAnimationFrame()
Correctly
Many developers make the mistake of using setInterval()
or setTimeout()
for animations. While they can work, they don’t sync with the browser’s refresh rate, which leads to screen tearing or uneven frame rendering.
Always use requestAnimationFrame()
for smooth animations:
requestAnimationFrame()
automatically syncs with the display refresh rate (usually 60fps), making your canvas animations smoother and preventing flickering.
3. Set Canvas CSS Dimensions Correctly
Improper use of CSS width and height can also cause canvas flickering. Your canvas tag might look like this:
This can lead to pixel stretching or blurry/flickering visuals. The fix?
Always keep the CSS and HTML dimensions aligned:
And don’t override width/height using CSS. If you need to scale the canvas, scale the drawing inside the canvas—not the canvas element itself.
4. Avoid Overlapping Draws Without Clearing
Another common flickering issue happens when developers draw overlapping elements without clearing them. Even if it’s the same object (e.g., a bouncing ball), failing to clear the canvas leads to smudged visuals.
Here’s a clean example:
Notice we start with ctx.clearRect()
and then draw the circle from scratch—this is the cleanest method to fix flickering canvas animation in JavaScript.
5. Avoid CSS Transitions on Canvas Elements
Applying CSS animations or transitions on canvas elements can conflict with internal redraws, especially if you’re manipulating canvas size or opacity dynamically.
Avoid doing this:
Instead, control all animations within JavaScript using canvas APIs. This gives you more control and eliminates flickering caused by style transitions.
6. Double Buffering (Advanced Fix)
In graphics programming, double buffering is used to draw on a hidden canvas, then copy the final result to the visible one. This ensures that users never see the canvas during a half-rendered state.
Here’s an example:
Double buffering minimizes screen tearing and flickering, especially useful for high-FPS animations or games.
Bonus: Combine with Debounced Scroll
Sometimes flickering occurs not because of canvas logic but due to other browser events triggering repaints, like scroll.
In that case, it’s smart to debounce scroll events to prevent performance issues. We’ve covered this in detail in our debounce scroll event guide.
Best Practices to Fix Flickering Canvas Animation in JavaScript
To summarize, here are key practices to follow:
✅ Clear the canvas using clearRect()
every frame
✅ Always use requestAnimationFrame()
✅ Match canvas HTML and CSS dimensions
✅ Avoid CSS transitions on <canvas>
✅ Redraw from scratch every frame
✅ Consider double buffering for complex visuals
✅ Optimize related events (scroll, resize)
Following these will eliminate most causes of canvas flickering.
Real World Example: Particle Animation
Let’s apply all these best practices in a real example. We’ll animate a simple particle system that moves without flickering.
This simple particle system is flicker-free, fast, and runs at 60fps because it follows all the techniques mentioned above.
Final Thoughts
Flickering canvas animations can frustrate both developers and users—but the good news is that they’re easily fixable with the right techniques.
In this guide, we explored how to:
-
Diagnose the root cause of canvas flickering
-
Apply
clearRect()
properly -
Use
requestAnimationFrame()
instead ofsetInterval
-
Keep canvas sizes in sync with CSS
-
Optimize redraw logic and reduce overlapping
-
Use double buffering for clean frame swaps
By following these principles, you’ll create smooth, high-performance animations with HTML5 canvas.
Remember, performance issues in web animations often come down to drawing too much, too fast, or inefficiently. With proper logic and best practices, you can easily fix flickering canvas animation in JavaScript.
If you’re also working with scroll-based animations, be sure to check out our guide on how to debounce scroll events, which helps avoid frame drops and visual glitches from scroll-triggered canvas repaints.