Skip to content

Instantly share code, notes, and snippets.

@Dremora
Last active October 1, 2015 01:17
Show Gist options
  • Save Dremora/1892488 to your computer and use it in GitHub Desktop.
Save Dremora/1892488 to your computer and use it in GitHub Desktop.
Fancy circles
var ctx;
var x = 0;
$(function() {
ctx = $('#canvas')[0].getContext("2d");
return setInterval(draw, 10);
});
function draw() {
ctx.clearRect(0, 0, 600, 600);
var x1 = x;
var x2 = x + Math.PI * 2 / 3;
var x3 = x + Math.PI * 4 / 3;
var circleDeviation = 35;
var figureDeviation = 100;
var figureSpeed = x / 3.5;
var figureLocationX = Math.sin(figureSpeed) * figureDeviation;
var figureLocationY = Math.cos(figureSpeed) * figureDeviation;
var radiusDeviation = 29;
var radius = Math.sin(x) * radiusDeviation + 30;
drawCircle(ctx, "rgba(255, 0, 0, .333)",
Math.sin(x1) * circleDeviation + 200 + figureLocationX,
Math.cos(x1) * circleDeviation + 200 + figureLocationY,
radius
);
drawCircle(ctx, "rgba(0, 255, 0, .333)",
Math.sin(x2) * circleDeviation + 200 + figureLocationX,
Math.cos(x2) * circleDeviation + 200 + figureLocationY,
radius
);
drawCircle(ctx, "rgba(0, 0, 255, .333)",
Math.sin(x3) * circleDeviation + 200 + figureLocationX,
Math.cos(x3) * circleDeviation + 200 + figureLocationY,
radius
);
x += 0.1;
}
function drawCircle(ctx, color, x, y, radius) {
with (ctx) {
fillStyle = color;
beginPath();
arc(x, y, radius, 0, Math.PI*2, true);
closePath();
fill();
}
}
<!DOCTYPE html>
<html>
<head>
<title>Fancy circles</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="circles.js"></script>
</head>
<body>
<canvas id="canvas" width="500" height="500"></canvas>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment