Skip to content

Instantly share code, notes, and snippets.

@worldoptimizer
Last active December 31, 2019 21:44
Show Gist options
  • Save worldoptimizer/427244f6cd6126ce2396fdde2e3cb697 to your computer and use it in GitHub Desktop.
Save worldoptimizer/427244f6cd6126ce2396fdde2e3cb697 to your computer and use it in GitHub Desktop.
// Lower value increases trail duration.
const CANVAS_CLEANUP_ALPHA = 0.3;
// Cleans up the canvas by removing older trails.
// ...
function cleanCanvas() {
// Set 'destination-out' composite mode, so additional fill doesn't remove non-overlapping content.
context.globalCompositeOperation = 'destination-out';
// Set alpha level of content to remove.
// Lower value means trails remain on screen longer.
context.fillStyle = `rgba(0, 0, 0, ${CANVAS_CLEANUP_ALPHA})`;
// Fill entire canvas.
context.fillRect(0, 0, canvas.width, canvas.height);
// Reset composite mode to 'lighter', so overlapping particles brighten each other.
context.globalCompositeOperation = 'lighter';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment