-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglitch.min.js
1 lines (1 loc) · 2.32 KB
/
glitch.min.js
1
var glitch=function(){"use strict";function b(a,b,f,g){if("string"!=typeof a)return h("An image source is required");if("function"!=typeof b)return h("A callback Function is required");f=i(f,5,100)||25,g=i(g,5,100)||50;var j=/data:[\w\/\w]+;base64,/i.test(a);j?e(d(this,a,b,f),g):c(this,a,b,f,g)}function c(a,c,d,e,f){var g=new XMLHttpRequest;g.open("GET",c),g.overrideMimeType("text/plain;charset=x-user-defined"),g.onload=function(){var h=g.response,i=h.substring(0,9).match(/(gif|png)/i),k=i?i[0].toLowerCase():"jpeg";c=j(h,"image/"+k),b.call(a,c,d,e,f)},g.onerror=function(){h("The image could not be loaded")},g.send()}function d(a,b,c,d){function i(a){var b=a.length,c=Math.sqrt(b*(parseFloat("1e"+b.toString().length)/d)),e=b/c;return a.match(new RegExp(".{1,"+Math.ceil(b/e)+"}","g"))}var e=d,f=b.split(","),g=f[0].charAt(11);"p"===g?e/=25:"g"===g&&(e/=5);var h=i(f[1],e);return{context:a,source:b,callback:c,strength:d,attempts:0,header:f[0]+",",data:h,slice:h[h.length-1].length}}function e(b,c){function k(b,c,d){for(var e=i(0|d/5,1,20);e--;){var f=Math.random()*c|0,g=a.charAt(64*Math.random()|0);b=b.substring(0,f)+g+b.substring(++f)}return b}var d=b.data,e=[b.header];if(++b.attempts<c){for(var h=0,j=d.length;j>h;++h)e.push(k(d[h],b.slice,c));f(e.join(""),b,c)}else g(b.source,!1,b)}function f(a,b,c){var d=document.createElement("canvas"),f=d.getContext("2d"),h=new Image;h.onload=function(){f.drawImage(h,0,0,2,3);for(var l,d=f.getImageData(0,0,2,3).data,i=0,j=d.length,k=!1;j>i;){var m=d[i++],n=d[i++],o=d[i++],q=(d[i++],0|(m+n+o)/3);if(l===q){k=!0;break}l=q}k?e(b,c):g(a,!0,b)},h.onerror=function(){e(b,c)},h.src=a}function g(a,b,c){c.callback.call(c.context,a,b,{source:c.source,strength:c.strength,attempts:c.attempts})}function h(a){console.error(a)}function i(a,b,c){return b>a?b:a>c?c:a}function j(b,c){for(var g,h,i,d=["data:"+c+";base64,"],e=0,f=b.length;f>e;){if(g=255&b.charCodeAt(e++),e===f){d.push(a.charAt(g>>2)),d.push(a.charAt((3&g)<<4)),d.push("==");break}if(h=b.charCodeAt(e++),e===f){d.push(a.charAt(g>>2)),d.push(a.charAt((3&g)<<4|(240&h)>>4)),d.push(a.charAt((15&h)<<2)),d.push("=");break}i=b.charCodeAt(e++),d.push(a.charAt(g>>2)),d.push(a.charAt((3&g)<<4|(240&h)>>4)),d.push(a.charAt((15&h)<<2|(192&i)>>6)),d.push(a.charAt(63&i))}return d.join("")}var a="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";return b}();