Skip to content

Instantly share code, notes, and snippets.

@frkosk
Created April 11, 2018 14:56
Show Gist options
  • Select an option

  • Save frkosk/20aa96acfcb2124d55a427e1eb0acf86 to your computer and use it in GitHub Desktop.

Select an option

Save frkosk/20aa96acfcb2124d55a427e1eb0acf86 to your computer and use it in GitHub Desktop.
Canvas Random Circle filled with gradient generator
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title Page</title>
</head>
<body>
<canvas id="canvas"></canvas>
</body>
</html>
let c = document.getElementById('canvas');
let ctx = c.getContext('2d');
let i = 0;
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;
ctx.canvas.style.position = 'fixed';
let min = (canvas.width < canvas.height) ? canvas.width : canvas.height;
function getRandomColor() {
let letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function drawCircle() {
i++;
let seed1 = Math.floor(Math.random() * 40) + 37;
let seed2 = Math.floor(Math.random() * canvas.height);
let seed3 = Math.floor(Math.random() * canvas.width);
let primaryColor = getRandomColor();
let secondaryColor = getRandomColor();
let arc = {
x: seed3 - (seed1 / 2),
y: seed2 - (seed1 / 2),
r: seed1
};
ctx.beginPath();
//Create gradient
let gradient = ctx.createLinearGradient((seed3 - 150), arc.x, (seed3 + seed1), arc.x);
gradient.addColorStop(0, primaryColor);
gradient.addColorStop(1, secondaryColor);
ctx.fillStyle = gradient;
ctx.arc(arc.x, arc.y, arc.r, Math.PI * 0, Math.PI * 2);
ctx.fill();
if (i < 6) {
setTimeout(function(){
drawCircle();
}, 300);
}
}
drawCircle();
body {
width: 100%;
height: 100vh;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment