skycons.js 18 KB

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