Latest version of my personal website

magic.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. var Magic = function (options) {
  2. options = options || {};
  3. // Array to hold all active spells being drawn
  4. this.spells = [];
  5. // When on, wait a certain amount of time before casting (for initial
  6. // load-up)
  7. this.counting = options.counting || true;
  8. // The amount of time to wait (in number of animate() calls)
  9. this.waitTime = options.waitTime || 20;
  10. // How fast to draw the spells
  11. this.step = options.step || 0.2;
  12. // Limit of branches per spell at any time
  13. this.prune_to = options.prune_to || 20;
  14. // Max duration possible (in steps) of a spell
  15. this.maxDuration = options.maxDuration || 600;
  16. // Min duration possible (in steps) of a spell
  17. this.minDuration = options.minDuration || 50;
  18. // Max angle deviation during branch split
  19. this.maxAngleChange = options.maxAngleChange || 180;
  20. // Min angle deviation during branch split
  21. this.minAngleChange = options.minAngleChange || 90;
  22. // Average length of new spline (branch)
  23. this.splineLength = options.splineLength || 15;
  24. // Whether new spells are cast with a new random color
  25. this.randomColored = options.randomColored || true;
  26. this.paused = false;
  27. // Save this query here now that the page is loaded
  28. this.canvas = document.getElementById("magic");
  29. };
  30. /* Get the height and width of full document. To avoid browser
  31. * incompatibility issues, choose the maximum of all height/width values.
  32. *
  33. * Method from http://stackoverflow.com/a/1147768 */
  34. Magic.prototype.getDocumentDimensions = function () {
  35. var body = document.body,
  36. html = document.documentElement;
  37. var height = Math.max(body.scrollHeight, body.offsetHeight,
  38. html.clientHeight, html.scrollHeight,
  39. html.offsetHeight);
  40. var width = Math.max(body.scrollWidth, body.offsetWidth,
  41. html.clientWidth, html.scrollWidth,
  42. html.offsetWidth);
  43. return {"height": height, "width": width};
  44. };
  45. /* Specifies what will be added to the red, green, and blue values of the
  46. * color at each interval. */
  47. Magic.prototype.pickGradient = function () {
  48. var directions = [-1, 0, 1];
  49. var magnitudes = [1, 2, 3];
  50. return {"r": directions[Math.floor(Math.random() * 3)] *
  51. magnitudes[Math.floor(Math.random() * 3)],
  52. "g": directions[Math.floor(Math.random() * 3)] *
  53. magnitudes[Math.floor(Math.random() * 3)],
  54. "b": directions[Math.floor(Math.random() * 3)] *
  55. magnitudes[Math.floor(Math.random() * 3)]};
  56. };
  57. /* Alters the given color by the given gradient and returns both.
  58. If the red, green, or blue value of the color is at 0 or 255 and the
  59. gradient for that value is not zero, then the gradient for that value
  60. will change signs.
  61. Unused for now. It turns out that changing the canvas stroke color is a
  62. costly operation and slows things down a lot. */
  63. Magic.prototype.nextColor = function(color, gradient) {
  64. var values = ["r", "g", "b"];
  65. // Check if color at end of range and reverse gradient direction if so
  66. for (var i = 0; i < 3; i++) {
  67. var colorValue = color[values[i]];
  68. var gradientValue = gradient[values[i]];
  69. if ((colorValue === 255 && gradientValue > 0) ||
  70. (colorValue === 0 && gradientValue < 0)) {
  71. gradient[values[i]] = gradientValue * -1;
  72. }
  73. }
  74. // Modify the color according to the gradient
  75. for (i = 0; i < 3; i++) {
  76. color[values[i]] = color[values[i]] + gradient[values[i]];
  77. }
  78. return {"color": color, "gradient": gradient};
  79. };
  80. /* Choose a random RGB color to begin the color gradient with */
  81. Magic.prototype.pickRandomColor = function() {
  82. return {"r": Math.floor(Math.random() * (255 + 1)),
  83. "g": Math.floor(Math.random() * (255 + 1)),
  84. "b": Math.floor(Math.random() * (255 + 1))};
  85. };
  86. Magic.prototype.clear = function() {
  87. this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  88. }
  89. Magic.prototype.drawSplineSegment = function(branch, context) {
  90. var ax = (-branch.points[0].x + 3*branch.points[1].x - 3*branch.points[2].x + branch.points[3].x) / 6;
  91. var ay = (-branch.points[0].y + 3*branch.points[1].y - 3*branch.points[2].y + branch.points[3].y) / 6;
  92. var bx = (branch.points[0].x - 2*branch.points[1].x + branch.points[2].x) / 2;
  93. var by = (branch.points[0].y - 2*branch.points[1].y + branch.points[2].y) / 2;
  94. var cx = (-branch.points[0].x + branch.points[2].x) / 2;
  95. var cy = (-branch.points[0].y + branch.points[2].y) / 2;
  96. var dx = (branch.points[0].x + 4*branch.points[1].x + branch.points[2].x) / 6;
  97. var dy = (branch.points[0].y + 4*branch.points[1].y + branch.points[2].y) / 6;
  98. context.moveTo(
  99. ax*Math.pow(branch.t, 3) + bx*Math.pow(branch.t, 2) + cx*branch.t + dx,
  100. ay*Math.pow(branch.t, 3) + by*Math.pow(branch.t, 2) + cy*branch.t + dy
  101. );
  102. context.lineTo(
  103. ax*Math.pow(branch.t+this.step, 3) + bx*Math.pow(branch.t+this.step, 2) + cx*(branch.t+this.step) + dx,
  104. ay*Math.pow(branch.t+this.step, 3) + by*Math.pow(branch.t+this.step, 2) + cy*(branch.t+this.step) + dy
  105. );
  106. branch.t += this.step;
  107. };
  108. Magic.prototype.splitBranch = function(branch) {
  109. var newBranches = [];
  110. // Replace with 2 new branches
  111. for (var k = 0; k < 2; k++) {
  112. // Generate random deviation from previous angle
  113. var angle = branch.angle - (Math.random() * this.maxAngleChange - this.minAngleChange);
  114. // Generate random length
  115. var length = Math.random() * this.splineLength + 4;
  116. // Calculate new point
  117. var x2 = branch.points[3].x + Math.sin(Math.PI * angle / 180) * length;
  118. var y2 = branch.points[3].y - Math.cos(Math.PI * angle / 180) * length;
  119. // Add to new branch array
  120. newBranches.push({
  121. points:new Array(
  122. branch.points[1],
  123. branch.points[2],
  124. branch.points[3],
  125. {x:x2, y:y2}
  126. ),
  127. angle:angle,
  128. t:0,
  129. });
  130. }
  131. return newBranches;
  132. };
  133. Magic.prototype.cast = function(x, y, angle, chain) {
  134. // Find random position if not defined
  135. if (x === undefined) {
  136. x = Math.floor(Math.random() * (this.canvas.width + 1));
  137. }
  138. if (y === undefined) {
  139. y = Math.floor(Math.random() * (this.canvas.height + 1));
  140. }
  141. if (angle === undefined) {
  142. angle = Math.random() * 360;
  143. }
  144. var colorString = "rgb(10,10,10)";
  145. if (this.randomColored) {
  146. var color = this.pickRandomColor();
  147. colorString = "rgb(" + color.r + "," + color.g + "," + color.b + ")";
  148. }
  149. // Create new spell (branch)
  150. this.spells.push({
  151. branches:new Array({
  152. points:new Array({x:x, y:y}, {x:x, y:y}, {x:x, y:y}, {x:x, y:y}),
  153. angle:angle,
  154. t:0,
  155. }),
  156. color:colorString,
  157. duration:Math.floor(Math.random() * (this.maxDuration - this.minDuration + 1)) + 50,
  158. chain:chain,
  159. });
  160. };
  161. /* Most of this funtion is provided by Maissan Inc. in their tutorial:
  162. http://www.maissan.net/articles/simulating-vines */
  163. Magic.prototype.drawSpells = function(context, sort, prune, chain) {
  164. var AnimationFrame = window.AnimationFrame;
  165. AnimationFrame.shim();
  166. var animationFrame = new AnimationFrame(30),
  167. timeCounter = 0,
  168. color,
  169. gradient;
  170. context.lineWidth = 0.5;
  171. var self = this;
  172. function animate(time) {
  173. // resize canvas if document size changed
  174. dimensions = self.getDocumentDimensions();
  175. if ((dimensions.height !== self.canvas.height) ||
  176. (dimensions.width !== self.canvas.width)) {
  177. self.canvas.height = dimensions.height;
  178. self.canvas.width = dimensions.width;
  179. }
  180. if (!self.paused) {
  181. // if enough time has passed, cast another spell to draw
  182. if (timeCounter >= self.waitTime && self.counting) {
  183. self.cast(undefined, undefined, undefined, chain); // start position
  184. self.counting = false;
  185. }
  186. // Draw branches
  187. for (var i in self.spells) {
  188. context.beginPath();
  189. context.strokeStyle = self.spells[i].color;
  190. if (self.spells[i].duration > 0) {
  191. for (var j in self.spells[i].branches) {
  192. self.drawSplineSegment(self.spells[i].branches[j], context);
  193. // When finished drawing splines, create a new set of branches
  194. if (self.spells[i].branches[j].t >= 1) {
  195. var newBranches = self.splitBranch(self.spells[i].branches[j]);
  196. // Replace old branch with two new branches
  197. self.spells[i].branches.splice(j, 1);
  198. self.spells[i].branches = self.spells[i].branches.concat(newBranches);
  199. }
  200. }
  201. // If over 10 branches, prune the branches
  202. if (prune) {
  203. if (sort) {
  204. while (self.spells[i].branches.length > self.prune_to) {
  205. self.spells[i].branches.pop();
  206. }
  207. } else {
  208. while (self.spells[i].branches.length > self.prune_to) {
  209. self.spells[i].branches.splice(
  210. Math.floor(Math.random() * self.spells[i].branches.length),
  211. 1);
  212. }
  213. }
  214. }
  215. context.stroke();
  216. context.closePath();
  217. self.spells[i].duration--;
  218. } else {
  219. // cast a new spell at random position
  220. if (self.spells[i].chain) {
  221. self.cast(undefined, undefined, undefined, true);
  222. }
  223. self.spells.splice(i, 1); // spell is done now
  224. }
  225. }
  226. if (self.counting) {
  227. timeCounter++;
  228. }
  229. }
  230. frameId = animationFrame.request(animate);
  231. }
  232. // Drawing interval
  233. var frameId = animationFrame.request(animate);
  234. return frameId;
  235. };
  236. Magic.prototype.canvasClickHandler = function(event) {
  237. var x = event.pageX;
  238. var y = event.pageY;
  239. this.cast(x, y, undefined, false);
  240. };
  241. Magic.prototype.draw = function() {
  242. var interval_time = 2000;
  243. // Initialize canvas
  244. var dimensions = this.getDocumentDimensions();
  245. this.canvas.height = dimensions.height;
  246. this.canvas.width = dimensions.width;
  247. // Check for canvas support, bind click event, and get context
  248. if (this.canvas.getContext){
  249. var self = this;
  250. this.canvas.addEventListener("click", function () { self.canvasClickHandler.apply(self, arguments); });
  251. this.context = this.canvas.getContext("2d");
  252. // Cast magic spells
  253. var frameId = this.drawSpells(this.context, false, true, true);
  254. }
  255. };