2026-07-16 15:19:48 +02:00
|
|
|
(() => {
|
|
|
|
|
const fileInput = document.getElementById('file-input');
|
|
|
|
|
const dropZone = document.getElementById('drop-zone');
|
|
|
|
|
const dropCard = document.getElementById('drop-card');
|
|
|
|
|
const editorCard = document.getElementById('editor-card');
|
|
|
|
|
const canvas = document.getElementById('canvas');
|
|
|
|
|
const ctx = canvas.getContext('2d');
|
|
|
|
|
|
2026-07-17 12:16:52 +02:00
|
|
|
const aspectRow = document.getElementById('aspect-row');
|
|
|
|
|
const rotateLeftBtn = document.getElementById('rotate-left-btn');
|
|
|
|
|
const rotateRightBtn = document.getElementById('rotate-right-btn');
|
|
|
|
|
|
|
|
|
|
const presetRow = document.getElementById('preset-row');
|
2026-07-16 15:19:48 +02:00
|
|
|
const grainSlider = document.getElementById('grain');
|
2026-07-17 12:16:52 +02:00
|
|
|
const grainSizeSlider = document.getElementById('grain-size');
|
|
|
|
|
const grainColorToggle = document.getElementById('grain-color-toggle');
|
2026-07-16 15:19:48 +02:00
|
|
|
const vignetteSlider = document.getElementById('vignette');
|
|
|
|
|
const halationSlider = document.getElementById('halation');
|
2026-07-17 12:16:52 +02:00
|
|
|
const aberrationSlider = document.getElementById('aberration');
|
|
|
|
|
const dustSlider = document.getElementById('dust');
|
|
|
|
|
const rerollDustBtn = document.getElementById('reroll-dust');
|
2026-07-16 15:19:48 +02:00
|
|
|
const leakSlider = document.getElementById('leak');
|
2026-07-17 12:16:52 +02:00
|
|
|
const leakShapeRow = document.getElementById('leak-shape-row');
|
2026-07-16 15:19:48 +02:00
|
|
|
const rerollLeakBtn = document.getElementById('reroll-leak');
|
2026-07-17 12:16:52 +02:00
|
|
|
|
|
|
|
|
const addExposureBtn = document.getElementById('add-exposure-btn');
|
|
|
|
|
const removeExposureBtn = document.getElementById('remove-exposure-btn');
|
|
|
|
|
const exposureInput = document.getElementById('exposure-input');
|
|
|
|
|
const exposureControls = document.getElementById('exposure-controls');
|
|
|
|
|
const exposureBlend = document.getElementById('exposure-blend');
|
|
|
|
|
const exposureOpacity = document.getElementById('exposure-opacity');
|
2026-07-16 15:19:48 +02:00
|
|
|
|
|
|
|
|
const resetBtn = document.getElementById('reset-btn');
|
|
|
|
|
const newPhotoBtn = document.getElementById('new-photo-btn');
|
|
|
|
|
const downloadBtn = document.getElementById('download-btn');
|
|
|
|
|
const shareBtn = document.getElementById('share-btn');
|
|
|
|
|
const developBtn = document.getElementById('develop-btn');
|
|
|
|
|
const resultPanel = document.getElementById('result-panel');
|
|
|
|
|
|
|
|
|
|
// Offscreen canvases reused across renders so we're not allocating per frame.
|
2026-07-17 12:16:52 +02:00
|
|
|
const source = document.createElement('canvas'); // original, rotated, uncropped
|
|
|
|
|
const sourceCtx = source.getContext('2d');
|
2026-07-16 15:19:48 +02:00
|
|
|
const work = document.createElement('canvas');
|
|
|
|
|
const workCtx = work.getContext('2d');
|
|
|
|
|
const bloom = document.createElement('canvas');
|
|
|
|
|
const bloomCtx = bloom.getContext('2d');
|
|
|
|
|
const noiseCanvas = document.createElement('canvas');
|
|
|
|
|
const noiseCtx = noiseCanvas.getContext('2d');
|
2026-07-17 12:16:52 +02:00
|
|
|
const channelCanvas = document.createElement('canvas');
|
|
|
|
|
const channelCtx = channelCanvas.getContext('2d');
|
2026-07-16 15:19:48 +02:00
|
|
|
|
|
|
|
|
let originalImg = null;
|
2026-07-17 12:16:52 +02:00
|
|
|
let exposureImg = null;
|
2026-07-16 15:19:48 +02:00
|
|
|
|
|
|
|
|
const PRESETS = {
|
2026-07-17 12:16:52 +02:00
|
|
|
none: { filter: 'none' },
|
|
|
|
|
// "Pick a vibe" presets rather than raw white-balance sliders, per
|
2026-07-16 15:19:48 +02:00
|
|
|
// PROPOSAL.md — expressed as CSS filter strings applied at draw time.
|
2026-07-17 12:16:52 +02:00
|
|
|
warm: { filter: 'sepia(0.6) saturate(1.8) hue-rotate(-15deg) contrast(1.1) brightness(1.05)' },
|
|
|
|
|
cool: { filter: 'saturate(1.9) hue-rotate(165deg) contrast(1.2) brightness(0.9)' },
|
|
|
|
|
faded: { filter: 'sepia(0.2) saturate(0.35) contrast(0.7) brightness(1.15)' },
|
|
|
|
|
polaroid: { filter: 'contrast(1.25) brightness(0.92) saturate(0.85) hue-rotate(6deg) sepia(0.18)' },
|
|
|
|
|
infrared: { filter: 'hue-rotate(275deg) saturate(2.2) contrast(1.3) brightness(1.1)' },
|
|
|
|
|
bw: { filter: 'grayscale(1) contrast(1.15) brightness(1.05)' },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const ASPECTS = {
|
|
|
|
|
original: null,
|
|
|
|
|
square: 1,
|
|
|
|
|
portrait: 4 / 5,
|
|
|
|
|
landscape: 16 / 9,
|
2026-07-16 15:19:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const params = {
|
2026-07-17 12:16:52 +02:00
|
|
|
rotation: 0,
|
|
|
|
|
cropAspect: 'original',
|
2026-07-16 15:19:48 +02:00
|
|
|
preset: 'none',
|
|
|
|
|
grain: 0,
|
2026-07-17 12:16:52 +02:00
|
|
|
grainSize: 40,
|
|
|
|
|
grainColor: 'mono',
|
2026-07-16 15:19:48 +02:00
|
|
|
vignette: 0,
|
|
|
|
|
halation: 0,
|
2026-07-17 12:16:52 +02:00
|
|
|
aberration: 0,
|
|
|
|
|
dust: 0,
|
|
|
|
|
dustSeed: Math.random(),
|
2026-07-16 15:19:48 +02:00
|
|
|
leak: 0,
|
2026-07-17 12:16:52 +02:00
|
|
|
leakShape: 'bloom',
|
2026-07-16 15:19:48 +02:00
|
|
|
leakSeed: Math.random(),
|
2026-07-17 12:16:52 +02:00
|
|
|
exposureBlend: 'screen',
|
|
|
|
|
exposureOpacity: 50,
|
2026-07-16 15:19:48 +02:00
|
|
|
};
|
|
|
|
|
|
2026-07-17 12:16:52 +02:00
|
|
|
// Small seeded PRNG so dust/scratch placement stays fixed across
|
|
|
|
|
// slider tweaks and only changes on an explicit reroll.
|
|
|
|
|
function mulberry32(seed) {
|
|
|
|
|
let a = Math.floor(seed * 0xffffffff);
|
|
|
|
|
return function () {
|
|
|
|
|
a |= 0; a = (a + 0x6d2b79f5) | 0;
|
|
|
|
|
let t = Math.imul(a ^ (a >>> 15), 1 | a);
|
|
|
|
|
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
|
|
|
|
|
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-16 15:19:48 +02:00
|
|
|
function loadFile(file) {
|
|
|
|
|
if (!file || !file.type.startsWith('image/')) return;
|
|
|
|
|
const url = URL.createObjectURL(file);
|
|
|
|
|
const img = new Image();
|
|
|
|
|
img.onload = () => {
|
|
|
|
|
originalImg = img;
|
|
|
|
|
URL.revokeObjectURL(url);
|
|
|
|
|
dropCard.hidden = true;
|
|
|
|
|
editorCard.hidden = false;
|
|
|
|
|
resultPanel.hidden = true;
|
|
|
|
|
resetParams();
|
2026-07-17 12:16:52 +02:00
|
|
|
rebuildSource();
|
2026-07-16 15:19:48 +02:00
|
|
|
render();
|
|
|
|
|
};
|
|
|
|
|
img.src = url;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function resetParams() {
|
2026-07-17 12:16:52 +02:00
|
|
|
params.rotation = 0;
|
|
|
|
|
params.cropAspect = 'original';
|
2026-07-16 15:19:48 +02:00
|
|
|
params.preset = 'none';
|
|
|
|
|
params.grain = 0;
|
2026-07-17 12:16:52 +02:00
|
|
|
params.grainSize = 40;
|
|
|
|
|
params.grainColor = 'mono';
|
2026-07-16 15:19:48 +02:00
|
|
|
params.vignette = 0;
|
|
|
|
|
params.halation = 0;
|
2026-07-17 12:16:52 +02:00
|
|
|
params.aberration = 0;
|
|
|
|
|
params.dust = 0;
|
|
|
|
|
params.dustSeed = Math.random();
|
2026-07-16 15:19:48 +02:00
|
|
|
params.leak = 0;
|
2026-07-17 12:16:52 +02:00
|
|
|
params.leakShape = 'bloom';
|
2026-07-16 15:19:48 +02:00
|
|
|
params.leakSeed = Math.random();
|
2026-07-17 12:16:52 +02:00
|
|
|
exposureImg = null;
|
|
|
|
|
params.exposureBlend = 'screen';
|
|
|
|
|
params.exposureOpacity = 50;
|
|
|
|
|
|
2026-07-16 15:19:48 +02:00
|
|
|
grainSlider.value = 0;
|
2026-07-17 12:16:52 +02:00
|
|
|
grainSizeSlider.value = 40;
|
|
|
|
|
grainColorToggle.dataset.mode = 'mono';
|
|
|
|
|
grainColorToggle.textContent = 'grain: mono';
|
2026-07-16 15:19:48 +02:00
|
|
|
vignetteSlider.value = 0;
|
|
|
|
|
halationSlider.value = 0;
|
2026-07-17 12:16:52 +02:00
|
|
|
aberrationSlider.value = 0;
|
|
|
|
|
dustSlider.value = 0;
|
2026-07-16 15:19:48 +02:00
|
|
|
leakSlider.value = 0;
|
2026-07-17 12:16:52 +02:00
|
|
|
exposureBlend.value = 'screen';
|
|
|
|
|
exposureOpacity.value = 50;
|
|
|
|
|
exposureControls.hidden = true;
|
|
|
|
|
addExposureBtn.hidden = false;
|
|
|
|
|
removeExposureBtn.hidden = true;
|
|
|
|
|
exposureInput.value = '';
|
|
|
|
|
|
2026-07-16 15:19:48 +02:00
|
|
|
updateValueLabels();
|
|
|
|
|
[...presetRow.children].forEach(b => b.classList.toggle('active', b.dataset.preset === 'none'));
|
2026-07-17 12:16:52 +02:00
|
|
|
[...aspectRow.children].forEach(b => b.classList.toggle('active', b.dataset.aspect === 'original'));
|
|
|
|
|
[...leakShapeRow.children].forEach(b => b.classList.toggle('active', b.dataset.shape === 'bloom'));
|
2026-07-16 15:19:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function updateValueLabels() {
|
|
|
|
|
document.getElementById('grain-value').textContent = params.grain;
|
|
|
|
|
document.getElementById('vignette-value').textContent = params.vignette;
|
|
|
|
|
document.getElementById('halation-value').textContent = params.halation;
|
2026-07-17 12:16:52 +02:00
|
|
|
document.getElementById('aberration-value').textContent = params.aberration;
|
|
|
|
|
document.getElementById('dust-value').textContent = params.dust;
|
2026-07-16 15:19:48 +02:00
|
|
|
document.getElementById('leak-value').textContent = params.leak;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-17 12:16:52 +02:00
|
|
|
// Rebuilds `source` (the rotated + cropped, but otherwise unedited,
|
|
|
|
|
// base image) from originalImg. Only called when rotation, crop, or
|
|
|
|
|
// the loaded image change — not on every effect-slider tweak — since
|
|
|
|
|
// it's the one step that can't be recomputed from other canvases.
|
|
|
|
|
function rebuildSource() {
|
|
|
|
|
if (!originalImg) return;
|
|
|
|
|
const rot = params.rotation;
|
|
|
|
|
const iw = originalImg.naturalWidth;
|
|
|
|
|
const ih = originalImg.naturalHeight;
|
|
|
|
|
const swapped = rot === 90 || rot === 270;
|
|
|
|
|
const rotatedW = swapped ? ih : iw;
|
|
|
|
|
const rotatedH = swapped ? iw : ih;
|
|
|
|
|
|
|
|
|
|
const rotated = document.createElement('canvas');
|
|
|
|
|
rotated.width = rotatedW;
|
|
|
|
|
rotated.height = rotatedH;
|
|
|
|
|
const rc = rotated.getContext('2d');
|
|
|
|
|
rc.save();
|
|
|
|
|
rc.translate(rotatedW / 2, rotatedH / 2);
|
|
|
|
|
rc.rotate((rot * Math.PI) / 180);
|
|
|
|
|
rc.drawImage(originalImg, -iw / 2, -ih / 2);
|
|
|
|
|
rc.restore();
|
|
|
|
|
|
|
|
|
|
const targetAspect = ASPECTS[params.cropAspect];
|
|
|
|
|
let cropW = rotatedW;
|
|
|
|
|
let cropH = rotatedH;
|
|
|
|
|
let cropX = 0;
|
|
|
|
|
let cropY = 0;
|
|
|
|
|
if (targetAspect) {
|
|
|
|
|
const currentAspect = rotatedW / rotatedH;
|
|
|
|
|
if (currentAspect > targetAspect) {
|
|
|
|
|
cropW = Math.round(rotatedH * targetAspect);
|
|
|
|
|
cropX = Math.round((rotatedW - cropW) / 2);
|
|
|
|
|
} else {
|
|
|
|
|
cropH = Math.round(rotatedW / targetAspect);
|
|
|
|
|
cropY = Math.round((rotatedH - cropH) / 2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Cap so a raw phone photo doesn't choke canvas ops.
|
|
|
|
|
const maxDim = 2400;
|
|
|
|
|
let outW = cropW;
|
|
|
|
|
let outH = cropH;
|
|
|
|
|
if (Math.max(outW, outH) > maxDim) {
|
|
|
|
|
const scale = maxDim / Math.max(outW, outH);
|
|
|
|
|
outW = Math.round(outW * scale);
|
|
|
|
|
outH = Math.round(outH * scale);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
source.width = outW;
|
|
|
|
|
source.height = outH;
|
|
|
|
|
sourceCtx.clearRect(0, 0, outW, outH);
|
|
|
|
|
sourceCtx.drawImage(rotated, cropX, cropY, cropW, cropH, 0, 0, outW, outH);
|
|
|
|
|
|
|
|
|
|
canvas.width = outW;
|
|
|
|
|
canvas.height = outH;
|
|
|
|
|
work.width = outW;
|
|
|
|
|
work.height = outH;
|
|
|
|
|
bloom.width = outW;
|
|
|
|
|
bloom.height = outH;
|
|
|
|
|
channelCanvas.width = outW;
|
|
|
|
|
channelCanvas.height = outH;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Recomputes the full stack from `source` every time — never chains
|
|
|
|
|
// operations onto already-processed pixels — so dragging any slider
|
|
|
|
|
// back to 0 reproduces the source exactly.
|
2026-07-16 15:19:48 +02:00
|
|
|
function render() {
|
|
|
|
|
if (!originalImg) return;
|
|
|
|
|
const w = canvas.width;
|
|
|
|
|
const h = canvas.height;
|
|
|
|
|
|
|
|
|
|
workCtx.filter = PRESETS[params.preset].filter;
|
|
|
|
|
workCtx.globalCompositeOperation = 'source-over';
|
|
|
|
|
workCtx.clearRect(0, 0, w, h);
|
2026-07-17 12:16:52 +02:00
|
|
|
workCtx.drawImage(source, 0, 0);
|
2026-07-16 15:19:48 +02:00
|
|
|
workCtx.filter = 'none';
|
|
|
|
|
|
2026-07-17 12:16:52 +02:00
|
|
|
if (exposureImg) applyDoubleExposure(w, h);
|
2026-07-16 15:19:48 +02:00
|
|
|
if (params.halation > 0) applyHalation(w, h);
|
2026-07-17 12:16:52 +02:00
|
|
|
if (params.aberration > 0) applyChromaticAberration(w, h);
|
2026-07-16 15:19:48 +02:00
|
|
|
if (params.vignette > 0) applyVignette(w, h);
|
|
|
|
|
if (params.leak > 0) applyLightLeak(w, h);
|
2026-07-17 12:16:52 +02:00
|
|
|
if (params.dust > 0) applyDustScratches(w, h);
|
2026-07-16 15:19:48 +02:00
|
|
|
if (params.grain > 0) applyGrain(w, h);
|
|
|
|
|
|
|
|
|
|
ctx.clearRect(0, 0, w, h);
|
|
|
|
|
ctx.drawImage(work, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-17 12:16:52 +02:00
|
|
|
// Blends a second uploaded photo in, cover-fit to the canvas — the
|
|
|
|
|
// literal "double exposure" film trick.
|
|
|
|
|
function applyDoubleExposure(w, h) {
|
|
|
|
|
const iw = exposureImg.naturalWidth;
|
|
|
|
|
const ih = exposureImg.naturalHeight;
|
|
|
|
|
const scale = Math.max(w / iw, h / ih);
|
|
|
|
|
const dw = iw * scale;
|
|
|
|
|
const dh = ih * scale;
|
|
|
|
|
const dx = (w - dw) / 2;
|
|
|
|
|
const dy = (h - dh) / 2;
|
|
|
|
|
|
|
|
|
|
workCtx.save();
|
|
|
|
|
workCtx.globalCompositeOperation = params.exposureBlend;
|
|
|
|
|
workCtx.globalAlpha = params.exposureOpacity / 100;
|
|
|
|
|
workCtx.drawImage(exposureImg, dx, dy, dw, dh);
|
|
|
|
|
workCtx.restore();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Bloom around bright areas: blur a thresholded bright-pass and screen
|
|
|
|
|
// it back over the original.
|
2026-07-16 15:19:48 +02:00
|
|
|
function applyHalation(w, h) {
|
|
|
|
|
bloomCtx.clearRect(0, 0, w, h);
|
|
|
|
|
bloomCtx.filter = 'brightness(180%) contrast(400%) saturate(120%)';
|
|
|
|
|
bloomCtx.drawImage(work, 0, 0);
|
|
|
|
|
bloomCtx.filter = 'none';
|
|
|
|
|
|
|
|
|
|
const blurPx = Math.max(4, Math.round(Math.min(w, h) * 0.02));
|
|
|
|
|
bloomCtx.filter = `blur(${blurPx}px)`;
|
|
|
|
|
bloomCtx.drawImage(bloom, 0, 0);
|
|
|
|
|
bloomCtx.filter = 'none';
|
|
|
|
|
|
|
|
|
|
workCtx.save();
|
|
|
|
|
workCtx.globalCompositeOperation = 'screen';
|
|
|
|
|
workCtx.globalAlpha = (params.halation / 100) * 0.85;
|
|
|
|
|
workCtx.drawImage(bloom, 0, 0);
|
|
|
|
|
workCtx.restore();
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-17 12:16:52 +02:00
|
|
|
// Isolates a single color channel by multiplying against a solid
|
|
|
|
|
// pure-channel fill — cheap alternative to a per-pixel channel split.
|
|
|
|
|
function channelLayer(colorRgb) {
|
|
|
|
|
channelCtx.clearRect(0, 0, channelCanvas.width, channelCanvas.height);
|
|
|
|
|
channelCtx.globalCompositeOperation = 'source-over';
|
|
|
|
|
channelCtx.drawImage(work, 0, 0);
|
|
|
|
|
channelCtx.globalCompositeOperation = 'multiply';
|
|
|
|
|
channelCtx.fillStyle = colorRgb;
|
|
|
|
|
channelCtx.fillRect(0, 0, channelCanvas.width, channelCanvas.height);
|
|
|
|
|
return channelCanvas;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Cheap "lens fringing" look: shift isolated red/blue channel layers in
|
|
|
|
|
// opposite directions and screen them back over the full-color image.
|
|
|
|
|
// Not a physically accurate simulation, just a stylized fringe.
|
|
|
|
|
function applyChromaticAberration(w, h) {
|
|
|
|
|
const amount = params.aberration / 100;
|
|
|
|
|
const shift = Math.max(1, Math.round(amount * Math.max(w, h) * 0.015));
|
|
|
|
|
|
|
|
|
|
const redSnapshot = document.createElement('canvas');
|
|
|
|
|
redSnapshot.width = w; redSnapshot.height = h;
|
|
|
|
|
redSnapshot.getContext('2d').drawImage(channelLayer('rgb(255,0,0)'), 0, 0);
|
|
|
|
|
|
|
|
|
|
const blueSnapshot = document.createElement('canvas');
|
|
|
|
|
blueSnapshot.width = w; blueSnapshot.height = h;
|
|
|
|
|
blueSnapshot.getContext('2d').drawImage(channelLayer('rgb(0,0,255)'), 0, 0);
|
|
|
|
|
|
|
|
|
|
workCtx.save();
|
|
|
|
|
workCtx.globalCompositeOperation = 'screen';
|
|
|
|
|
workCtx.globalAlpha = 0.55;
|
|
|
|
|
workCtx.drawImage(redSnapshot, -shift, 0);
|
|
|
|
|
workCtx.drawImage(blueSnapshot, shift, 0);
|
|
|
|
|
workCtx.restore();
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-16 15:19:48 +02:00
|
|
|
function applyVignette(w, h) {
|
|
|
|
|
const amount = params.vignette / 100;
|
|
|
|
|
const cx = w / 2;
|
|
|
|
|
const cy = h / 2;
|
|
|
|
|
const outerR = Math.hypot(cx, cy);
|
|
|
|
|
const grad = workCtx.createRadialGradient(cx, cy, outerR * 0.35, cx, cy, outerR);
|
|
|
|
|
grad.addColorStop(0, 'rgba(0,0,0,0)');
|
|
|
|
|
grad.addColorStop(1, `rgba(0,0,0,${amount * 0.85})`);
|
|
|
|
|
workCtx.save();
|
|
|
|
|
workCtx.globalCompositeOperation = 'multiply';
|
|
|
|
|
workCtx.fillStyle = grad;
|
|
|
|
|
workCtx.fillRect(0, 0, w, h);
|
|
|
|
|
workCtx.restore();
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-17 12:16:52 +02:00
|
|
|
// Three shapes off one seed so repeat use doesn't look identical, and
|
|
|
|
|
// no pre-baked PNG assets are needed — it's all procedural gradients.
|
2026-07-16 15:19:48 +02:00
|
|
|
function applyLightLeak(w, h) {
|
|
|
|
|
const seed = params.leakSeed;
|
2026-07-17 12:16:52 +02:00
|
|
|
const alpha = params.leak / 100;
|
2026-07-16 15:19:48 +02:00
|
|
|
const hue = 25 + seed * 30; // warm amber/red range
|
|
|
|
|
|
|
|
|
|
workCtx.save();
|
|
|
|
|
workCtx.globalCompositeOperation = 'screen';
|
2026-07-17 12:16:52 +02:00
|
|
|
|
|
|
|
|
if (params.leakShape === 'streak') {
|
|
|
|
|
const angle = seed * Math.PI * 2;
|
|
|
|
|
const cx = w * (0.2 + seed * 0.6);
|
|
|
|
|
const cy = h * (0.2 + (1 - seed) * 0.6);
|
|
|
|
|
const length = Math.max(w, h) * 1.4;
|
|
|
|
|
const x0 = cx - Math.cos(angle) * length / 2;
|
|
|
|
|
const y0 = cy - Math.sin(angle) * length / 2;
|
|
|
|
|
const x1 = cx + Math.cos(angle) * length / 2;
|
|
|
|
|
const y1 = cy + Math.sin(angle) * length / 2;
|
|
|
|
|
const grad = workCtx.createLinearGradient(x0, y0, x1, y1);
|
|
|
|
|
grad.addColorStop(0, 'hsla(0, 0%, 0%, 0)');
|
|
|
|
|
grad.addColorStop(0.5, `hsla(${hue}, 90%, 60%, ${alpha * 0.75})`);
|
|
|
|
|
grad.addColorStop(1, 'hsla(0, 0%, 0%, 0)');
|
|
|
|
|
workCtx.fillStyle = grad;
|
|
|
|
|
workCtx.fillRect(0, 0, w, h);
|
|
|
|
|
} else if (params.leakShape === 'corner') {
|
|
|
|
|
const corners = [[0, 0], [w, 0], [0, h], [w, h]];
|
|
|
|
|
const [cx, cy] = corners[Math.floor(seed * 4) % 4];
|
|
|
|
|
const radius = Math.max(w, h) * (0.55 + seed * 0.3);
|
|
|
|
|
const grad = workCtx.createRadialGradient(cx, cy, 0, cx, cy, radius);
|
|
|
|
|
grad.addColorStop(0, `hsla(${hue}, 90%, 60%, ${alpha * 0.9})`);
|
|
|
|
|
grad.addColorStop(1, 'hsla(0, 0%, 0%, 0)');
|
|
|
|
|
workCtx.fillStyle = grad;
|
|
|
|
|
workCtx.fillRect(0, 0, w, h);
|
|
|
|
|
} else { // bloom — a blob placed near a randomized edge point
|
|
|
|
|
const angle = seed * Math.PI * 2;
|
|
|
|
|
const edgeX = w / 2 + Math.cos(angle) * w * 0.7;
|
|
|
|
|
const edgeY = h / 2 + Math.sin(angle) * h * 0.7;
|
|
|
|
|
const radius = Math.max(w, h) * (0.5 + seed * 0.3);
|
|
|
|
|
const grad = workCtx.createRadialGradient(edgeX, edgeY, 0, edgeX, edgeY, radius);
|
|
|
|
|
grad.addColorStop(0, `hsla(${hue}, 90%, 60%, ${alpha * 0.9})`);
|
|
|
|
|
grad.addColorStop(0.5, `hsla(${hue + 10}, 90%, 55%, ${alpha * 0.35})`);
|
|
|
|
|
grad.addColorStop(1, 'hsla(0, 0%, 0%, 0)');
|
|
|
|
|
workCtx.fillStyle = grad;
|
|
|
|
|
workCtx.fillRect(0, 0, w, h);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
workCtx.restore();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Random hairline scratches + specks of dust, regenerated only on
|
|
|
|
|
// reroll (seeded), not on every slider tweak.
|
|
|
|
|
function applyDustScratches(w, h) {
|
|
|
|
|
const amount = params.dust / 100;
|
|
|
|
|
const rng = mulberry32(params.dustSeed);
|
|
|
|
|
|
|
|
|
|
workCtx.save();
|
|
|
|
|
workCtx.globalCompositeOperation = 'screen';
|
|
|
|
|
|
|
|
|
|
const scratchCount = Math.round(amount * 12);
|
|
|
|
|
for (let i = 0; i < scratchCount; i++) {
|
|
|
|
|
workCtx.strokeStyle = `rgba(255,255,255,${0.05 + rng() * 0.15})`;
|
|
|
|
|
workCtx.lineWidth = 1;
|
|
|
|
|
const x = rng() * w;
|
|
|
|
|
workCtx.beginPath();
|
|
|
|
|
workCtx.moveTo(x, 0);
|
|
|
|
|
workCtx.lineTo(x + (rng() - 0.5) * 24, h);
|
|
|
|
|
workCtx.stroke();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const dustCount = Math.round(amount * 90);
|
|
|
|
|
for (let i = 0; i < dustCount; i++) {
|
|
|
|
|
workCtx.fillStyle = `rgba(255,255,255,${0.2 + rng() * 0.5})`;
|
|
|
|
|
const x = rng() * w;
|
|
|
|
|
const y = rng() * h;
|
|
|
|
|
const r = 0.5 + rng() * 1.5;
|
|
|
|
|
workCtx.beginPath();
|
|
|
|
|
workCtx.arc(x, y, r, 0, Math.PI * 2);
|
|
|
|
|
workCtx.fill();
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-16 15:19:48 +02:00
|
|
|
workCtx.restore();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function applyGrain(w, h) {
|
|
|
|
|
// Noise is generated at reduced resolution and scaled up — a
|
|
|
|
|
// per-pixel random overlay at full phone-photo resolution is
|
|
|
|
|
// needlessly slow and reads no differently once blended in.
|
2026-07-17 12:16:52 +02:00
|
|
|
// grainSize maps a bigger divisor to coarser, more visible grain.
|
|
|
|
|
const divisor = 1.5 + (params.grainSize / 100) * 8.5;
|
|
|
|
|
const nw = Math.max(1, Math.round(w / divisor));
|
|
|
|
|
const nh = Math.max(1, Math.round(h / divisor));
|
2026-07-16 15:19:48 +02:00
|
|
|
noiseCanvas.width = nw;
|
|
|
|
|
noiseCanvas.height = nh;
|
|
|
|
|
const imgData = noiseCtx.createImageData(nw, nh);
|
|
|
|
|
const d = imgData.data;
|
2026-07-17 12:16:52 +02:00
|
|
|
if (params.grainColor === 'color') {
|
|
|
|
|
for (let i = 0; i < d.length; i += 4) {
|
|
|
|
|
d[i] = Math.random() * 255;
|
|
|
|
|
d[i + 1] = Math.random() * 255;
|
|
|
|
|
d[i + 2] = Math.random() * 255;
|
|
|
|
|
d[i + 3] = 255;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
for (let i = 0; i < d.length; i += 4) {
|
|
|
|
|
const v = Math.random() * 255;
|
|
|
|
|
d[i] = v;
|
|
|
|
|
d[i + 1] = v;
|
|
|
|
|
d[i + 2] = v;
|
|
|
|
|
d[i + 3] = 255;
|
|
|
|
|
}
|
2026-07-16 15:19:48 +02:00
|
|
|
}
|
|
|
|
|
noiseCtx.putImageData(imgData, 0, 0);
|
|
|
|
|
|
|
|
|
|
workCtx.save();
|
|
|
|
|
workCtx.globalCompositeOperation = 'overlay';
|
|
|
|
|
workCtx.globalAlpha = (params.grain / 100) * 0.6;
|
|
|
|
|
workCtx.imageSmoothingEnabled = false;
|
|
|
|
|
workCtx.drawImage(noiseCanvas, 0, 0, w, h);
|
|
|
|
|
workCtx.restore();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function showResult(html, isError) {
|
|
|
|
|
resultPanel.innerHTML = html;
|
|
|
|
|
resultPanel.classList.toggle('error', !!isError);
|
|
|
|
|
resultPanel.hidden = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function canvasToBlob() {
|
|
|
|
|
return new Promise(resolve => canvas.toBlob(resolve, 'image/jpeg', 0.92));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function upload(delayed) {
|
|
|
|
|
const btn = delayed ? developBtn : shareBtn;
|
|
|
|
|
btn.disabled = true;
|
|
|
|
|
showResult('uploading…');
|
|
|
|
|
try {
|
|
|
|
|
const blob = await canvasToBlob();
|
|
|
|
|
const res = await fetch(`/api/upload?delayed=${delayed}`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'image/jpeg' },
|
|
|
|
|
body: blob,
|
|
|
|
|
});
|
|
|
|
|
if (!res.ok) {
|
|
|
|
|
const body = await res.json().catch(() => ({}));
|
|
|
|
|
throw new Error(body.error || `upload failed (${res.status})`);
|
|
|
|
|
}
|
|
|
|
|
const data = await res.json();
|
|
|
|
|
const link = `${location.origin}${data.url}`;
|
|
|
|
|
if (delayed) {
|
|
|
|
|
showResult(
|
|
|
|
|
`sent to develop. link: <a href="${link}">${link}</a><br>` +
|
|
|
|
|
`ready sometime in the next ${data.developInHours} hours — ` +
|
|
|
|
|
`same link resolves once it's done.`
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
showResult(`share link: <a href="${link}">${link}</a>`);
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
showResult(err.message || 'something went wrong', true);
|
|
|
|
|
} finally {
|
|
|
|
|
btn.disabled = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileInput.addEventListener('change', () => loadFile(fileInput.files[0]));
|
|
|
|
|
|
|
|
|
|
['dragover', 'dragenter'].forEach(evt =>
|
|
|
|
|
dropZone.addEventListener(evt, e => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
dropZone.classList.add('drag-over');
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
['dragleave', 'dragend', 'drop'].forEach(evt =>
|
|
|
|
|
dropZone.addEventListener(evt, () => dropZone.classList.remove('drag-over'))
|
|
|
|
|
);
|
|
|
|
|
dropZone.addEventListener('drop', e => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
loadFile(e.dataTransfer.files[0]);
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-17 12:16:52 +02:00
|
|
|
[...aspectRow.children].forEach(btn => {
|
|
|
|
|
btn.addEventListener('click', () => {
|
|
|
|
|
params.cropAspect = btn.dataset.aspect;
|
|
|
|
|
[...aspectRow.children].forEach(b => b.classList.toggle('active', b === btn));
|
|
|
|
|
rebuildSource();
|
|
|
|
|
render();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
rotateLeftBtn.addEventListener('click', () => {
|
|
|
|
|
params.rotation = (params.rotation + 270) % 360;
|
|
|
|
|
rebuildSource();
|
|
|
|
|
render();
|
|
|
|
|
});
|
|
|
|
|
rotateRightBtn.addEventListener('click', () => {
|
|
|
|
|
params.rotation = (params.rotation + 90) % 360;
|
|
|
|
|
rebuildSource();
|
|
|
|
|
render();
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-16 15:19:48 +02:00
|
|
|
[...presetRow.children].forEach(btn => {
|
|
|
|
|
btn.addEventListener('click', () => {
|
|
|
|
|
params.preset = btn.dataset.preset;
|
|
|
|
|
[...presetRow.children].forEach(b => b.classList.toggle('active', b === btn));
|
|
|
|
|
render();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-17 12:16:52 +02:00
|
|
|
[...leakShapeRow.children].forEach(btn => {
|
|
|
|
|
btn.addEventListener('click', () => {
|
|
|
|
|
params.leakShape = btn.dataset.shape;
|
|
|
|
|
[...leakShapeRow.children].forEach(b => b.classList.toggle('active', b === btn));
|
|
|
|
|
render();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-16 15:19:48 +02:00
|
|
|
grainSlider.addEventListener('input', () => { params.grain = Number(grainSlider.value); updateValueLabels(); render(); });
|
2026-07-17 12:16:52 +02:00
|
|
|
grainSizeSlider.addEventListener('input', () => { params.grainSize = Number(grainSizeSlider.value); render(); });
|
|
|
|
|
grainColorToggle.addEventListener('click', () => {
|
|
|
|
|
params.grainColor = params.grainColor === 'mono' ? 'color' : 'mono';
|
|
|
|
|
grainColorToggle.dataset.mode = params.grainColor;
|
|
|
|
|
grainColorToggle.textContent = `grain: ${params.grainColor}`;
|
|
|
|
|
render();
|
|
|
|
|
});
|
2026-07-16 15:19:48 +02:00
|
|
|
vignetteSlider.addEventListener('input', () => { params.vignette = Number(vignetteSlider.value); updateValueLabels(); render(); });
|
|
|
|
|
halationSlider.addEventListener('input', () => { params.halation = Number(halationSlider.value); updateValueLabels(); render(); });
|
2026-07-17 12:16:52 +02:00
|
|
|
aberrationSlider.addEventListener('input', () => { params.aberration = Number(aberrationSlider.value); updateValueLabels(); render(); });
|
|
|
|
|
dustSlider.addEventListener('input', () => { params.dust = Number(dustSlider.value); updateValueLabels(); render(); });
|
|
|
|
|
rerollDustBtn.addEventListener('click', () => { params.dustSeed = Math.random(); render(); });
|
2026-07-16 15:19:48 +02:00
|
|
|
leakSlider.addEventListener('input', () => { params.leak = Number(leakSlider.value); updateValueLabels(); render(); });
|
|
|
|
|
rerollLeakBtn.addEventListener('click', () => { params.leakSeed = Math.random(); render(); });
|
|
|
|
|
|
2026-07-17 12:16:52 +02:00
|
|
|
addExposureBtn.addEventListener('click', () => exposureInput.click());
|
|
|
|
|
exposureInput.addEventListener('change', () => {
|
|
|
|
|
const file = exposureInput.files[0];
|
|
|
|
|
if (!file || !file.type.startsWith('image/')) return;
|
|
|
|
|
const url = URL.createObjectURL(file);
|
|
|
|
|
const img = new Image();
|
|
|
|
|
img.onload = () => {
|
|
|
|
|
exposureImg = img;
|
|
|
|
|
URL.revokeObjectURL(url);
|
|
|
|
|
addExposureBtn.hidden = true;
|
|
|
|
|
removeExposureBtn.hidden = false;
|
|
|
|
|
exposureControls.hidden = false;
|
|
|
|
|
render();
|
|
|
|
|
};
|
|
|
|
|
img.src = url;
|
|
|
|
|
});
|
|
|
|
|
removeExposureBtn.addEventListener('click', () => {
|
|
|
|
|
exposureImg = null;
|
|
|
|
|
exposureInput.value = '';
|
|
|
|
|
addExposureBtn.hidden = false;
|
|
|
|
|
removeExposureBtn.hidden = true;
|
|
|
|
|
exposureControls.hidden = true;
|
|
|
|
|
render();
|
|
|
|
|
});
|
|
|
|
|
exposureBlend.addEventListener('change', () => { params.exposureBlend = exposureBlend.value; render(); });
|
|
|
|
|
exposureOpacity.addEventListener('input', () => { params.exposureOpacity = Number(exposureOpacity.value); render(); });
|
|
|
|
|
|
|
|
|
|
resetBtn.addEventListener('click', () => { resetParams(); rebuildSource(); render(); });
|
2026-07-16 15:19:48 +02:00
|
|
|
newPhotoBtn.addEventListener('click', () => {
|
|
|
|
|
originalImg = null;
|
2026-07-17 12:16:52 +02:00
|
|
|
exposureImg = null;
|
2026-07-16 15:19:48 +02:00
|
|
|
fileInput.value = '';
|
|
|
|
|
editorCard.hidden = true;
|
|
|
|
|
dropCard.hidden = false;
|
|
|
|
|
resultPanel.hidden = true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
downloadBtn.addEventListener('click', async () => {
|
|
|
|
|
const blob = await canvasToBlob();
|
|
|
|
|
const url = URL.createObjectURL(blob);
|
|
|
|
|
const a = document.createElement('a');
|
|
|
|
|
a.href = url;
|
|
|
|
|
a.download = `latent-${Date.now()}.jpg`;
|
|
|
|
|
a.click();
|
|
|
|
|
URL.revokeObjectURL(url);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
shareBtn.addEventListener('click', () => upload(false));
|
|
|
|
|
developBtn.addEventListener('click', () => upload(true));
|
|
|
|
|
})();
|