Imagine/main.js

101 lines
2.7 KiB
JavaScript
Raw Normal View History

2024-12-08 02:59:45 +01:00
let canvas;
let ctx;
let imports;
let settings = {
brightness: {},
saturate: {},
contrast: {},
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
// 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");
imports = document.getElementById("imports");
// 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)
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;
// 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);
}
}
})
function take_picture() {
canvas.classList.remove("is-hidden");
imports.classList.add("is-hidden");
}
2024-12-08 02:59:45 +01:00
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);
}
2024-12-05 22:55:49 +01:00
2024-12-07 20:24:16 +01:00
function save_image() {
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;
}
draw(true);
}
function draw(viewport_scale) {
2024-12-08 02:59:45 +01:00
const filter = Object.entries(settings)
.map(([setting, {elements, filter}]) => `${setting}(${filter(elements[0].value)})`)
2024-12-08 02:59:45 +01:00
.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;
}
2024-12-08 02:59:45 +01:00
ctx.filter = filter
console.log(filter);
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
}