skycons.js 16 KB

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