Skip to content

Instantly share code, notes, and snippets.

@PurpleSun
Created December 24, 2014 06:36
Show Gist options
  • Save PurpleSun/572b42e5f5fe40ed6f11 to your computer and use it in GitHub Desktop.
Save PurpleSun/572b42e5f5fe40ed6f11 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Dynamic Balls</title>
<style>
* {
margin: 0;
padding: 0;
}
</style>
</head>
<body style="height: 100%">
<canvas id="canvas" style="display: block;">Your browser doesn't support canvas!</canvas>
<script type="application/javascript">
const WINDOW_WIDTH = window.innerWidth;
const WINDOW_HEIGHT = window.innerHeight;
window.onload = function () {
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
canvas.width = WINDOW_WIDTH;
canvas.height = WINDOW_HEIGHT;
context.globalAlpha = 0.7;
// context.globalCompositeOperation = 'lighter';
var balls = [];
for (var i = 0; i < 100; i++) {
var x = getRandomArbitrary(0, WINDOW_WIDTH);
var y = getRandomArbitrary(0, WINDOW_HEIGHT);
var vx = getRandomArbitrary(-10, 10);
var vy = getRandomArbitrary(-10, 10);
var r = getRandomArbitrary(0, 100);
var color = getRandomColor();
var randomBall = {
x: x,
y: y,
vx: vx,
vy: vy,
r: r,
color: color
};
balls.push(randomBall);
}
setInterval(function () {
render(context, balls);
update(context, balls);
}, 50)
};
function render(cxt, balls) {
var canvas = cxt.canvas;
cxt.clearRect(0, 0, canvas.width, canvas.height);
for (var i = 0; i < balls.length; i++) {
drawBall(balls[i].x, balls[i].y, balls[i].r, balls[i].color, cxt);
}
var string = 'Wishing you a Merry Christmas, Dear Miaomiao!';
var x = canvas.width - 20;
var y = canvas.height - 20;
drawText(string, x, y, cxt);
}
function update(cxt, balls) {
var canvas = cxt.canvas;
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
for (var i = 0; i < balls.length; i++) {
var ball = balls[i];
ball.x += ball.vx;
ball.y += ball.vy;
// 左边缘检测
if (ball.x <= ball.r) {
ball.x = ball.r;
ball.vx = -ball.vx;
}
// 右边缘检测
if (ball.x >= (canvasWidth - ball.r)) {
ball.x = canvasWidth - ball.r;
ball.vx = -ball.vx;
}
// 上边缘检测
if (ball.y <= ball.r) {
ball.y = ball.r;
ball.vy = -ball.vy;
}
// 下边缘检测
if (ball.y >= (canvasHeight - ball.r)) {
ball.y = canvasHeight - ball.r;
ball.vy = - ball.vy;
}
}
}
function drawBall(x, y, r, color, cxt) {
cxt.beginPath();
var startAngle = 0;
var endAngle = 2 * Math.PI;
cxt.arc(x, y, r, startAngle, endAngle);
cxt.fillStyle = color;
cxt.fill();
cxt.closePath();
}
function drawText(string, x, y, cxt) {
cxt.save();
cxt.font = '25px Papyrus';
cxt.fillStyle = '#FF36AD';
cxt.textAlign = 'right';
cxt.shadowColor = 'gray';
cxt.shadowOffsetX = 5;
cxt.shadowOffsetY = 5;
cxt.shadowBlur = 10;
cxt.fillText(string, x, y);
cxt.restore();
}
function getRandomColor() {
var color = ['#'];
for (var i = 0; i < 6; i++) {
color.push(parseInt(getRandomArbitrary(0, 17)).toString(16));
}
return color.join('');
}
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment