PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/petridish.js

https://github.com/aiham/Petri-Dish
JavaScript | 426 lines | 356 code | 68 blank | 2 comment | 45 complexity | d1fefaed41791425e282614b53c93c84 MD5 | raw file
  1. (function (window) {
  2. // JSLint settings
  3. // browser: true, maxerr: 50, indent: 2
  4. 'use strict';
  5. var $ = window.jQuery,
  6. gyudon = window.gyudon,
  7. PetriDish,
  8. randomInt = function (min, max) {
  9. return Math.floor(Math.random() * (max - min + 1)) + min;
  10. },
  11. timeToCoords = function (start) {
  12. var time = start - Math.floor(start / 12) * 12,
  13. angle = time * 30 / 180 * Math.PI;
  14. return new gyudon.Coord(gyudon.Math.cos(angle), gyudon.Math.sin(angle));
  15. };
  16. PetriDish = function (container) {
  17. this.pattern = 'random';
  18. this.circle_style = 'fill';
  19. this.red = 0;
  20. this.blue = 0;
  21. this.green = 0;
  22. this.scale = 0;
  23. this.radius = 248;
  24. this.colour_spacer = 45;
  25. this.c = new gyudon.Manager(500, 500);
  26. container.append(this.c.e);
  27. container = null;
  28. this.c.start();
  29. this.border_circle = new gyudon.Item.Circle({
  30. tag: 'border',
  31. stroke: '#cccccc',
  32. center: new gyudon.Coord(this.c.width / 2, this.c.height / 2),
  33. radius: this.radius
  34. });
  35. this.c.addItem(this.border_circle);
  36. gyudon.Timer.add(this, function (callback) {
  37. this.patterns[this.pattern].call(this, callback);
  38. });
  39. };
  40. PetriDish.prototype = {
  41. constructor: PetriDish,
  42. setPattern: function (pattern) {
  43. gyudon.Timer.stop();
  44. this.c.canvas.clear();
  45. this.c.removeAllItems().
  46. addItem(this.border_circle);
  47. this.pattern = pattern;
  48. this.red = 0;
  49. this.blue = 0;
  50. this.green = 0;
  51. this.scale = 0;
  52. gyudon.Timer.step();
  53. return this;
  54. },
  55. setStyleWireframe: function () {
  56. this.circle_style = 'stroke';
  57. },
  58. setStyleFilled: function () {
  59. this.circle_style = 'fill';
  60. },
  61. pulse: function (options) {
  62. var colour, target, circle, circle_options, key,
  63. defaults = {
  64. time: randomInt(0, 1200) / 100,
  65. red: randomInt(0, 255),
  66. green: randomInt(0, 255),
  67. blue: randomInt(0, 255),
  68. radius: 20,
  69. center: new gyudon.Coord(this.c.width / 2, this.c.height / 2),
  70. speed: 1000
  71. };
  72. options = options || {};
  73. for (key in defaults) {
  74. if (defaults.hasOwnProperty(key) && !options.hasOwnProperty(key)) {
  75. options[key] = defaults[key];
  76. }
  77. }
  78. if (options.speed < 100) {
  79. options.speed = 100;
  80. }
  81. colour = 'rgb(' + options.red + ', ' + options.green + ', ' + options.blue + ')';
  82. target = timeToCoords(options.time);
  83. target.x = target.x * this.radius + this.c.width / 2 - options.radius;
  84. target.y = target.y * this.radius + this.c.height / 2 - options.radius;
  85. circle_options = {
  86. tag: 'circle',
  87. center: options.center,
  88. radius: options.radius,
  89. alpha: 0
  90. };
  91. circle_options[this.circle_style] = colour;
  92. circle = new gyudon.Item.Circle(circle_options);
  93. this.c.addItem(circle);
  94. circle.moveTo(options.speed, target).
  95. fadeIn(options.speed / 2 - 50, function () {
  96. this.fadeOut(options.speed / 2 - 50, function () {
  97. this.removeFromParent().destroy();
  98. });
  99. });
  100. },
  101. patterns: {
  102. random: function () {
  103. this.pulse();
  104. },
  105. full_random: function () {
  106. var z;
  107. for (z = 0; z < 7; z += 1) {
  108. this.pulse();
  109. }
  110. },
  111. small_random: function () {
  112. var z;
  113. for (z = 0; z < 7; z += 1) {
  114. this.pulse({radius: 5});
  115. }
  116. },
  117. red_random: function () {
  118. this.pulse({
  119. red: this.red,
  120. blue: this.blue,
  121. green: this.green
  122. });
  123. this.red += 10;
  124. if (this.red > 255) {
  125. this.red = 0;
  126. }
  127. },
  128. blue_random: function () {
  129. this.pulse({
  130. red: this.red,
  131. blue: this.blue,
  132. green: this.green
  133. });
  134. this.blue += 10;
  135. if (this.blue > 255) {
  136. this.blue = 0;
  137. }
  138. },
  139. green_random: function () {
  140. this.pulse({
  141. red: this.red,
  142. blue: this.blue,
  143. green: this.green
  144. });
  145. this.green += 10;
  146. if (this.green > 255) {
  147. this.green = 0;
  148. }
  149. },
  150. ray_gun: function (callback) {
  151. var z;
  152. if (callback.count % 2 !== 0) {
  153. return;
  154. }
  155. for (z = 0; z < 12; z += 1) {
  156. this.pulse({
  157. time: z,
  158. red: this.red,
  159. blue: this.blue,
  160. green: this.green
  161. });
  162. }
  163. this.red += this.colour_spacer;
  164. if (this.red > 255) {
  165. this.red = 0;
  166. this.blue += this.colour_spacer;
  167. if (this.blue > 255) {
  168. this.blue = 0;
  169. this.green += this.colour_spacer;
  170. if (this.green > 255) {
  171. this.green = 0;
  172. }
  173. }
  174. }
  175. },
  176. slow_ray_gun: function (callback) {
  177. var z;
  178. if (callback.count % 10 !== 0) {
  179. return;
  180. }
  181. for (z = 0; z < 12; z += 1) {
  182. this.pulse({
  183. time: z,
  184. red: this.red,
  185. blue: this.blue,
  186. green: this.green,
  187. speed: 3000
  188. });
  189. }
  190. this.red += this.colour_spacer;
  191. if (this.red > 255) {
  192. this.red = 0;
  193. this.blue += this.colour_spacer;
  194. if (this.blue > 255) {
  195. this.blue = 0;
  196. this.green += this.colour_spacer;
  197. if (this.green > 255) {
  198. this.green = 0;
  199. }
  200. }
  201. }
  202. },
  203. random_ray_gun: function (callback) {
  204. var z;
  205. if (callback.count % 2 !== 0) {
  206. return;
  207. }
  208. for (z = 0; z < 12; z += 1) {
  209. this.pulse({time: z});
  210. }
  211. },
  212. take_over: function (callback) {
  213. var z;
  214. if (callback.count % 10 !== 0) {
  215. return;
  216. }
  217. for (z = 0; z < 12; z += 1) {
  218. this.pulse({
  219. time: z,
  220. red: this.red,
  221. blue: this.blue,
  222. green: this.green,
  223. radius: this.scale * 5,
  224. speed: 3000
  225. });
  226. }
  227. this.scale += 1;
  228. if (this.scale > 50) {
  229. this.scale = 0;
  230. }
  231. this.red += this.colour_spacer + 20;
  232. if (this.red > 255) {
  233. this.red = 0;
  234. this.blue += this.colour_spacer + 20;
  235. if (this.blue > 255) {
  236. this.blue = 0;
  237. this.green += this.colour_spacer + 20;
  238. if (this.green > 255) {
  239. this.green = 0;
  240. }
  241. }
  242. }
  243. },
  244. spiral: function () {
  245. this.pulse({
  246. time: this.scale,
  247. red: this.red,
  248. blue: this.blue,
  249. green: this.green
  250. });
  251. this.scale += 1;
  252. if (this.scale >= 12) {
  253. this.scale = 0;
  254. }
  255. this.red += this.colour_spacer;
  256. if (this.red > 255) {
  257. this.red = 0;
  258. this.blue += this.colour_spacer;
  259. if (this.blue > 255) {
  260. this.blue = 0;
  261. this.green += this.colour_spacer;
  262. if (this.green > 255) {
  263. this.green = 0;
  264. }
  265. }
  266. }
  267. },
  268. random_spiral: function () {
  269. this.pulse({time: this.scale});
  270. this.scale += 1;
  271. if (this.scale >= 12) {
  272. this.scale = 0;
  273. }
  274. },
  275. sprinkler: function (callback) {
  276. var target, z;
  277. if (callback.count % 2 !== 0) {
  278. return;
  279. }
  280. target = timeToCoords(this.scale);
  281. target.x = target.x * (this.radius - 30) + this.c.width / 2;
  282. target.y = target.y * (this.radius - 30) + this.c.height / 2;
  283. for (z = 0; z < 12; z += 1) {
  284. if (z !== this.scale) {
  285. this.pulse({
  286. time: z + 1,
  287. center: new gyudon.Coord(target.x, target.y)
  288. });
  289. }
  290. }
  291. this.scale += 1;
  292. if (this.scale >= 12) {
  293. this.scale = 0;
  294. }
  295. }
  296. }
  297. };
  298. $(window.document).ready(function () {
  299. var petridish, stroke_input, fill_input, menu, circle_type_action,
  300. pattern, name, button, about_button, pattern_setter;
  301. $('.js_only').removeClass('js_only');
  302. petridish = new PetriDish($('#main'));
  303. stroke_input = $('#stroke');
  304. fill_input = $('#fill');
  305. menu = $('#menu');
  306. fill_input.attr('checked', 'checked');
  307. circle_type_action = function () {
  308. if ($('input[@name=circle_type]:checked').attr('id') === 'stroke') {
  309. petridish.setStyleWireframe();
  310. } else {
  311. petridish.setStyleFilled();
  312. }
  313. };
  314. stroke_input.click(circle_type_action);
  315. fill_input.click(circle_type_action);
  316. pattern_setter = function (pattern) {
  317. return function (e) {
  318. petridish.setPattern(pattern);
  319. e.preventDefault();
  320. };
  321. };
  322. for (pattern in petridish.patterns) {
  323. if (petridish.patterns.hasOwnProperty(pattern)) {
  324. name = pattern.replace(/_/g, ' ');
  325. name = name.charAt(0).toUpperCase() + name.slice(1);
  326. menu.append($('<li>').
  327. append(
  328. $('<a>').attr('href', '#').
  329. click(pattern_setter(pattern)).
  330. text(name)
  331. ).
  332. addClass('option'));
  333. }
  334. }
  335. button = $('#menu li.button a');
  336. button.click(function (e) {
  337. if (!button.hasClass('hidden')) {
  338. button.addClass('hidden');
  339. $('#menu li.option').stop(true, true).slideUp('foobar', 'swing');
  340. } else {
  341. button.removeClass('hidden');
  342. $('#menu li.option').stop(true, true).slideDown('foobar', 'swing');
  343. }
  344. e.preventDefault();
  345. });
  346. about_button = $('#about li.button a');
  347. about_button.click(function (e) {
  348. if (!about_button.hasClass('hidden')) {
  349. about_button.addClass('hidden');
  350. $('#about li.info').stop(true, true).slideUp('foobar', 'swing');
  351. } else {
  352. about_button.removeClass('hidden');
  353. $('#about li.info').stop(true, true).slideDown('foobar', 'swing');
  354. }
  355. e.preventDefault();
  356. }).click();
  357. });
  358. }(window));