Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions zodiac/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Zodiac</title>
<style>
html,
body {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
background-color: #111;
overflow: hidden;
}

#zodiac {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<canvas id="zodiac"></canvas>
<script src="script/zodiac.min.js"></script>
<script>

document.addEventListener('DOMContentLoaded', function () {

new Zodiac('zodiac', { // HTMLCanvasElement or id
direction: [0, -1], // [dirX (-1:left,0:random,1:right),dirX (-1:up,0:random,1:down)]
velocity: [[.1, .2], [.5, 1]], // [[velXmin,velXmax],[velYmin,velYmax]]
bounce: [false, true], //[bounceTopBottom,bounceLeftRight]
parallax: .2, // float [0-1...]; 0 no paralax; 1 move one vw
density: 6000, // px^2
proximity: 50, // px
dotRadius: [1, 5], // float or [float(min),float(max)]
//dotColor: '#aaa',
lineColor: '#666',
lineWidth: 2,
//backgroundColor: 'rgba(20,20,20,.3)', // default transparent; use alpha value for motion blur and ghosting
});

}, false);

</script>
</body>
</html>
112 changes: 112 additions & 0 deletions zodiac/script/zodiac.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
"use static";
var Zodiac = (function () {
function Zodiac(canvas, options) {
if (options === void 0) { options = {}; }
this.options = {
direction: [0, 0],
velocity: [[.1, 0.3], [.1, .3]],
bounce: [true, true],
parallax: .2,
density: 5000,
proximity: 50,
//dotColor: '#aaa',
dotRadius: [1, 5],
lineColor: '#aaa',
lineWidth: 1,
};
var canvas = (typeof canvas == 'string' || canvas instanceof String) ? document.getElementById(canvas) : canvas;
if (canvas.tagName != 'CANVAS')
throw "no canvas";
for (var key in options) {
this.options[key] = options[key];
}
options = this.options;
var ctx = canvas.getContext('2d', { alpha: !options.backgroundColor }), particles = [], tilt = [0, 0], radius = [].concat(options.dotRadius), parallax = options.parallax / 2 / (radius[1] ? (radius[1] - radius[0]) : radius[0]);
var update = function () {
var w = canvas.width, h = canvas.height;
if (options.backgroundColor) {
ctx.fillStyle = options.backgroundColor;
ctx.fillRect(0, 0, w, h);
ctx.fillStyle = options.dotColor;
}
else {
ctx.clearRect(0, 0, w, h);
}
ctx.beginPath();
for (var i = 0, p, x, y; i < particles.length; i++) {
p = particles[i];
/* MOVE */
p[1] += p[3];
p[2] += p[4];
/* POSITION */
if (options.parallax) {
var fac = p[0] * parallax;
p[5] += (tilt[0] * fac - p[5]) / 10;
p[6] += (tilt[1] * fac - p[6]) / 10;
}
x = p[1] + p[5];
y = p[2] + p[6];
if (x < 0 || x > w)
(options.direction[0]) ? (p[1] = ((x + w) % w) - p[5]) : (p[3] = -p[3]);
if (y < 0 || y > h)
(options.direction[1]) ? (p[2] = ((y + h) % h) - p[6]) : (p[4] = -p[4]);
var r = radius[1] ? p[0] : radius[0];
/* DRAW */
ctx.moveTo(x + r, y);
ctx.arc(x, y, r, 0, Math.PI * 2);
for (var j = i - 1; j >= 0; j--) {
var q = particles[j], dx = q[1] - p[1], dy = q[2] - p[2], dist = Math.sqrt((dx * dx) + (dy * dy));
if (dist < options.proximity) {
var x = p[1] + p[5], y = p[2] + p[6], x2 = q[1] + q[5], y2 = q[2] + q[6], a = Math.atan2(y2 - y, x2 - x), r2 = radius[1] ? q[0] : radius[0], cos = Math.cos(a), sin = Math.sin(a);
x += r * cos;
y += r * sin;
x2 -= r2 * cos;
y2 -= r2 * sin;
ctx.moveTo(x, y);
ctx.lineTo(x2, y2);
}
}
}
;
ctx.stroke();
options.dotColor && ctx.fill();
requestAnimationFrame(update);
};
var onMousemove = function (ev) {
tilt[0] = ev.pageX - window.innerWidth / 2;
tilt[1] = ev.pageY - window.innerHeight / 2;
};
var onOrientation = function (ev) {
tilt[0] = Math.min(Math.max(-ev.beta, -30), 30) * (window.innerWidth / 30);
tilt[1] = Math.min(Math.max(-ev.gamma, -30), 30) * (window.innerHeight / 30);
};
var onResize = function () {
var w = canvas.width = canvas.offsetWidth, h = canvas.height = canvas.offsetHeight, v = options.velocity, d = options.direction, random = Math.random;
var num = Math.ceil((w * h) / options.density);
for (var i = particles.length - 1; i >= 0; i--)
if (particles[i][1] > w || particles[i][2] > h)
particles.splice(i, 1);
if (num < particles.length)
particles.splice(num);
while (num > particles.length)
particles.push([
Math.ceil(radius[1] ? (random() * (radius[1] - radius[0]) + radius[0]) : random() * radius[0]),
Math.ceil(random() * w),
Math.ceil(random() * h),
(d[0] || ((random() > .5) ? 1 : -1)) * (random() * (v[0][1] - v[0][0]) + v[0][0]),
(d[1] || ((random() > .5) ? 1 : -1)) * (random() * (v[1][1] - v[1][0]) + v[1][0]),
0,
0
]);
ctx.strokeStyle = options.lineColor;
ctx.fillStyle = options.dotColor;
ctx.lineWidth = options.lineWidth;
};
window.addEventListener('resize', onResize, false);
document.addEventListener('mousemove', onMousemove, false);
window.addEventListener('deviceorientation', onOrientation, false);
onResize();
update();
}
return Zodiac;
})();
1 change: 1 addition & 0 deletions zodiac/script/zodiac.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

161 changes: 161 additions & 0 deletions zodiac/script/zodiac.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
"use static"

class Zodiac {
options: any = {
direction: [0, 0],
velocity: [[.1, 0.3], [.1, .3]],
bounce: [true, true],
parallax: .2,
density: 5000,
proximity: 50,
//dotColor: '#aaa',
dotRadius: [1,5],
lineColor: '#aaa',
lineWidth: 1,
//backgroundColor: 'rgba(20,20,20,.2)',
};

constructor(canvas: any, options: any = {}) {

var canvas = (typeof canvas == 'string' || canvas instanceof String) ?
document.getElementById(canvas) : canvas;

if (canvas.tagName != 'CANVAS') throw "no canvas";

for (var key in options) { this.options[key] = options[key]; }
options = this.options;

var ctx = canvas.getContext('2d', { alpha: !options.backgroundColor }),
particles = [],
tilt = [0, 0],
radius = [].concat(options.dotRadius),
parallax = options.parallax/2 / (radius[1] ? (radius[1] - radius[0]) : radius[0]);

var update = function () {
var w = canvas.width,
h = canvas.height;

if (options.backgroundColor) {
ctx.fillStyle = options.backgroundColor;
ctx.fillRect(0, 0, w, h);
ctx.fillStyle = options.dotColor;
} else {
ctx.clearRect(0, 0, w, h);
}

ctx.beginPath();

for (var i = 0, p, x, y; i < particles.length; i++) {
p = particles[i];

/* MOVE */
p[1] += p[3];
p[2] += p[4];

/* POSITION */
if (options.parallax) {
var fac = p[0] * parallax;
p[5] += (tilt[0] * fac - p[5]) / 10;
p[6] += (tilt[1] * fac - p[6]) / 10;
}

x = p[1] + p[5];
y = p[2] + p[6];

if (x < 0 || x > w)
(options.direction[0]) ? (p[1] = ((x + w) % w) - p[5]) : (p[3] = -p[3]);

if (y < 0 || y > h)
(options.direction[1]) ? (p[2] = ((y + h) % h) - p[6]) : (p[4] = -p[4]);

var r = radius[1] ? p[0] : radius[0];
/* DRAW */
ctx.moveTo(x + r, y);
ctx.arc(x, y, r, 0, Math.PI * 2);

// loop back no double connections
for (var j = i - 1; j >= 0; j--) {
var q = particles[j],
dx = q[1] - p[1],
dy = q[2] - p[2],
dist = Math.sqrt((dx * dx) + (dy * dy));

if (dist < options.proximity) {
var x = p[1] + p[5],
y = p[2] + p[6],
x2 = q[1] + q[5],
y2 = q[2] + q[6],
a = Math.atan2(y2 - y, x2 - x),
r2 = radius[1] ? q[0] : radius[0],
cos = Math.cos(a),
sin = Math.sin(a);

x += r * cos;
y += r * sin;
x2 -= r2 * cos;
y2 -= r2 * sin;

ctx.moveTo(x, y);
ctx.lineTo(x2, y2);
}
}
};
ctx.stroke();
options.dotColor && ctx.fill();

requestAnimationFrame(update);
}


var onMousemove = function (ev?: MouseEvent) {
tilt[0] = ev.pageX - window.innerWidth / 2;
tilt[1] = ev.pageY - window.innerHeight / 2;
}

var onOrientation = function (ev?: any) {
tilt[0] = Math.min(Math.max(-ev.beta, -30), 30) * (window.innerWidth / 30);
tilt[1] = Math.min(Math.max(-ev.gamma, -30), 30) * (window.innerHeight / 30);

}

var onResize = function () {

var w = canvas.width = canvas.offsetWidth,
h = canvas.height = canvas.offsetHeight,
v = options.velocity,
d = options.direction,
random = Math.random;

var num = Math.ceil((w * h) / options.density);

for (var i = particles.length - 1; i >= 0; i--)
if (particles[i][1] > w || particles[i][2] > h)
particles.splice(i, 1);

if (num < particles.length)
particles.splice(num);

while (num > particles.length)
particles.push([
Math.ceil(radius[1] ? (random() * (radius[1] - radius[0]) + radius[0]) : random() * radius[0]), //z
Math.ceil(random() * w), //x
Math.ceil(random() * h), //y
// (random)direction * clamped random velocity
(d[0] || ((random() > .5) ? 1 : -1)) * (random() * (v[0][1] - v[0][0]) + v[0][0]), //vx
(d[1] || ((random() > .5) ? 1 : -1)) * (random() * (v[1][1] - v[1][0]) + v[1][0]), //vy
0, 0 // offset
]);


ctx.strokeStyle = options.lineColor;
ctx.fillStyle = options.dotColor;
ctx.lineWidth = options.lineWidth;
}

window.addEventListener('resize', onResize, false);
document.addEventListener('mousemove', onMousemove, false);
window.addEventListener('deviceorientation', onOrientation, false);
onResize();
update();
}
}