skycons.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. (function(global) {
  2. "use strict";
  3. /* Set up a RequestAnimationFrame shim so we can animate efficiently FOR
  4. * GREAT JUSTICE. */
  5. var requestInterval, cancelInterval;
  6. (function() {
  7. var raf = global.requestAnimationFrame ||
  8. global.webkitRequestAnimationFrame ||
  9. global.mozRequestAnimationFrame ||
  10. global.oRequestAnimationFrame ||
  11. global.msRequestAnimationFrame ,
  12. caf = global.cancelAnimationFrame ||
  13. global.webkitCancelAnimationFrame ||
  14. global.mozCancelAnimationFrame ||
  15. global.oCancelAnimationFrame ||
  16. global.msCancelAnimationFrame ;
  17. if(raf && caf) {
  18. requestInterval = function(fn, delay) {
  19. var handle = {value: null};
  20. function loop() {
  21. handle.value = raf(loop);
  22. fn();
  23. }
  24. loop();
  25. return handle;
  26. };
  27. cancelInterval = function(handle) {
  28. caf(handle.value);
  29. };
  30. }
  31. else {
  32. requestInterval = setInterval;
  33. cancelInterval = clearInterval;
  34. }
  35. }());
  36. /* Catmull-rom spline stuffs. */
  37. /*
  38. function upsample(n, spline) {
  39. var polyline = [],
  40. len = spline.length,
  41. bx = spline[0],
  42. by = spline[1],
  43. cx = spline[2],
  44. cy = spline[3],
  45. dx = spline[4],
  46. dy = spline[5],
  47. i, j, ax, ay, px, qx, rx, sx, py, qy, ry, sy, t;
  48. for(i = 6; i !== spline.length; i += 2) {
  49. ax = bx;
  50. bx = cx;
  51. cx = dx;
  52. dx = spline[i ];
  53. px = -0.5 * ax + 1.5 * bx - 1.5 * cx + 0.5 * dx;
  54. qx = ax - 2.5 * bx + 2.0 * cx - 0.5 * dx;
  55. rx = -0.5 * ax + 0.5 * cx ;
  56. sx = bx ;
  57. ay = by;
  58. by = cy;
  59. cy = dy;
  60. dy = spline[i + 1];
  61. py = -0.5 * ay + 1.5 * by - 1.5 * cy + 0.5 * dy;
  62. qy = ay - 2.5 * by + 2.0 * cy - 0.5 * dy;
  63. ry = -0.5 * ay + 0.5 * cy ;
  64. sy = by ;
  65. for(j = 0; j !== n; ++j) {
  66. t = j / n;
  67. polyline.push(
  68. ((px * t + qx) * t + rx) * t + sx,
  69. ((py * t + qy) * t + ry) * t + sy
  70. );
  71. }
  72. }
  73. polyline.push(
  74. px + qx + rx + sx,
  75. py + qy + ry + sy
  76. );
  77. return polyline;
  78. }
  79. function downsample(n, polyline) {
  80. var len = 0,
  81. i, dx, dy;
  82. for(i = 2; i !== polyline.length; i += 2) {
  83. dx = polyline[i ] - polyline[i - 2];
  84. dy = polyline[i + 1] - polyline[i - 1];
  85. len += Math.sqrt(dx * dx + dy * dy);
  86. }
  87. len /= n;
  88. var small = [],
  89. target = len,
  90. min = 0,
  91. max, t;
  92. small.push(polyline[0], polyline[1]);
  93. for(i = 2; i !== polyline.length; i += 2) {
  94. dx = polyline[i ] - polyline[i - 2];
  95. dy = polyline[i + 1] - polyline[i - 1];
  96. max = min + Math.sqrt(dx * dx + dy * dy);
  97. if(max > target) {
  98. t = (target - min) / (max - min);
  99. small.push(
  100. polyline[i - 2] + dx * t,
  101. polyline[i - 1] + dy * t
  102. );
  103. target += len;
  104. }
  105. min = max;
  106. }
  107. small.push(polyline[polyline.length - 2], polyline[polyline.length - 1]);
  108. return small;
  109. }
  110. */
  111. /* Define skycon things. */
  112. /* FIXME: I'm *really really* sorry that this code is so gross. Really, I am.
  113. * I'll try to clean it up eventually! Promise! */
  114. var KEYFRAME = 500,
  115. STROKE = 0.08,
  116. TAU = 2.0 * Math.PI,
  117. TWO_OVER_SQRT_2 = 2.0 / Math.sqrt(2);
  118. function circle(ctx, x, y, r) {
  119. ctx.beginPath();
  120. ctx.arc(x, y, r, 0, TAU, false);
  121. ctx.fill();
  122. }
  123. function line(ctx, ax, ay, bx, by) {
  124. ctx.beginPath();
  125. ctx.moveTo(ax, ay);
  126. ctx.lineTo(bx, by);
  127. ctx.stroke();
  128. }
  129. function puff(ctx, t, cx, cy, rx, ry, rmin, rmax) {
  130. var c = Math.cos(t * TAU),
  131. s = Math.sin(t * TAU);
  132. rmax -= rmin;
  133. circle(
  134. ctx,
  135. cx - s * rx,
  136. cy + c * ry + rmax * 0.5,
  137. rmin + (1 - c * 0.5) * rmax
  138. );
  139. }
  140. function puffs(ctx, t, cx, cy, rx, ry, rmin, rmax) {
  141. var i;
  142. for(i = 5; i--; )
  143. puff(ctx, t + i / 5, cx, cy, rx, ry, rmin, rmax);
  144. }
  145. function cloud(ctx, t, cx, cy, cw, s, color) {
  146. t /= 30000;
  147. var a = cw * 0.21,
  148. b = cw * 0.12,
  149. c = cw * 0.24,
  150. d = cw * 0.28;
  151. ctx.fillStyle = color;
  152. puffs(ctx, t, cx, cy, a, b, c, d);
  153. ctx.globalCompositeOperation = 'destination-out';
  154. puffs(ctx, t, cx, cy, a, b, c - s, d - s);
  155. ctx.globalCompositeOperation = 'source-over';
  156. }
  157. function sun(ctx, t, cx, cy, cw, s, color) {
  158. t /= 120000;
  159. var a = cw * 0.25 - s * 0.5,
  160. b = cw * 0.32 + s * 0.5,
  161. c = cw * 0.50 - s * 0.5,
  162. i, p, cos, sin;
  163. ctx.strokeStyle = color;
  164. ctx.lineWidth = s;
  165. ctx.lineCap = "round";
  166. ctx.lineJoin = "round";
  167. ctx.beginPath();
  168. ctx.arc(cx, cy, a, 0, TAU, false);
  169. ctx.stroke();
  170. for(i = 8; i--; ) {
  171. p = (t + i / 8) * TAU;
  172. cos = Math.cos(p);
  173. sin = Math.sin(p);
  174. line(ctx, cx + cos * b, cy + sin * b, cx + cos * c, cy + sin * c);
  175. }
  176. }
  177. function moon(ctx, t, cx, cy, cw, s, color) {
  178. t /= 15000;
  179. var a = cw * 0.29 - s * 0.5,
  180. b = cw * 0.05,
  181. c = Math.cos(t * TAU),
  182. p = c * TAU / -16;
  183. ctx.strokeStyle = color;
  184. ctx.lineWidth = s;
  185. ctx.lineCap = "round";
  186. ctx.lineJoin = "round";
  187. cx += c * b;
  188. ctx.beginPath();
  189. ctx.arc(cx, cy, a, p + TAU / 8, p + TAU * 7 / 8, false);
  190. ctx.arc(cx + Math.cos(p) * a * TWO_OVER_SQRT_2, cy + Math.sin(p) * a * TWO_OVER_SQRT_2, a, p + TAU * 5 / 8, p + TAU * 3 / 8, true);
  191. ctx.closePath();
  192. ctx.stroke();
  193. }
  194. function rain(ctx, t, cx, cy, cw, s, color) {
  195. t /= 1350;
  196. var a = cw * 0.16,
  197. b = TAU * 11 / 12,
  198. c = TAU * 7 / 12,
  199. i, p, x, y;
  200. ctx.fillStyle = color;
  201. for(i = 4; i--; ) {
  202. p = (t + i / 4) % 1;
  203. x = cx + ((i - 1.5) / 1.5) * (i === 1 || i === 2 ? -1 : 1) * a;
  204. y = cy + p * p * cw;
  205. ctx.beginPath();
  206. ctx.moveTo(x, y - s * 1.5);
  207. ctx.arc(x, y, s * 0.75, b, c, false);
  208. ctx.fill();
  209. }
  210. }
  211. function sleet(ctx, t, cx, cy, cw, s, color) {
  212. t /= 750;
  213. var a = cw * 0.1875,
  214. b = TAU * 11 / 12,
  215. c = TAU * 7 / 12,
  216. i, p, x, y;
  217. ctx.strokeStyle = color;
  218. ctx.lineWidth = s * 0.5;
  219. ctx.lineCap = "round";
  220. ctx.lineJoin = "round";
  221. for(i = 4; i--; ) {
  222. p = (t + i / 4) % 1;
  223. x = Math.floor(cx + ((i - 1.5) / 1.5) * (i === 1 || i === 2 ? -1 : 1) * a) + 0.5;
  224. y = cy + p * cw;
  225. line(ctx, x, y - s * 1.5, x, y + s * 1.5);
  226. }
  227. }
  228. function snow(ctx, t, cx, cy, cw, s, color) {
  229. t /= 3000;
  230. var a = cw * 0.16,
  231. b = s * 0.75,
  232. u = t * TAU * 0.7,
  233. ux = Math.cos(u) * b,
  234. uy = Math.sin(u) * b,
  235. v = u + TAU / 3,
  236. vx = Math.cos(v) * b,
  237. vy = Math.sin(v) * b,
  238. w = u + TAU * 2 / 3,
  239. wx = Math.cos(w) * b,
  240. wy = Math.sin(w) * b,
  241. i, p, x, y;
  242. ctx.strokeStyle = color;
  243. ctx.lineWidth = s * 0.5;
  244. ctx.lineCap = "round";
  245. ctx.lineJoin = "round";
  246. for(i = 4; i--; ) {
  247. p = (t + i / 4) % 1;
  248. x = cx + Math.sin((p + i / 4) * TAU) * a;
  249. y = cy + p * cw;
  250. line(ctx, x - ux, y - uy, x + ux, y + uy);
  251. line(ctx, x - vx, y - vy, x + vx, y + vy);
  252. line(ctx, x - wx, y - wy, x + wx, y + wy);
  253. }
  254. }
  255. function fogbank(ctx, t, cx, cy, cw, s, color) {
  256. t /= 30000;
  257. var a = cw * 0.21,
  258. b = cw * 0.06,
  259. c = cw * 0.21,
  260. d = cw * 0.28;
  261. ctx.fillStyle = color;
  262. puffs(ctx, t, cx, cy, a, b, c, d);
  263. ctx.globalCompositeOperation = 'destination-out';
  264. puffs(ctx, t, cx, cy, a, b, c - s, d - s);
  265. ctx.globalCompositeOperation = 'source-over';
  266. }
  267. /*
  268. var WIND_PATHS = [
  269. downsample(63, upsample(8, [
  270. -1.00, -0.28,
  271. -0.75, -0.18,
  272. -0.50, 0.12,
  273. -0.20, 0.12,
  274. -0.04, -0.04,
  275. -0.07, -0.18,
  276. -0.19, -0.18,
  277. -0.23, -0.05,
  278. -0.12, 0.11,
  279. 0.02, 0.16,
  280. 0.20, 0.15,
  281. 0.50, 0.07,
  282. 0.75, 0.18,
  283. 1.00, 0.28
  284. ])),
  285. downsample(31, upsample(16, [
  286. -1.00, -0.10,
  287. -0.75, 0.00,
  288. -0.50, 0.10,
  289. -0.25, 0.14,
  290. 0.00, 0.10,
  291. 0.25, 0.00,
  292. 0.50, -0.10,
  293. 0.75, -0.14,
  294. 1.00, -0.10
  295. ]))
  296. ];
  297. */
  298. var WIND_PATHS = [
  299. [
  300. -0.7500, -0.1800, -0.7219, -0.1527, -0.6971, -0.1225,
  301. -0.6739, -0.0910, -0.6516, -0.0588, -0.6298, -0.0262,
  302. -0.6083, 0.0065, -0.5868, 0.0396, -0.5643, 0.0731,
  303. -0.5372, 0.1041, -0.5033, 0.1259, -0.4662, 0.1406,
  304. -0.4275, 0.1493, -0.3881, 0.1530, -0.3487, 0.1526,
  305. -0.3095, 0.1488, -0.2708, 0.1421, -0.2319, 0.1342,
  306. -0.1943, 0.1217, -0.1600, 0.1025, -0.1290, 0.0785,
  307. -0.1012, 0.0509, -0.0764, 0.0206, -0.0547, -0.0120,
  308. -0.0378, -0.0472, -0.0324, -0.0857, -0.0389, -0.1241,
  309. -0.0546, -0.1599, -0.0814, -0.1876, -0.1193, -0.1964,
  310. -0.1582, -0.1935, -0.1931, -0.1769, -0.2157, -0.1453,
  311. -0.2290, -0.1085, -0.2327, -0.0697, -0.2240, -0.0317,
  312. -0.2064, 0.0033, -0.1853, 0.0362, -0.1613, 0.0672,
  313. -0.1350, 0.0961, -0.1051, 0.1213, -0.0706, 0.1397,
  314. -0.0332, 0.1512, 0.0053, 0.1580, 0.0442, 0.1624,
  315. 0.0833, 0.1636, 0.1224, 0.1615, 0.1613, 0.1565,
  316. 0.1999, 0.1500, 0.2378, 0.1402, 0.2749, 0.1279,
  317. 0.3118, 0.1147, 0.3487, 0.1015, 0.3858, 0.0892,
  318. 0.4236, 0.0787, 0.4621, 0.0715, 0.5012, 0.0702,
  319. 0.5398, 0.0766, 0.5768, 0.0890, 0.6123, 0.1055,
  320. 0.6466, 0.1244, 0.6805, 0.1440, 0.7147, 0.1630,
  321. 0.7500, 0.1800
  322. ],
  323. [
  324. -0.7500, 0.0000, -0.7033, 0.0195, -0.6569, 0.0399,
  325. -0.6104, 0.0600, -0.5634, 0.0789, -0.5155, 0.0954,
  326. -0.4667, 0.1089, -0.4174, 0.1206, -0.3676, 0.1299,
  327. -0.3174, 0.1365, -0.2669, 0.1398, -0.2162, 0.1391,
  328. -0.1658, 0.1347, -0.1157, 0.1271, -0.0661, 0.1169,
  329. -0.0170, 0.1046, 0.0316, 0.0903, 0.0791, 0.0728,
  330. 0.1259, 0.0534, 0.1723, 0.0331, 0.2188, 0.0129,
  331. 0.2656, -0.0064, 0.3122, -0.0263, 0.3586, -0.0466,
  332. 0.4052, -0.0665, 0.4525, -0.0847, 0.5007, -0.1002,
  333. 0.5497, -0.1130, 0.5991, -0.1240, 0.6491, -0.1325,
  334. 0.6994, -0.1380, 0.7500, -0.1400
  335. ]
  336. ],
  337. WIND_OFFSETS = [
  338. {start: 0.36, end: 0.11},
  339. {start: 0.56, end: 0.16}
  340. ];
  341. function leaf(ctx, t, x, y, cw, s, color) {
  342. var a = cw / 8,
  343. b = a / 3,
  344. c = 2 * b,
  345. d = (t % 1) * TAU,
  346. e = Math.cos(d),
  347. f = Math.sin(d);
  348. ctx.fillStyle = color;
  349. ctx.strokeStyle = color;
  350. ctx.lineWidth = s;
  351. ctx.lineCap = "round";
  352. ctx.lineJoin = "round";
  353. ctx.beginPath();
  354. ctx.arc(x , y , a, d , d + Math.PI, false);
  355. ctx.arc(x - b * e, y - b * f, c, d + Math.PI, d , false);
  356. ctx.arc(x + c * e, y + c * f, b, d + Math.PI, d , true );
  357. ctx.globalCompositeOperation = 'destination-out';
  358. ctx.fill();
  359. ctx.globalCompositeOperation = 'source-over';
  360. ctx.stroke();
  361. }
  362. function swoosh(ctx, t, cx, cy, cw, s, index, total, color) {
  363. t /= 2500;
  364. var path = WIND_PATHS[index],
  365. a = (t + index - WIND_OFFSETS[index].start) % total,
  366. c = (t + index - WIND_OFFSETS[index].end ) % total,
  367. e = (t + index ) % total,
  368. b, d, f, i;
  369. ctx.strokeStyle = color;
  370. ctx.lineWidth = s;
  371. ctx.lineCap = "round";
  372. ctx.lineJoin = "round";
  373. if(a < 1) {
  374. ctx.beginPath();
  375. a *= path.length / 2 - 1;
  376. b = Math.floor(a);
  377. a -= b;
  378. b *= 2;
  379. b += 2;
  380. ctx.moveTo(
  381. cx + (path[b - 2] * (1 - a) + path[b ] * a) * cw,
  382. cy + (path[b - 1] * (1 - a) + path[b + 1] * a) * cw
  383. );
  384. if(c < 1) {
  385. c *= path.length / 2 - 1;
  386. d = Math.floor(c);
  387. c -= d;
  388. d *= 2;
  389. d += 2;
  390. for(i = b; i !== d; i += 2)
  391. ctx.lineTo(cx + path[i] * cw, cy + path[i + 1] * cw);
  392. ctx.lineTo(
  393. cx + (path[d - 2] * (1 - c) + path[d ] * c) * cw,
  394. cy + (path[d - 1] * (1 - c) + path[d + 1] * c) * cw
  395. );
  396. }
  397. else
  398. for(i = b; i !== path.length; i += 2)
  399. ctx.lineTo(cx + path[i] * cw, cy + path[i + 1] * cw);
  400. ctx.stroke();
  401. }
  402. else if(c < 1) {
  403. ctx.beginPath();
  404. c *= path.length / 2 - 1;
  405. d = Math.floor(c);
  406. c -= d;
  407. d *= 2;
  408. d += 2;
  409. ctx.moveTo(cx + path[0] * cw, cy + path[1] * cw);
  410. for(i = 2; i !== d; i += 2)
  411. ctx.lineTo(cx + path[i] * cw, cy + path[i + 1] * cw);
  412. ctx.lineTo(
  413. cx + (path[d - 2] * (1 - c) + path[d ] * c) * cw,
  414. cy + (path[d - 1] * (1 - c) + path[d + 1] * c) * cw
  415. );
  416. ctx.stroke();
  417. }
  418. if(e < 1) {
  419. e *= path.length / 2 - 1;
  420. f = Math.floor(e);
  421. e -= f;
  422. f *= 2;
  423. f += 2;
  424. leaf(
  425. ctx,
  426. t,
  427. cx + (path[f - 2] * (1 - e) + path[f ] * e) * cw,
  428. cy + (path[f - 1] * (1 - e) + path[f + 1] * e) * cw,
  429. cw,
  430. s,
  431. color
  432. );
  433. }
  434. }
  435. var Skycons = function(opts) {
  436. this.list = [];
  437. this.interval = null;
  438. this.color = opts && opts.color ? opts.color : "black";
  439. this.resizeClear = !!(opts && opts.resizeClear);
  440. };
  441. Skycons.CLEAR_DAY = function(ctx, t, color) {
  442. var w = ctx.canvas.width,
  443. h = ctx.canvas.height,
  444. s = Math.min(w, h);
  445. sun(ctx, t, w * 0.5, h * 0.5, s, s * STROKE, color);
  446. };
  447. Skycons.CLEAR_NIGHT = function(ctx, t, color) {
  448. var w = ctx.canvas.width,
  449. h = ctx.canvas.height,
  450. s = Math.min(w, h);
  451. moon(ctx, t, w * 0.5, h * 0.5, s, s * STROKE, color);
  452. };
  453. Skycons.PARTLY_CLOUDY_DAY = function(ctx, t, color) {
  454. var w = ctx.canvas.width,
  455. h = ctx.canvas.height,
  456. s = Math.min(w, h);
  457. sun(ctx, t, w * 0.625, h * 0.375, s * 0.75, s * STROKE, color);
  458. cloud(ctx, t, w * 0.375, h * 0.625, s * 0.75, s * STROKE, color);
  459. };
  460. Skycons.PARTLY_CLOUDY_NIGHT = function(ctx, t, color) {
  461. var w = ctx.canvas.width,
  462. h = ctx.canvas.height,
  463. s = Math.min(w, h);
  464. moon(ctx, t, w * 0.667, h * 0.375, s * 0.75, s * STROKE, color);
  465. cloud(ctx, t, w * 0.375, h * 0.625, s * 0.75, s * STROKE, color);
  466. };
  467. Skycons.CLOUDY = function(ctx, t, color) {
  468. var w = ctx.canvas.width,
  469. h = ctx.canvas.height,
  470. s = Math.min(w, h);
  471. cloud(ctx, t, w * 0.5, h * 0.5, s, s * STROKE, color);
  472. };
  473. Skycons.RAIN = function(ctx, t, color) {
  474. var w = ctx.canvas.width,
  475. h = ctx.canvas.height,
  476. s = Math.min(w, h);
  477. rain(ctx, t, w * 0.5, h * 0.37, s * 0.9, s * STROKE, color);
  478. cloud(ctx, t, w * 0.5, h * 0.37, s * 0.9, s * STROKE, color);
  479. };
  480. Skycons.SLEET = function(ctx, t, color) {
  481. var w = ctx.canvas.width,
  482. h = ctx.canvas.height,
  483. s = Math.min(w, h);
  484. sleet(ctx, t, w * 0.5, h * 0.37, s * 0.9, s * STROKE, color);
  485. cloud(ctx, t, w * 0.5, h * 0.37, s * 0.9, s * STROKE, color);
  486. };
  487. Skycons.SNOW = function(ctx, t, color) {
  488. var w = ctx.canvas.width,
  489. h = ctx.canvas.height,
  490. s = Math.min(w, h);
  491. snow(ctx, t, w * 0.5, h * 0.37, s * 0.9, s * STROKE, color);
  492. cloud(ctx, t, w * 0.5, h * 0.37, s * 0.9, s * STROKE, color);
  493. };
  494. Skycons.WIND = function(ctx, t, color) {
  495. var w = ctx.canvas.width,
  496. h = ctx.canvas.height,
  497. s = Math.min(w, h);
  498. swoosh(ctx, t, w * 0.5, h * 0.5, s, s * STROKE, 0, 2, color);
  499. swoosh(ctx, t, w * 0.5, h * 0.5, s, s * STROKE, 1, 2, color);
  500. };
  501. Skycons.FOG = function(ctx, t, color) {
  502. var w = ctx.canvas.width,
  503. h = ctx.canvas.height,
  504. s = Math.min(w, h),
  505. k = s * STROKE;
  506. fogbank(ctx, t, w * 0.5, h * 0.32, s * 0.75, k, color);
  507. t /= 5000;
  508. var a = Math.cos((t ) * TAU) * s * 0.02,
  509. b = Math.cos((t + 0.25) * TAU) * s * 0.02,
  510. c = Math.cos((t + 0.50) * TAU) * s * 0.02,
  511. d = Math.cos((t + 0.75) * TAU) * s * 0.02,
  512. n = h * 0.936,
  513. e = Math.floor(n - k * 0.5) + 0.5,
  514. f = Math.floor(n - k * 2.5) + 0.5;
  515. ctx.strokeStyle = color;
  516. ctx.lineWidth = k;
  517. ctx.lineCap = "round";
  518. ctx.lineJoin = "round";
  519. line(ctx, a + w * 0.2 + k * 0.5, e, b + w * 0.8 - k * 0.5, e);
  520. line(ctx, c + w * 0.2 + k * 0.5, f, d + w * 0.8 - k * 0.5, f);
  521. };
  522. Skycons.prototype = {
  523. _determineDrawingFunction: function(draw) {
  524. if(typeof draw === "string")
  525. draw = Skycons[draw.toUpperCase().replace(/-/g, "_")] || null;
  526. return draw;
  527. },
  528. add: function(el, draw) {
  529. var obj;
  530. if(typeof el === "string")
  531. el = document.getElementById(el);
  532. // Does nothing if canvas name doesn't exists
  533. if(el === null)
  534. return;
  535. draw = this._determineDrawingFunction(draw);
  536. // Does nothing if the draw function isn't actually a function
  537. if(typeof draw !== "function")
  538. return;
  539. obj = {
  540. element: el,
  541. context: el.getContext("2d"),
  542. drawing: draw
  543. };
  544. this.list.push(obj);
  545. this.draw(obj, KEYFRAME);
  546. },
  547. set: function(el, draw) {
  548. var i;
  549. if(typeof el === "string")
  550. el = document.getElementById(el);
  551. for(i = this.list.length; i--; )
  552. if(this.list[i].element === el) {
  553. this.list[i].drawing = this._determineDrawingFunction(draw);
  554. this.draw(this.list[i], KEYFRAME);
  555. return;
  556. }
  557. this.add(el, draw);
  558. },
  559. remove: function(el) {
  560. var i;
  561. if(typeof el === "string")
  562. el = document.getElementById(el);
  563. for(i = this.list.length; i--; )
  564. if(this.list[i].element === el) {
  565. this.list.splice(i, 1);
  566. return;
  567. }
  568. },
  569. draw: function(obj, time) {
  570. var canvas = obj.context.canvas;
  571. if(this.resizeClear)
  572. canvas.width = canvas.width;
  573. else
  574. obj.context.clearRect(0, 0, canvas.width, canvas.height);
  575. obj.drawing(obj.context, time, this.color);
  576. },
  577. play: function() {
  578. var self = this;
  579. this.pause();
  580. this.interval = requestInterval(function() {
  581. var now = Date.now(),
  582. i;
  583. for(i = self.list.length; i--; )
  584. self.draw(self.list[i], now);
  585. }, 1000 / 60);
  586. },
  587. pause: function() {
  588. var i;
  589. if(this.interval) {
  590. cancelInterval(this.interval);
  591. this.interval = null;
  592. }
  593. }
  594. };
  595. global.Skycons = Skycons;
  596. }(this));