Advertisement
Guest User

Animated Factorization. Math Eye Candy. Modified JS.

a guest
Nov 1st, 2012
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function n() {
  2.     function a(a, c) {
  3.         return "undefined" !== typeof a ? a : c
  4.     }
  5.     this.d = function () {
  6.         return (new Date).getTime()
  7.     };
  8.     this.getTime = function () {
  9.         var a = this.d() - this.e,
  10.             a = this.b + this.speed(a),
  11.             a = Math.min(a, this.g);
  12.         return a = Math.max(a, this.h)
  13.     };
  14.     this.a = function (a) {
  15.         this.b = this.getTime();
  16.         this.e = this.d();
  17.         this.speed = "function" === typeof a ? a : function (c) {
  18.             return c * a
  19.         }
  20.     };
  21.     this.c = function () {
  22.         return (this.speed(1E3) - this.speed(0)) / 1E3
  23.     };
  24.     this.f = function () {
  25.         var a = this.speed;
  26.         this.a(function (c) {
  27.             return 4 * a(c)
  28.         })
  29.     };
  30.     this.stop = function () {
  31.         this.a(0)
  32.     };
  33.     this.i = function (a) {
  34.         this.b += a
  35.     };
  36.     this.h = a(0, 0);
  37.     this.g = a(9999500, 1E100);
  38.     this.b = a(0, 0);
  39.     this.e = this.d();
  40.     this.speed = function (a) {
  41.         return a
  42.     };
  43.     this.a(a(0, 1))
  44. };
  45. window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame || window.oRequestAnimationFrame;
  46. window.requestAnimationFrame || (window.requestAnimationFrame = function (a) {
  47.     var b = 0,
  48.         c = Math.max(0, 16 - b);
  49.     return window.setTimeout(function () {
  50.         var c = (new Date).getTime();
  51.         a(c);
  52.         b = (new Date).getTime() - c
  53.     }, c)
  54. });
  55. var p, t, u, v, x, y, z;
  56.  
  57. function A(a, b, c) {
  58.     var d = document.getElementById(a),
  59.         i = document.createElement("canvas");
  60.     i.setAttribute("width", 30);
  61.     i.setAttribute("height", 30);
  62.     d.appendChild(i);
  63.     d.onclick = function () {
  64.         B();
  65.         document.getElementById(a).className = "on";
  66.         b()
  67.     };
  68.     d.onmouseover = function () {
  69.         this.style.border = "solid 1px rgba(0,0,0,0.1)"
  70.     };
  71.     d.onmouseout = function () {
  72.         this.style.border = ""
  73.     };
  74.     c(i);
  75.     return d
  76. }
  77. function B() {
  78.     for (var a = document.getElementById("controls").childNodes, b = 0; b < a.length; b++) a[b].className = "off"
  79. }
  80.  
  81. function F(a, b, c, d) {
  82.     var d = b + 0.85 * c * d,
  83.         i = 0.5 - c / 2,
  84.         c = 0.5 + c / 2,
  85.         h = a.getContext("2d");
  86.     h.save();
  87.     h.scale(a.width, a.height);
  88.     h.beginPath();
  89.     h.moveTo(b, i);
  90.     h.lineTo(d, 0.5);
  91.     h.lineTo(b, c);
  92.     h.closePath();
  93.     h.lineJoin = "round";
  94.     h.fillStyle = "#fff";
  95.     h.fill();
  96.     h.lineWidth = 1 / a.width;
  97.     h.strokeStyle = "rgba(0,0,0,0.2)";
  98.     h.stroke();
  99.     h.restore()
  100. }
  101. function G(a) {
  102.     F(a, 0.44, 0.4, - 1);
  103.     F(a, 0.8, 0.4, - 1)
  104. }
  105. function H(a) {
  106.     F(a, 0.56, 0.4, 1);
  107.     F(a, 0.2, 0.4, 1)
  108. }
  109. function I(a) {
  110.     F(a, 0.7, 0.6, - 1)
  111. }
  112. function J(a) {
  113.     F(a, 0.3, 0.6, 1)
  114. }
  115.  
  116. function K(a) {
  117.     var b = a.getContext("2d");
  118.     b.save();
  119.     b.scale(a.width, a.height);
  120.     b.beginPath();
  121.     b.rect(0.25, 0.25, 0.5, 0.5);
  122.     b.lineJoin = "round";
  123.     b.fillStyle = "#fff";
  124.     b.fill();
  125.     b.lineWidth = 1 / a.width;
  126.     b.strokeStyle = "rgba(0,0,0,0.2)";
  127.     b.stroke();
  128.     b.restore()
  129. }
  130. function L(a, b) {
  131.     var c = a * v,
  132.         d = b * v;
  133.     return Math.acos(Math.cos(c) * Math.cos(d) + Math.sin(c) * Math.sin(d)) / v
  134. }
  135.  
  136. function M(a, b, c, d, i, h, q, r, k) {
  137.     if (i >= d.length) a.push(h), b.push(q), c.push(r);
  138.     else {
  139.         var e = d[i],
  140.             m = i + 1 < d.length ? d[i + 1] : 0,
  141.             l = 0.4 * r * v / (e + 3.4);
  142.         m && (l *= 0.93, 2 === m && (l *= 1.2), 3 === m && 7 >= e && (l *= 1.12), 4 === m && 7 >= e && (l *= 1.08));
  143.         var r = r - l,
  144.             j;
  145.         j = k + (4 === e ? -t : -u);
  146.         for (var s = v / e, f = 0; f < e; f++) {
  147.             var g = j + f * s;
  148.             M(a, b, c, d, i + 1, h + r * Math.cos(g), q + r * Math.sin(g), l, 2 === m ? g - u : k)
  149.         }
  150.     }
  151. }
  152. function N(a) {
  153.     if (1 >= a) return [];
  154.     var b = O(a),
  155.         a = N(a / b);
  156.     a.push(b);
  157.     return a
  158. }
  159.  
  160. function O(a) {
  161.     if (0 === a % 4) return 4;
  162.     if (0 === a % 2) return 2;
  163.     for (var b = Math.sqrt(a) + 1 | 0, c = 3; c <= b; c += 2) if (0 === a % c) return c;
  164.     return a
  165. }
  166. function P(a, b) {
  167.     var c;
  168.     if (1 > b.length) c = "";
  169.     else if (1 >= b.length && 4 !== b[0]) c = "prime";
  170.     else {
  171.         c = "";
  172.         for (var d = 0; d < b.length; d++) {
  173.             "" !== c && (c += '<span class="times">&times;</span>');
  174.             var i = b[d];
  175.             c = 4 == i ? c + '2<span class="times">&times;</span>2' : c + i
  176.         }
  177.     }
  178.     return '<span class="number">' + a + '</span><br><span class="decomposition">' + c + "</span>"
  179. }
  180.  
  181. function Q() {
  182.     var a, b = 0,
  183.         c = a = 0,
  184.         d = 0;
  185.     try {
  186.         b = window.innerWidth
  187.     } catch (i) {}
  188.     try {
  189.         a = window.innerHeight
  190.     } catch (h) {}
  191.     try {
  192.         c = screen.availWidth
  193.     } catch (q) {}
  194.     try {
  195.         d = screen.availHeight
  196.     } catch (r) {}
  197.     d && (d -= 70);
  198.     b = b ? b : c;
  199.     a = a ? a : d;
  200.     b || (b = 974);
  201.     a || (a = 718);
  202.     a = Math.min(b, a);
  203.     a = Math.max(a, 350);
  204.     var k = Math.round(0.45 * (a - 90)),
  205.         k = Math.max(k, 50),
  206.         k = Math.min(k, 600),
  207.         e = 2 * k + 1;
  208.     if (e != canvas.width || e != canvas.height) canvas.width = e, canvas.height = e, a = Math.round(-e / 2) + "px", canvas.style.marginLeft = a, canvas.style.marginTop = a;
  209.     var m = e / 2,
  210.         l = e / 2;
  211.     a = y.getTime() + 700;
  212.     var j;
  213.     j = Math.max(a / 1E3 | 0, 1);
  214.     var s = j + 1,
  215.         f = a - 500 - 1E3 * j,
  216.         f = 3 * (f / 1E3),
  217.         f = Math.max(f, 0),
  218.         f = Math.min(f, 1),
  219.         f = 0.5 - 0.5 * Math.cos(f * x),
  220.         g = N(j);
  221.     a = [];
  222.     d = [];
  223.     b = [];
  224.     M(a, d, b, g, 0, m, l, k * (1 - 0.6 / (j + 1)), 0);
  225.     var w = N(s),
  226.         c = [],
  227.         C = [],
  228.         D = [];
  229.     M(c, C, D, w, 0, m, l, k * (1 - 0.6 / (s + 1)), 0);
  230.     k = document.getElementById("status1");
  231.     m = document.getElementById("status2");
  232.     k.innerHTML = P(j, g);
  233.     m.innerHTML = P(s, w);
  234.     j = 0.7 / (1 - f + f * f);
  235.     k.style.opacity = 0.001 > j * (1 - f) ? 0.001 : 0.999 < j * (1 - f) ? 0.999 : j * (1 - f);
  236.     m.style.opacity = 0.001 > j * f ? 0.001 : 0.999 < j * f ? 0.999 : j * f;
  237.    
  238.     //p.clearRect(0, 0, e, e);
  239.        
  240.     e = f;
  241.     j = a.length;
  242.     g = c.length;
  243.     s = 1 / j;
  244.     f = 1 / g;
  245.     k = 1 - e;
  246.     for (g -= 1; 0 <= g; g--) {
  247.     if(g==0){
  248.         var E;
  249.         g < j ? (w = k * b[g] + e * D[g], m = k * g * s + e * g * f, l = k * a[g] + e * c[g], E = k * d[g] + e * C[g]) : (w = e * D[g], m = g * f, l = k * a[j - 1] + e * c[g], E = k * d[j - 1] + e * C[g]);
  250.         p.fillStyle = z[m * z.length | 0];
  251.         p.beginPath();
  252.         p.arc(l, E, 5, 0, v, !0);
  253.         p.fill()
  254.        
  255.         }
  256.     }
  257.     window.requestAnimationFrame(Q)
  258. }
  259. window.start = function () {
  260.     canvas = document.getElementById("canvas");
  261.     if (!canvas.getContext || !canvas.getContext("2d")) {
  262.         var a = document.getElementById("frame"),
  263.             b = document.createElement("div");
  264.         b.setAttribute("id", "error");
  265.         b.innerHTML = 'To view this visualization, upgrade to a modern web browser like <a href="http://www.google.com/chrome/">Chrome</a>.';
  266.         a.appendChild(b)
  267.     } else {
  268.         p = canvas.getContext("2d");
  269.         t = Math.PI / 4;
  270.         u = Math.PI / 2;
  271.         v = 2 * Math.PI;
  272.         x = Math.PI;
  273.         y = new n;
  274.         for (var a = 1E3, b = [], c = 0, a = 1E3; c < a; c++) {
  275.             var d = c / a + 0.9,
  276.                 i = 766.5 * Math.max(1 / 3 - L(1 / 6, d), 0),
  277.                 h = 766.5 * Math.max(1 / 3 - L(0.5, d), 0),
  278.                 d = 766.5 * Math.max(1 / 3 - L(5 / 6, d), 0),
  279.                 q = Math.sqrt(4E4 / (2 * i * i + 3 * h * h + d * d)),
  280.                 i = Math.round(i * q),
  281.                 h = Math.round(h * q),
  282.                 d = Math.round(d * q);
  283.             b[c] = "rgb(" + i + "," + h + "," + d + ")"
  284.         }
  285.         z = b;
  286.         A("fast_backward", function () {
  287.             -2 < y.c() ? y.a(-4) : -100 < y.c() && y.f()
  288.         }, G);
  289.         A("play_backward", function () {
  290.             y.a(-1)
  291.         }, I);
  292.         A("stop", function () {
  293.             y.a(0)
  294.         }, K);
  295.         a = A("play_forward", function () {
  296.             y.a(1)
  297.         }, J);
  298.         A("fast_forward", function () {
  299.             2 > y.c() ? y.a(4) : 100 > y.c() && y.f()
  300.         }, H);
  301.         B();
  302.         a.onclick();
  303.         window.requestAnimationFrame(Q)
  304.     }
  305. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement