ijontichy

robotron.js

Oct 31st, 2014
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.44 KB | None | 0 0
  1. var movemargin = 40;
  2.  
  3. var rainbowColors = [ "#FF0000", "#FF4000", "#FF8000", "#FFB000", "#FFFF00",
  4. "#B0FF00", "#80FF00", "#40FF00", "#00FF00", "#00FF40", "#00FF80", "#00FFB0",
  5. "#00FFFF", "#00B0FF", "#0080FF", "#0040FF", "#0000FF", "#4000FF", "#8000FF",
  6. "#B000FF", "#FF00FF", "#FF00B0", "#FF0080", "#FF0040" ];
  7.  
  8. var cursorX = 0;
  9. var cursorY = 0;
  10.  
  11.  
  12. function getCursor(e)
  13. {
  14. cursorX = (window.Event) ? e.pageX : event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
  15. cursorY = (window.Event) ? e.pageY : event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
  16. }
  17.  
  18. function mod(x, y)
  19. {
  20. var ret = x - (Math.floor(x / y) * y);
  21. if (ret < 0) { ret = y + ret; }
  22. return ret;
  23. }
  24.  
  25. function moveText(text, speed, direction, mass)
  26. {
  27. if (text == null) { return; }
  28.  
  29. speed = Math.min(50, speed);
  30. mass = (mass == 0) ? 1 : mass;
  31.  
  32. var parent = text.parentNode;
  33.  
  34. var tstyle = window.getComputedStyle(text, null);
  35. var pstyle = window.getComputedStyle(parent, null);
  36.  
  37. var curTop = parseInt(tstyle.getPropertyValue('top'));
  38. var curLeft = parseInt(tstyle.getPropertyValue('left'));
  39.  
  40. var twidth = text.offsetWidth;
  41. var theight = text.offsetHeight;
  42.  
  43. direction = mod(direction, 360);
  44.  
  45. if (((curTop < movemargin) && (direction >= 0 && direction <= 180))
  46. || ((curTop + movemargin + theight > parent.offsetHeight) && (direction > 180 && direction < 360)))
  47. {
  48. direction = 360 - direction;
  49. }
  50.  
  51. if (((curLeft < movemargin) && (direction > 90 && direction < 270))
  52. || ((curLeft + movemargin + twidth > parent.offsetWidth) && (direction < 90 || direction > 270)))
  53. {
  54. if (direction <= 180)
  55. {
  56. direction = 180 - direction;
  57. }
  58. else
  59. {
  60. direction = 540 - direction;
  61. }
  62.  
  63. text.attributes['flipimage'] = curLeft < movemargin ? 1 : -1;
  64. }
  65.  
  66. direction = mod(direction, 360);
  67. direction *= (Math.PI / 180);
  68.  
  69. var xvel = speed * Math.cos(direction);
  70. var yvel = speed * -Math.sin(direction);
  71.  
  72. var pushx = cursorX - (curLeft + (twidth / 2));
  73. var pushy = cursorY - (curTop + (theight / 2));
  74.  
  75. var pushdist = Math.sqrt(Math.pow(pushx, 2) + Math.pow(pushy, 2));
  76.  
  77. var pushstrength = Math.pow(300, 2) / Math.pow(pushdist, 1.5);
  78. pushstrength += Math.pow(50, 4) / Math.pow(pushdist, 3);
  79.  
  80. pushstrength /= mass;
  81.  
  82. pushx /= pushdist;
  83. pushy /= pushdist;
  84.  
  85. xvel -= (pushx * pushstrength);
  86. yvel -= (pushy * pushstrength);
  87.  
  88. xvel = Math.round(xvel);
  89. yvel = Math.round(yvel);
  90.  
  91. curTop += yvel;
  92. curLeft += xvel;
  93.  
  94. var leftmin = movemargin - 1;
  95. var leftmax = parent.offsetWidth - twidth - movemargin + 1;
  96.  
  97. var topmin = movemargin - 1;
  98. var topmax = parent.offsetHeight - theight - movemargin + 1;
  99.  
  100. if (curTop < topmin) { curTop = topmin; }
  101. if (curTop > topmax) { curTop = topmax; }
  102. if (curLeft < leftmin) { curLeft = leftmin; }
  103. if (curLeft > leftmax) { curLeft = leftmax; }
  104.  
  105.  
  106. text.style.top = curTop + "px";
  107. text.style.left = curLeft + "px";
  108.  
  109. var sizemult = Math.pow(mass, 0.275);
  110. var intext = text.children;
  111. var i;
  112. var isimg = 0;
  113.  
  114. for (i = 0; i < intext.length; i++)
  115. {
  116. var child = intext[i];
  117.  
  118. if (child.tagName.toLowerCase() == "img")
  119. {
  120. isimg = 1;
  121. child.style.width = Math.round(50 * sizemult) + "%";
  122. break;
  123. }
  124. }
  125.  
  126. if (!isimg) { text.style.fontSize = Math.round(36 * sizemult) + "px"; }
  127.  
  128.  
  129.  
  130. var randsign = Math.random() >= 0.5 ? 1 : -1;
  131.  
  132. speed = Math.sqrt(Math.pow(xvel, 2) + Math.pow(yvel, 2));
  133. speed += ((Math.random() * 8) - 4) / Math.pow(mass, 0.5);
  134.  
  135. if (speed > 2)
  136. {
  137. direction = Math.atan2(-yvel / speed, xvel / speed);
  138. }
  139.  
  140. direction = mod(direction, Math.PI * 2);
  141.  
  142. direction *= (180 / Math.PI);
  143. direction = Math.round(direction);
  144.  
  145. direction += ((Math.random() * 60) - 30) / Math.pow(mass, 0.5);
  146.  
  147. if (speed < 0)
  148. {
  149. direction = direction - 180;
  150. speed = -speed;
  151. }
  152.  
  153. setTimeout(function() { moveText(text, speed, direction, mass); }, 25);
  154. }
  155.  
  156. function rainbowMode(text, index)
  157. {
  158. var index = mod(index, rainbowColors.length);
  159.  
  160. text.style.color = rainbowColors[index];
  161.  
  162. setTimeout(function() { rainbowMode(text, index+1); }, 25);
  163. }
  164.  
  165. function igorFlipping(mover)
  166. {
  167. var i, children;
  168. var igor = null;
  169. children = mover.children;
  170.  
  171. for (i = 0; i < children.length; i++)
  172. {
  173. var child = children[i];
  174.  
  175. if (child.tagName.toLowerCase() == "img")
  176. {
  177. if (child.src.indexOf("igor") != -1)
  178. {
  179. igor = child;
  180. break;
  181. }
  182. }
  183. }
  184.  
  185. if (igor === null) { return; }
  186.  
  187. if (mover.attributes['flipimage'])
  188. {
  189. var imgname = (mover.attributes['flipimage'] == 1) ? "igorchatter_right.gif" : "igorchatter.gif";
  190. var imgpath = "http://ijontichy.lostsig.com/img/" + imgname;
  191.  
  192. mover.attributes['flipimage'] = 0;
  193. igor.src = imgpath;
  194. }
  195.  
  196. setTimeout(function() { igorFlipping(mover); }, 25);
  197. }
  198.  
  199. function randPos(text, isFixed)
  200. {
  201. if (text == null) { return; }
  202.  
  203. var parent = text.parentNode;
  204.  
  205. var tstyle = window.getComputedStyle(text, null);
  206. var pstyle = window.getComputedStyle(parent, null);
  207.  
  208. var pwidth, pheight;
  209.  
  210. if (isFixed)
  211. {
  212. pwidth = window.innerWidth;
  213. pheight = window.innerHeight;
  214.  
  215. }
  216. else
  217. {
  218. pwidth = parent.offsetWidth;
  219. pheight = parent.offsetHeight;
  220. }
  221.  
  222. var twidth = text.offsetWidth;
  223. var theight = text.offsetHeight;
  224.  
  225. text.style.top = Math.round(Math.random() * (pheight - theight)) + "px";
  226. text.style.left = Math.round(Math.random() * (pwidth - twidth)) + "px";
  227. }
  228.  
  229.  
  230. function fillMover(mover)
  231. {
  232. /*var insideText = document.createTextNode("Heh");
  233. mover.appendChild(insideText);*/
  234.  
  235. var insideImg = document.createElement("img");
  236.  
  237. if (Math.random() < 0.5)
  238. {
  239. insideImg.src = "http://ijontichy.lostsig.com/img/igorchatter.gif";
  240. }
  241. else
  242. {
  243. insideImg.src = "http://ijontichy.lostsig.com/img/igorchatter_right.gif";
  244. }
  245.  
  246. mover.appendChild(insideImg);
  247.  
  248. for (rule in moverCSS)
  249. {
  250. mover.style[rule] = moverCSS[rule];
  251. }
  252. }
  253.  
  254.  
  255. var moverTemplate = "robotron_mover<#>";
  256. var moverClass = "robotron_mover";
  257.  
  258. var moverCSS = {
  259. "position": "absolute",
  260. "fontSize": "72px",
  261. "textShadow": "1px 1px 2px black, 1px -1px 2px black, -1px 1px 2px black, -1px -1px 2px black",
  262. "fontFamily": "monospace",
  263. "pointerEvents": "none",
  264. "minWidth": "50px",
  265. "minHeight": "50px",
  266. };
  267.  
  268. function init()
  269. {
  270. var i;
  271. var toplevel = document.body;
  272. if (toplevel == null) { return; }
  273.  
  274. if (window.Event) { document.captureEvents(Event.MOUSEMOVE); }
  275. document.onmousemove = getCursor;
  276.  
  277. var pheight = toplevel.offsetHeight;
  278.  
  279. for (i = 0; i < Math.max(6, Math.round(pheight / 300)); i++)
  280. {
  281. var moverName = moverTemplate.replace("<#>", i);
  282. var mover = document.createElement("span");
  283.  
  284. mover.id = moverName;
  285. mover.className = moverClass;
  286.  
  287. fillMover(mover);
  288. toplevel.appendChild(mover);
  289.  
  290. randPos(mover, false);
  291. moveText(mover, 5, Math.round(Math.random() * 360), 9 + (Math.random() * 91));
  292. igorFlipping(mover);
  293. }
  294. }
  295.  
  296. window.onload = init;
  297. window.init = init;
Advertisement
Add Comment
Please, Sign In to add comment