code.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. var requestId;
  2. var pause = false;
  3. document.getElementById('pause').onclick = function(){
  4. pause = !pause;
  5. };
  6. document.getElementById("drop").style.cursor = "pointer";
  7. var inputFile;
  8. function updateFile(file) {
  9. if (typeof file != 'undefined') {
  10. document.getElementById('list').innerHTML = '<strong>' + escape(file.name) + '</strong> [' + file.type + ', ' + file.size + ' bytes]';
  11. document.getElementById("parse").style.visibility = "visible";
  12. inputFile = file;
  13. }
  14. }
  15. function handleFileSelect(e) {
  16. e.stopPropagation();
  17. e.preventDefault();
  18. var files = e.dataTransfer.files;
  19. updateFile(files[0]);
  20. }
  21. function handleBrowse(e) {
  22. var files = e.target.files;
  23. updateFile(files[0]);
  24. }
  25. document.getElementById('browse').addEventListener('change', handleBrowse, false);
  26. function handleDragOver(e) {
  27. e.stopPropagation();
  28. e.preventDefault();
  29. e.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
  30. }
  31. // Setup the dnd listeners.
  32. var dropZone = document.getElementById('drop');
  33. dropZone.addEventListener('dragover', handleDragOver, false);
  34. dropZone.addEventListener('drop', handleFileSelect, false);
  35. const canvas = document.querySelector("#canvas");
  36. //var context = canvas.getContext('2d');
  37. function resize() {
  38. canvas.width = window.innerWidth;
  39. canvas.height = window.innerHeight;
  40. }
  41. window.addEventListener('resize', resize);
  42. resize();
  43. m00 = 0, m10 = 4, m20 = 8, m30 = 12, m01 = 1, m11 = 5, m21 = 9, m31 = 13, m02 = 2, m12 = 6,
  44. m22 = 10, m32 = 14, m03 = 3, m13 = 7, m23 = 11, m33 = 15;
  45. function ortho(left, right, bottom, top, z_near, z_far) {
  46. a = 2 / (right - left);
  47. b = 2 / (top - bottom);
  48. c = -2 / (z_far - z_near);
  49. r = -(right + left) / (right - left);
  50. s = -(top + bottom) / (top - bottom);
  51. t = -(z_far + z_near) / (z_far - z_near);
  52. m =
  53. [1, 0, 0, 0,
  54. 0, 1, 0, 0,
  55. 0, 0, 1, 0,
  56. 0, 0, 0, 1];
  57. m[m00] = a;
  58. m[m11] = b;
  59. m[m22] = c;
  60. m[m30] = r;
  61. m[m31] = s;
  62. m[m32] = t;
  63. m[m33] = 1;
  64. return m;
  65. }
  66. function isPowerOf2(value) {
  67. return (value & (value - 1)) == 0;
  68. }
  69. let startGL = function(img) {
  70. // Initialize the GL context
  71. const gl = canvas.getContext("webgl");
  72. // Only continue if WebGL is available and working
  73. if (!gl) {
  74. alert("Unable to initialize WebGL.");
  75. return;
  76. }
  77. const program = initShader(gl, vsSource, fsSource);
  78. const data = {
  79. program: program,
  80. attribs: {
  81. position: gl.getAttribLocation(program, 'position'),
  82. texcoord: gl.getAttribLocation(program, 'texcoord'),
  83. },
  84. uniforms: {
  85. projection: gl.getUniformLocation(program, 'projection'),
  86. dim: gl.getUniformLocation(program, 'dim'),
  87. enabled: gl.getUniformLocation(program, 'enabled'),
  88. rseed: gl.getUniformLocation(program, 'rseed'),
  89. sampler: gl.getUniformLocation(program, 'sampler'),
  90. },
  91. };
  92. const identity =
  93. [1, 0, 0, 0,
  94. 0, 1, 0, 0,
  95. 0, 0, 1, 0,
  96. 0, 0, 0, 1]
  97. const mat = ortho(-1, 1, 1, -1, 1, -1);
  98. const positionBuffer = gl.createBuffer();
  99. gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
  100. const positions = [
  101. -1.0, 1.0,
  102. -1.0, -1.0,
  103. 1.0, -1.0,
  104. -1.0, 1.0,
  105. 1.0, -1.0,
  106. 1.0, 1.0
  107. ];
  108. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
  109. const textureBuffer = gl.createBuffer();
  110. gl.bindBuffer(gl.ARRAY_BUFFER, textureBuffer);
  111. const tex = [
  112. 0.0, 1.0,
  113. 0.0, 0.0,
  114. 1.0, 0.0,
  115. 0.0, 1.0,
  116. 1.0, 0.0,
  117. 1.0, 1.0
  118. ];
  119. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(tex), gl.STATIC_DRAW);
  120. const tex0 = createTexture(gl, img);
  121. const tex1 = createTexture(gl, img);
  122. const fb0 = gl.createFramebuffer();
  123. gl.bindFramebuffer(gl.FRAMEBUFFER, fb0);
  124. const attachmentPoint = gl.COLOR_ATTACHMENT0;
  125. gl.framebufferTexture2D(gl.FRAMEBUFFER, attachmentPoint, gl.TEXTURE_2D, tex0, 0);
  126. const fb1 = gl.createFramebuffer();
  127. gl.bindFramebuffer(gl.FRAMEBUFFER, fb1);
  128. gl.framebufferTexture2D(gl.FRAMEBUFFER, attachmentPoint, gl.TEXTURE_2D, tex1, 0);
  129. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  130. var flipTex = false;
  131. requestId = window.requestAnimationFrame(render);
  132. function render(t) {
  133. if(pause) {
  134. requestId = window.requestAnimationFrame(render);
  135. return;
  136. }
  137. gl.viewport(0, 0, img.width, img.height);
  138. // swap which texture is being rendered to each frame
  139. texture = flipTex ? tex0 : tex1;
  140. fb = flipTex ? fb1 : fb0;
  141. flipTex = !flipTex;
  142. gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
  143. {
  144. const numComponents = 2; // pull out 2 values per iteration
  145. const type = gl.FLOAT; // the data in the buffer is 32bit floats
  146. const normalize = false; // don't normalize
  147. const stride = 0; // how many bytes to get from one set of values to the next
  148. // 0 = use type and numComponents above
  149. const offset = 0; // how many bytes inside the buffer to start from
  150. gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
  151. gl.vertexAttribPointer(
  152. data.attribs.position,
  153. numComponents,
  154. type,
  155. normalize,
  156. stride,
  157. offset);
  158. gl.enableVertexAttribArray(data.attribs.position);
  159. }
  160. {
  161. const num = 2; // every coordinate composed of 2 values
  162. const type = gl.FLOAT; // the data in the buffer is 32 bit float
  163. const normalize = false; // don't normalize
  164. const stride = 0; // how many bytes to get from one set to the next
  165. const offset = 0; // how many bytes inside the buffer to start from
  166. gl.bindBuffer(gl.ARRAY_BUFFER, textureBuffer);
  167. gl.vertexAttribPointer(data.attribs.texcoord, num, type, normalize, stride, offset);
  168. gl.enableVertexAttribArray(data.attribs.texcoord);
  169. }
  170. gl.useProgram(data.program);
  171. gl.uniform1f(data.uniforms.enabled, true);
  172. gl.uniform2f(data.uniforms.dim, img.width/2, img.height/2);
  173. gl.uniform2f(data.uniforms.rseed, Math.random(), Math.random());
  174. {
  175. // Tell WebGL we want to affect texture unit 0
  176. gl.activeTexture(gl.TEXTURE0);
  177. // Bind the texture to texture unit 0
  178. gl.bindTexture(gl.TEXTURE_2D, texture);
  179. // Tell the shader we bound the texture to texture unit 0
  180. gl.uniform1i(data.uniforms.sampler, 0);
  181. const offset = 0;
  182. const vertexCount = 6;
  183. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
  184. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
  185. gl.drawArrays(gl.TRIANGLES, offset, vertexCount);
  186. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  187. gl.uniform1f(data.uniforms.enabled, false);
  188. gl.uniformMatrix4fv(
  189. data.uniforms.projection,
  190. false,
  191. identity);
  192. gl.clearColor(0.2, 0.2, 0.2, 1.0);
  193. // Clear the color buffer with specified clear color
  194. gl.clear(gl.COLOR_BUFFER_BIT);
  195. gl.viewport(0, 0, canvas.width, canvas.height);
  196. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
  197. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  198. gl.drawArrays(gl.TRIANGLES, offset, vertexCount);
  199. }
  200. requestId = window.requestAnimationFrame(render);
  201. }
  202. function createTexture(gl, image) {
  203. const texture = gl.createTexture();
  204. gl.bindTexture(gl.TEXTURE_2D, texture);
  205. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true)
  206. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
  207. if (isPowerOf2(image.width) && isPowerOf2(image.height)) {
  208. // Yes, it's a power of 2. Generate mips.
  209. gl.generateMipmap(gl.TEXTURE_2D);
  210. }
  211. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  212. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  213. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
  214. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
  215. return texture;
  216. }
  217. }
  218. function loadImage() {
  219. var img = new Image;
  220. img.onload = function() {
  221. window.cancelAnimationFrame(requestId);
  222. startGL(img);
  223. }
  224. img.src = URL.createObjectURL(inputFile);
  225. }
  226. document.getElementById('parse').onclick = loadImage;
  227. /*
  228. var dim = 128
  229. var sq = []
  230. for(var i = 0; i < dim*dim; i++) {
  231. sq.push((i % 128)/128 * 360)
  232. }
  233. var t0 = performance.now();
  234. function loop(t)
  235. {
  236. var dt = (t - t0) / 1000; // delta t, in seconds
  237. t0 = t;
  238. var fy = 0;
  239. var size = Math.min(canvas.height, canvas.width) / dim;
  240. let newArray = []; newArray.length = dim*dim
  241. if (!pause) {
  242. for(var i = 0; i < sq.length; i++) {
  243. newArray[i] = decideOutcome(i)
  244. context.fillStyle = 'hsl(' + newArray[i] + ', 90%, 60%)'
  245. context.fillRect((i % dim)*size, Math.floor(i/dim) * size, size, size);
  246. }
  247. sq = newArray;
  248. }
  249. window.requestAnimationFrame(loop);
  250. }
  251. function decideOutcome(i) {
  252. opts = []
  253. function op(b) {
  254. opts.push(b)
  255. }
  256. op(sq[i])
  257. if(i % dim == 0) {
  258. // first column
  259. op(sq[i+1]) // search right
  260. }
  261. else if(i % dim == dim-1) {
  262. //last column
  263. op(sq[i-1]) // search left
  264. }
  265. else {
  266. op(sq[i+1]) // search right
  267. op(sq[i-1]) // search left
  268. }
  269. if(i < dim) {
  270. // first row
  271. op(sq[i+dim]) // search down
  272. }
  273. else if(i >= sq.length - dim) {
  274. // last row
  275. op(sq[i-dim]) // search up
  276. }
  277. else {
  278. op(sq[i+dim]) // search down
  279. op(sq[i-dim]) // search up
  280. }
  281. end = opts[Math.floor(Math.random() * opts.length)]
  282. return (end - sq[i]) * 0.99 + sq[i];
  283. }
  284. window.requestAnimationFrame(loop);
  285. */