2024-12-08 02:59:45 +01:00
|
|
|
let canvas;
|
|
|
|
let ctx;
|
|
|
|
let settings = {
|
|
|
|
brightness: {},
|
|
|
|
saturate: {},
|
|
|
|
contrast: {},
|
2024-12-08 07:04:03 +01:00
|
|
|
blur: { filter: value => value * canvas.width / 100 + 'px' }
|
2024-12-08 02:59:45 +01:00
|
|
|
};
|
|
|
|
const img = new Image();
|
2024-12-05 22:55:49 +01:00
|
|
|
|
2024-12-06 19:06:31 +01:00
|
|
|
// wait for site to be parsed so element can be found
|
2024-12-08 02:59:45 +01:00
|
|
|
document.addEventListener("DOMContentLoaded", function() {
|
|
|
|
canvas = document.getElementById("myCanvas");
|
|
|
|
ctx = canvas.getContext("2d");
|
2024-12-10 03:14:49 +01:00
|
|
|
document.getElementById("back").addEventListener("click", () => document.body.classList.remove("editor-enabled"));
|
2024-12-08 02:59:45 +01:00
|
|
|
|
2024-12-06 19:06:31 +01:00
|
|
|
// bind listeners
|
|
|
|
document.getElementById("take-picture").addEventListener("click", take_picture);
|
|
|
|
document.getElementById("upload-image").addEventListener("change", upload_image)
|
|
|
|
|
2024-12-07 20:24:16 +01:00
|
|
|
document.getElementById("save").addEventListener("click", save_image)
|
|
|
|
document.getElementById("share").addEventListener("click", share_image)
|
|
|
|
|
2024-12-08 04:19:51 +01:00
|
|
|
window.addEventListener("resize", () => draw(true))
|
|
|
|
|
2024-12-08 02:59:45 +01:00
|
|
|
for (let setting in settings) {
|
|
|
|
// make an array out of an iterable
|
|
|
|
const elements = [...document.getElementsByClassName(setting)];
|
|
|
|
settings[setting].elements = elements;
|
2024-12-08 04:19:51 +01:00
|
|
|
// if filter is not definded, use identity function
|
|
|
|
settings[setting].filter ||= value => value;
|
2024-12-08 02:59:45 +01:00
|
|
|
for (let element of elements) {
|
|
|
|
element.addEventListener("input", settings_apply);
|
|
|
|
}
|
2024-12-10 04:33:17 +01:00
|
|
|
|
|
|
|
const reset_elements = document.getElementsByClassName(setting + "-reset");
|
|
|
|
for (let reset_element of reset_elements) {
|
|
|
|
reset_element.addEventListener("click", () => reset_all(setting));
|
|
|
|
}
|
2024-12-08 02:59:45 +01:00
|
|
|
}
|
2024-12-10 04:33:17 +01:00
|
|
|
});
|
2024-12-08 02:59:45 +01:00
|
|
|
|
2024-12-10 04:33:17 +01:00
|
|
|
function reset_all(setting) {
|
|
|
|
console.log("reseting " + setting);
|
|
|
|
for (let element of settings[setting].elements) {
|
|
|
|
element.value = element.defaultValue;
|
|
|
|
}
|
|
|
|
draw(true);
|
|
|
|
}
|
2024-12-06 19:06:31 +01:00
|
|
|
|
|
|
|
function take_picture() {
|
2024-12-10 03:14:49 +01:00
|
|
|
document.body.classList.add("editor-enabled");
|
2024-12-06 19:06:31 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-12-08 02:59:45 +01:00
|
|
|
function upload_image() {
|
2024-12-10 03:14:49 +01:00
|
|
|
document.body.classList.add("editor-enabled");
|
|
|
|
|
2024-12-06 19:06:31 +01:00
|
|
|
console.log(this.files[0]);
|
|
|
|
|
|
|
|
img.src = URL.createObjectURL(this.files[0]);
|
2024-12-08 04:19:51 +01:00
|
|
|
img.onload = () => draw(true);
|
2024-12-06 19:06:31 +01:00
|
|
|
}
|
2024-12-05 22:55:49 +01:00
|
|
|
|
2024-12-07 20:24:16 +01:00
|
|
|
function save_image() {
|
2024-12-08 04:19:51 +01:00
|
|
|
draw(false);
|
|
|
|
|
2024-12-07 20:24:16 +01:00
|
|
|
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() {
|
2024-12-05 22:55:49 +01:00
|
|
|
|
2024-12-07 20:24:16 +01:00
|
|
|
}
|
2024-12-08 02:59:45 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-12-08 04:19:51 +01:00
|
|
|
draw(true);
|
|
|
|
}
|
|
|
|
|
2024-12-08 07:04:03 +01:00
|
|
|
// https://www.shadertoy.com/view/ldSXWK
|
2024-12-08 08:37:58 +01:00
|
|
|
function lensflare(pos_x, pos_y) {
|
|
|
|
const imgdata = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
|
|
|
const pixel_count = imgdata.data.length / 4;
|
|
|
|
const aspect_ratio = canvas.width / canvas.height;
|
|
|
|
for (let i = 0; i < pixel_count; i++) {
|
|
|
|
const x = i % canvas.width;
|
|
|
|
const y = i / canvas.width;
|
|
|
|
const u = (x / canvas.width - .5) * aspect_ratio;
|
|
|
|
const v = y / canvas.height - .5;
|
|
|
|
|
2024-12-08 17:00:26 +01:00
|
|
|
const intensity = 1.5 * 255;
|
|
|
|
const uv_len = u * u + v * v;
|
2024-12-08 08:37:58 +01:00
|
|
|
const uvd_x = u * uv_len;
|
|
|
|
const uvd_y = v * uv_len;
|
2024-12-08 17:00:26 +01:00
|
|
|
const uvd_len = uvd_x * uvd_x + uvd_y * uvd_y;
|
2024-12-08 08:37:58 +01:00
|
|
|
|
|
|
|
const uvd_pos_x = uvd_x + pos_x;
|
|
|
|
const uvd_pos_y = uvd_y + pos_y;
|
|
|
|
let temp = uvd_pos_x * uvd_pos_x + uvd_pos_y * uvd_pos_y
|
2024-12-08 17:00:26 +01:00
|
|
|
const f2 = Math.max(1.0 / (1.0 + 32.0 * temp * .64), .0) * 0.1;
|
|
|
|
const f22 = Math.max(1.0 / (1.0 + 32.0 * temp * .72), .0) * 0.08;
|
|
|
|
const f23 = Math.max(1.0 / (1.0 + 32.0 * temp * .81), .0) * 0.06;
|
2024-12-08 08:37:58 +01:00
|
|
|
|
|
|
|
let uvx_x = u * (1 + 0.5) + uvd_x * 0.5;
|
|
|
|
let uvx_y = v * (1 + 0.5) + uvd_y * 0.5;
|
|
|
|
let uvx_pos_x = uvx_x + pos_x;
|
|
|
|
let uvx_pos_y = uvx_y + pos_y;
|
|
|
|
|
|
|
|
temp = uvx_pos_x * uvx_pos_x + uvx_pos_y * uvx_pos_y
|
2024-12-08 17:00:26 +01:00
|
|
|
const f4 = Math.max(0.01 - temp * .16, .0) * 6.0;
|
|
|
|
const f42 = Math.max(0.01 - temp * .20, .0) * 5.0;
|
|
|
|
const f43 = Math.max(0.01 - temp * .25, .0) * 3.0;
|
2024-12-08 08:37:58 +01:00
|
|
|
|
|
|
|
uvx_x = u * (1 + 0.4) + uvd_x * 0.4;
|
|
|
|
uvx_y = v * (1 + 0.4) + uvd_y * 0.4;
|
|
|
|
uvx_pos_x = uvx_x + pos_x;
|
|
|
|
uvx_pos_y = uvx_y + pos_y;
|
|
|
|
|
|
|
|
temp = uvx_pos_x * uvx_pos_x + uvx_pos_y * uvx_pos_y
|
2024-12-08 17:00:26 +01:00
|
|
|
const f5 = Math.max(0.01 - temp * .04, .0) * 2.0;
|
|
|
|
const f52 = Math.max(0.01 - temp * .16, .0) * 2.0;
|
|
|
|
const f53 = Math.max(0.01 - temp * .36, .0) * 2.0;
|
2024-12-08 08:37:58 +01:00
|
|
|
|
|
|
|
uvx_x = u * (1 + 0.5) + uvd_x * 0.5;
|
|
|
|
uvx_y = v * (1 + 0.5) + uvd_y * 0.5;
|
|
|
|
uvx_pos_x = uvx_x - pos_x;
|
|
|
|
uvx_pos_y = uvx_y - pos_y;
|
|
|
|
|
|
|
|
temp = uvx_pos_x * uvx_pos_x + uvx_pos_y * uvx_pos_y
|
2024-12-08 17:00:26 +01:00
|
|
|
const f6 = Math.max(0.01 - temp * .9, .0) * 6.0;
|
|
|
|
const f62 = Math.max(0.01 - temp * .1, .0) * 3.0;
|
|
|
|
const f63 = Math.max(0.01 - temp * .12, .0) * 5.0;
|
2024-12-08 08:37:58 +01:00
|
|
|
|
2024-12-08 17:00:26 +01:00
|
|
|
imgdata.data[4 * i + 0] += 1.2 * ((f2 + f4 + f5 + f6) * 1.3 - uvd_len * .05) * intensity;
|
|
|
|
imgdata.data[4 * i + 1] += 1.5 * ((f22 + f42 + f52 + f62) * 1.3 - uvd_len * .05) * intensity;
|
|
|
|
imgdata.data[4 * i + 2] += 1.3 * ((f23 + f43 + f53 + f63) * 1.3 - uvd_len * .05) * intensity;
|
2024-12-08 08:37:58 +01:00
|
|
|
imgdata.data[4 * i + 3] = 255;
|
|
|
|
}
|
|
|
|
ctx.putImageData(imgdata, 0, 0);
|
2024-12-08 07:04:03 +01:00
|
|
|
}
|
|
|
|
|
2024-12-08 04:19:51 +01:00
|
|
|
function draw(viewport_scale) {
|
2024-12-08 02:59:45 +01:00
|
|
|
const filter = Object.entries(settings)
|
2024-12-08 07:04:03 +01:00
|
|
|
.map(([setting, { elements, filter }]) => `${setting}(${filter(elements[0].value)})`)
|
2024-12-08 02:59:45 +01:00
|
|
|
.join(" ")
|
|
|
|
|
2024-12-08 04:19:51 +01:00
|
|
|
// 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;
|
|
|
|
}
|
2024-12-08 02:59:45 +01:00
|
|
|
ctx.filter = filter
|
|
|
|
console.log(filter);
|
|
|
|
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
|
2024-12-08 07:04:03 +01:00
|
|
|
ctx.filter = '';
|
|
|
|
|
2024-12-10 01:05:05 +01:00
|
|
|
// lensflare(.3,.3);
|
2024-12-08 02:59:45 +01:00
|
|
|
}
|
2024-12-08 04:19:51 +01:00
|
|
|
|