let canvas; let ctx; let imports; let settings = { brightness: {}, saturate: {}, contrast: {}, blur: { filter: value => value * canvas.width / 100 + 'px' } }; const img = new Image(); // wait for site to be parsed so element can be found document.addEventListener("DOMContentLoaded", function() { canvas = document.getElementById("myCanvas"); ctx = canvas.getContext("2d"); imports = document.getElementById("imports"); // bind listeners document.getElementById("take-picture").addEventListener("click", take_picture); document.getElementById("upload-image").addEventListener("change", upload_image) document.getElementById("save").addEventListener("click", save_image) document.getElementById("share").addEventListener("click", share_image) window.addEventListener("resize", () => draw(true)) for (let setting in settings) { // make an array out of an iterable const elements = [...document.getElementsByClassName(setting)]; settings[setting].elements = elements; // if filter is not definded, use identity function settings[setting].filter ||= value => value; for (let element of elements) { element.addEventListener("input", settings_apply); } } }) function take_picture() { canvas.classList.remove("is-hidden"); imports.classList.add("is-hidden"); } function upload_image() { canvas.classList.remove("is-hidden"); imports.classList.add("is-hidden"); console.log(this.files[0]); img.src = URL.createObjectURL(this.files[0]); img.onload = () => draw(true); } function save_image() { draw(false); const dataUrl = canvas.toDataURL("image/png"); // downloading only works with links but not window.open const link = document.createElement('a'); link.href = dataUrl; link.download = 'imagine.png'; link.click(); } function share_image() { } function settings_apply(event) { const changed_setting = event.target.id; const new_value = event.target.value; // update all inputs for that setting (mobile and desktop) to the new value for (let element of settings[changed_setting].elements) { if (element == event.target) continue; element.value = new_value; } draw(true); } function length(vec) { return Math.sqrt(vec.reduce((acc, e) => acc + e * e, 0)) } function mul(vec, scalar) { return vec.map(e => e * scalar); } function add(vec_a, vec_b) { return vec_a.map((e, i) => e + vec_b[i]); } function mix(vec_x, vec_y, scalar) { return add(mul(vec_x, 1 - scalar), mul(vec_y, scalar)); } // https://www.shadertoy.com/view/ldSXWK function lensflare(uv, pos) { const intensity = 1.5; // const main = add(uv, mul(pos, -1)); const uvd = mul(uv, length(uv)); // let dist = length(main); dist = Math.pow(dist, .1); // const f1 = max(0.01-Math.pow(length(add(uv,mul(pos,1.2))),1.9),.0)*7.0; const f2 = Math.max(1.0 / (1.0 + 32.0 * Math.pow(length(add(uvd, mul(pos, 0.8))), 2.0)), .0) * 0.1; const f22 = Math.max(1.0 / (1.0 + 32.0 * Math.pow(length(add(uvd, mul(pos, 0.85))), 2.0)), .0) * 0.08; const f23 = Math.max(1.0 / (1.0 + 32.0 * Math.pow(length(add(uvd, mul(pos, 0.9))), 2.0)), .0) * 0.06; let uvx = mix(uv, uvd, -0.5); const f4 = Math.max(0.01 - Math.pow(length(add(uvx, mul(pos, 0.4))), 2.4), .0) * 6.0; const f42 = Math.max(0.01 - Math.pow(length(add(uvx, mul(pos, 0.45))), 2.4), .0) * 5.0; const f43 = Math.max(0.01 - Math.pow(length(add(uvx, mul(pos, 0.5))), 2.4), .0) * 3.0; uvx = mix(uv, uvd, -.4); const f5 = Math.max(0.01 - Math.pow(length(add(uvx, mul(pos, 0.2))), 5.5), .0) * 2.0; const f52 = Math.max(0.01 - Math.pow(length(add(uvx, mul(pos, 0.4))), 5.5), .0) * 2.0; const f53 = Math.max(0.01 - Math.pow(length(add(uvx, mul(pos, 0.6))), 5.5), .0) * 2.0; uvx = mix(uv, uvd, -0.5); const f6 = Math.max(0.01 - Math.pow(length(add(uvx, mul(pos, -0.3))), 1.6), .0) * 6.0; const f62 = Math.max(0.01 - Math.pow(length(add(uvx, mul(pos, -0.325))), 1.6), .0) * 3.0; const f63 = Math.max(0.01 - Math.pow(length(add(uvx, mul(pos, -0.35))), 1.6), .0) * 5.0; let c = [ f2 + f4 + f5 + f6, f22 + f42 + f52 + f62, f23 + f43 + f53 + f63 ]; c = mul(c, 1.3).map(e => e - length(uvd) * .05); return mul(c, intensity); } function draw(viewport_scale) { const filter = Object.entries(settings) .map(([setting, { elements, filter }]) => `${setting}(${filter(elements[0].value)})`) .join(" ") // set the resolution to the original and then scale down to the viewport to // only calculate the filter on pixels the user can see. canvas.height = img.naturalHeight; canvas.width = img.naturalWidth; if (viewport_scale) { canvas.height = canvas.clientHeight; canvas.width = canvas.clientWidth; } ctx.filter = filter console.log(filter); ctx.drawImage(img, 0, 0, canvas.width, canvas.height); ctx.filter = ''; // for (let y = 0; y < canvas.height; y++) { // for (let x = 0; x < canvas.width; x++) { // let uv = [x / canvas.width, y / canvas.height]; // // uv[0] *= canvas.width / canvas.height; // // let color = lensflare(uv, [.5,.5]); // ctx.fillStyle = `rgb(${[...uv,0].map(e => Math.floor(e * 255)).join()})`; // ctx.fillRect(x,y,1,1); // } // } var imgdata = ctx.getImageData(0, 0, canvas.width, canvas.height); const pixel_count = imgdata.data.length / 4; for (let i = 0; i < pixel_count; i++) { let x = i % canvas.width; let y = i / canvas.width; let u = (x / canvas.width - .5) * canvas.width / canvas.height; let v = y / canvas.height - .5; // imgdata.data[4*i] = u * 255; // imgdata.data[4*i+1] = v * 255; // imgdata.data[4*i+2] = 0 // imgdata.data[4*i+3] = 255; let color = lensflare([u, v], [0.3, 0.3]); imgdata.data[4 * i] = color[0] * 255; imgdata.data[4 * i + 1] = color[1] * 255; imgdata.data[4 * i + 2] = color[2] * 255; imgdata.data[4 * i + 3] = 255; } ctx.putImageData(imgdata, 0, 0); }