Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
How to fully fade out contents in canvas

This question has been asked twice without the caveat of "Fully Fade Out"

  • Fastest way of fading out entire contents of a canvas to transparency, not other color
  • HTML5: Apply transparency to Canvas after drawing through JavaScript

Both of the accepted answers only partially fade out the contents. They both suggest something like:

// semi functional code, but doesn't fully work
ctx.save();
ctx.globalAlpha = 1;
ctx.globalCompositeOperation = "destination-in";
ctx.fillStyle = "rgba(0, 0, 0, 0.9)";
ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
ctx.restore();

This leaves residue everywhere, never fully fading out anything. See example below:

let canvas = document.getElementsByTagName('canvas')[0];
let ctx = canvas.getContext('2d');
let rand = n => Math.floor(Math.random() * n);
setInterval(() => {
  ctx.beginPath();
  ctx.arc(rand(300), rand(120), rand(60), Math.PI * 2, 0);
  ctx.fillStyle = `rgba(${rand(256)}, ${rand(256)}, ${rand(256)}, 1)`;
  ctx.globalCompositeOperation = 'source-over';
  ctx.fill();
}, 1000);

let fadeOut = () => {
  let fadeAmount = 0.05;
  // Note that the colour here doesn't matter! Only the alpha matters.
  // The colour here is red, but you'll see no red appear
  ctx.fillStyle = `rgba(255, 0, 0, ${1 - fadeAmount})`;
  ctx.globalCompositeOperation = 'destination-in';
  ctx.fillRect(0, 0, 300, 120);
  requestAnimationFrame(fadeOut);
};
requestAnimationFrame(fadeOut);
canvas { border: 3px solid #808080; background-color: #000000; }
<canvas width="300" height="120"></canvas>


The question is: How can one fully fade out elements on a canvas, all the way to transparent?


EDIT: I'm searching for a performant solution that works for heavily layered (think visualizer) situations.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Here is a small sample using globalAlpha, looks good to me, no residue...
each FadingCircle will have own fade, that will determine how fast it fades and if it goes to 0 or below we do not draw it, seems like an easy solution.
You can add colors, random positions and change it as much as you like to suit your needs.

const canvas = document.getElementsByTagName('canvas')[0];
const ctx = canvas.getContext('2d');

class FadingCircle {
  constructor(x, y, radius, fade) {
    this.x = x
    this.y = y
    this.radius = radius
    this.fade = fade
    this.globalAlpha = 1
  }
  draw(ctx) {
    if (this.globalAlpha > 0) {
      ctx.beginPath()
      ctx.globalAlpha = this.globalAlpha
      ctx.arc(this.x, this.y, this.radius, Math.PI * 2, 0)
      ctx.fill()
      this.globalAlpha -= this.fade
    }
  }
}

let sml = new FadingCircle(40, 50, 20, 0.01)
let med = new FadingCircle(140, 50, 30, 0)
let big = new FadingCircle(100, 50, 50, 0.005)

let animation = () => {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  big.draw(ctx)
  med.draw(ctx)
  sml.draw(ctx)
  requestAnimationFrame(animation);
};
requestAnimationFrame(animation);
<canvas width="300" height="120"></canvas>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!