xonecas.io
212
Ca

canvas-rings / commit f433e85

initial commit

sean2011-10-14 f433e853081e08276756346854f6baa8579adb18 parent c49d917

6 files changed

.gitignore+13−0
@@ -0,0 +1,13 @@
1+node_modules
2+
3+# Mac stuff
4+.DS_Store
5+._*
6+.Spotlight-V100
7+.Trashes
8+
9+# Vim stuff
10+.*.sw[a-z]
11+*.un~
12+Session.vim
13+
favicon.ico+0−0
Binary file, not shown.
index.html+16−0
@@ -0,0 +1,16 @@
1+<!doctype html>
2+<html>
3+ <head>
4+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
5+ <meta charset="utf-8">
6+
7+
8+ <title>rings</title>
9+ <link rel="stylesheet" href="style.css">
10+ </head>
11+
12+ <body>
13+ <script src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
14+ <script defer src="script.js"></script>
15+ </body>
16+</html>
node.js+16−0
@@ -0,0 +1,16 @@
1+var
2+ connect = require('connect'),
3+ server = connect(
4+ connect.profiler(),
5+ connect.logger('dev'),
6+ connect.favicon("./favicon.ico"),
7+ connect['static'](__dirname),
8+ connect.errorHandler({
9+ 'stack': true,
10+ 'message': true,
11+ 'dump': true
12+ })
13+ );
14+
15+server.listen(8080);
16+console.log('OK');
script.js+116−0
@@ -0,0 +1,116 @@
1+/*jshint expr: true, onevar: true */
2+var rings = !function (win, doc) {
3+ var
4+ body = doc.querySelector('body'),
5+ canvas = doc.createElement('canvas'),
6+ c = canvas.getContext('2d'),
7+ width = window.innerWidth,
8+ height = window.innerHeight;
9+
10+ //random range
11+ function _rr(min,max){
12+ return Math.random()*(max-min) + min;
13+ }
14+
15+ //takes the values returns an hsla(...) css string
16+ function _hsla (h, s, l, a) {
17+ h = h || Math.floor(_rr(80, 200));
18+ s = s || 100;
19+ l = l || 50;
20+ a = a || 1.0;
21+
22+ return "hsla("+ h +", "+ s +"%, "+ l +"%, "+ a +")";
23+ }
24+
25+ window._hsla = _hsla;
26+
27+ //takes degrees returns radians
28+ function _rad(deg) {
29+ return deg * (Math.PI /180);
30+ }
31+
32+ function Ring (options) {
33+ options = options || {};
34+
35+ this.radius = 1;
36+ this.max = 1;
37+ this.counter = 0.1;
38+ this.length = Math.floor(_rr(10, 300));
39+ this.x = options.x || Math.floor(width /2);
40+ this.y = options.y || Math.floor(height /2);
41+ this.color = options.color || _hsla();
42+
43+ return this;
44+ }
45+
46+ Ring.prototype = {
47+ update: function () {
48+ if (this.radius < 1) {
49+ this.counter = 0.1;
50+ }
51+
52+ this.radius = this.length * Math.sin(this.counter);
53+ this.counter += 0.01;
54+
55+ if (this.radius < this.max) {
56+ this.destroy = true;
57+ } else {
58+ this.max = this.radius;
59+ }
60+ },
61+
62+ draw: function () {
63+ c.beginPath();
64+ c.arc(this.x, this.y, this.radius, 0, _rad(360), true);
65+ c.strokeStyle = this.color;
66+ c.lineWidth = 2.5;
67+ c.stroke();
68+ c.closePath();
69+ }
70+ };
71+
72+ function init () {
73+ var
74+ rings = [],
75+ hue = 1;
76+
77+ canvas.width = width;
78+ canvas.height = height;
79+ body.appendChild(canvas);
80+ canvas.addEventListener('mousemove', function (ev) {
81+ hue = hue+1 === 360 ? 1 : hue+1;
82+ var ring = new Ring({
83+ x: ev.clientX,
84+ y: ev.clientY,
85+ color: _hsla(hue)
86+ });
87+ rings.push(ring);
88+ console.log(ring);
89+ }, false);
90+
91+ window.rings = rings;
92+
93+ setInterval(function () {
94+ c.save();
95+ c.fillStyle = "hsla(1,1%,0%, 0.1)";
96+ c.fillRect(0, 0, width, height);
97+ c.restore();
98+
99+ if (rings.length === 0) {
100+ return;
101+ }
102+
103+ _.each(rings, function (ring) {
104+ ring.update();
105+ ring.draw();
106+ });
107+
108+ rings = _.reject(rings, function (ring) {
109+ return ring.destroy;
110+ });
111+ }, 1000/60);
112+ }
113+
114+ init();
115+
116+} (window, document);
style.css+58−0
@@ -0,0 +1,58 @@
1+/* http://meyerweb.com/eric/tools/css/reset/
2+ v2.0 | 20110126
3+ License: none (public domain)
4+*/
5+
6+html, body, div, span, applet, object, iframe,
7+h1, h2, h3, h4, h5, h6, p, blockquote, pre,
8+a, abbr, acronym, address, big, cite, code,
9+del, dfn, em, img, ins, kbd, q, s, samp,
10+small, strike, strong, sub, sup, tt, var,
11+b, u, i, center,
12+dl, dt, dd, ol, ul, li,
13+fieldset, form, label, legend,
14+table, caption, tbody, tfoot, thead, tr, th, td,
15+article, aside, canvas, details, embed,
16+figure, figcaption, footer, header, hgroup,
17+menu, nav, output, ruby, section, summary,
18+time, mark, audio, video {
19+ margin: 0;
20+ padding: 0;
21+ border: 0;
22+ font-size: 100%;
23+ font: inherit;
24+ vertical-align: baseline;
25+}
26+/* HTML5 display-role reset for older browsers */
27+article, aside, details, figcaption, figure,
28+footer, header, hgroup, menu, nav, section {
29+ display: block;
30+}
31+body {
32+ line-height: 1;
33+}
34+ol, ul {
35+ list-style: none;
36+}
37+blockquote, q {
38+ quotes: none;
39+}
40+blockquote:before, blockquote:after,
41+q:before, q:after {
42+ content: '';
43+ content: none;
44+}
45+table {
46+ border-collapse: collapse;
47+ border-spacing: 0;
48+}
49+
50+/* my stuff */
51+html, body {
52+ background-color: black;
53+}
54+canvas {
55+ display: block;
56+ margin: 0 auto;
57+ background-color: black;
58+}