File size: 24,531 Bytes
46686e3 c82c483 46686e3 c82c483 46686e3 c82c483 46686e3 c82c483 46686e3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | <!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>LaMa large 512px Inpainting Demo</title>
<style>
/* Brush look and wheel/pan behaviour follow the StoneCandy Translator editor
(MaskLayer refined color 172,0,187 at alpha 140, zoom x1.15, Shift+wheel 5/20, Alt+wheel 2, size 5-250).
Tools match the editor: W inpaint brush, E inpaint eraser, R overlay brush, T overlay eraser, Tab show inpaint area,
right-click on a brush = its eraser, overlay color default #ffffff. */
:root { --line: #e5e7eb; --text: #191c21; --muted: #6b7280; --accent: #289BB2; --accent-base: #31B9D4; --stage: #2b2d31; }
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { display: flex; flex-direction: column; font: 13px/1.4 system-ui, -apple-system, "Segoe UI", "Malgun Gothic", sans-serif; color: var(--text); background: #fff; }
.bar { flex: none; display: flex; align-items: center; gap: 10px; height: 44px; padding: 0 12px; border-bottom: 1px solid var(--line); }
.bar b { font-size: 14px; }
.chip { padding: 1px 6px; border: 1px solid var(--line); border-radius: 4px; background: #f9fafb; color: var(--muted); font-size: 10.5px; }
.size { margin-left: auto; color: var(--muted); font-variant-numeric: tabular-nums; }
.size span { display: inline-block; min-width: 28px; color: var(--text); font-weight: 600; text-align: right; }
button { font: inherit; padding: 4px 10px; border: 1px solid var(--line); border-radius: 6px; background: #fff; color: #3d434d; cursor: pointer; }
button:hover { border-color: var(--accent-base); color: var(--accent); }
button:disabled { opacity: .45; cursor: default; border-color: var(--line); color: #3d434d; }
.stage { position: relative; flex: 1 1 auto; min-height: 0; overflow: hidden; background: var(--stage); }
.stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; cursor: none; touch-action: none; }
.stage.panning canvas { cursor: grabbing; }
.busy { position: absolute; left: 50%; top: 14px; transform: translateX(-50%); padding: 5px 12px; border-radius: 999px; background: rgba(0,0,0,.65); color: #fff; font-size: 12px; pointer-events: none; }
.busy[hidden] { display: none; }
.notice { position: absolute; right: 8px; bottom: 6px; max-width: 340px; font-size: 10.5px; line-height: 1.35; color: rgba(255,255,255,.6); text-align: right; pointer-events: none; }
/* tool frames: a column of buttons in the stage's top-right corner */
.tools { position: absolute; top: 12px; right: 12px; display: flex; flex-direction: column; gap: 6px; width: 148px; z-index: 2; }
.tools button { display: flex; align-items: center; gap: 8px; width: 100%; padding: 7px 10px; text-align: left;
border: 1px solid var(--line); border-radius: 7px; background: #fff; color: #3d434d; }
.tools button:hover { border-color: var(--accent-base); color: var(--accent); }
.tools button.active { border-color: var(--accent); color: var(--accent); font-weight: 600; box-shadow: inset 0 0 0 1px var(--accent); }
.tools button { font-size: 12px; }
.tools button .name { flex: 1; }
.tools button kbd { font-size: 10px; }
.tools .sep { height: 6px; }
.swatch { width: 20px; height: 10px; flex: none; border-radius: 3px; border: 1px solid #9ca3af; background: #ffffff; }
/* color picker popover: pick, then 확인 applies / 닫기 (or Esc) cancels */
.picker { position: absolute; top: 12px; right: 172px; width: 214px; padding: 10px; z-index: 3;
border: 1px solid var(--line); border-radius: 8px; background: #fff; box-shadow: 0 6px 20px rgba(0,0,0,.28); }
.picker[hidden] { display: none; }
.picker-head { display: flex; align-items: center; margin-bottom: 8px; font-weight: 600; font-size: 12px; }
.picker-head .x { margin-left: auto; padding: 0 7px; line-height: 20px; font-size: 15px; }
.sv { position: relative; height: 120px; border-radius: 5px; cursor: crosshair; touch-action: none;
background: linear-gradient(to top, #000, transparent), linear-gradient(to right, #fff, transparent); }
.sv i { position: absolute; width: 12px; height: 12px; margin: -6px 0 0 -6px; border: 2px solid #fff; border-radius: 50%; box-shadow: 0 0 0 1px rgba(0,0,0,.5); pointer-events: none; }
.hue { display: block; width: 100%; height: 12px; margin: 8px 0; -webkit-appearance: none; appearance: none; border-radius: 6px; outline: none; cursor: pointer;
background: linear-gradient(to right, #f00, #ff0, #0f0, #0ff, #00f, #f0f, #f00); }
.hue::-webkit-slider-thumb { -webkit-appearance: none; width: 14px; height: 14px; border-radius: 50%; background: #fff; border: 1px solid #6b7280; }
.hue::-moz-range-thumb { width: 14px; height: 14px; border-radius: 50%; background: #fff; border: 1px solid #6b7280; }
.presets { display: grid; grid-template-columns: repeat(8, 1fr); gap: 4px; margin-bottom: 8px; }
.presets button { height: 18px; padding: 0; border-radius: 3px; border: 1px solid #9ca3af; }
.picker-row { display: flex; align-items: center; gap: 6px; }
.picker-row .now { width: 26px; height: 22px; flex: none; border-radius: 4px; border: 1px solid #9ca3af; }
.picker-row input { width: 0; flex: 1; padding: 3px 6px; border: 1px solid var(--line); border-radius: 5px; font: 12px ui-monospace, Consolas, monospace; }
.picker-btns { display: flex; justify-content: flex-end; gap: 6px; margin-top: 10px; }
.picker-btns .ok { border-color: var(--accent); background: var(--accent); color: #fff; }
.picker-btns .ok:hover { background: var(--accent-base); color: #fff; }
.foot { flex: none; display: flex; align-items: center; gap: 12px; min-height: 32px; padding: 6px 12px; border-top: 1px solid var(--line); color: var(--muted); font-size: 12px; }
.foot .status { margin-left: auto; white-space: nowrap; }
kbd { padding: 0 4px; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 3px; background: #f9fafb; font: 11px ui-monospace, Consolas, monospace; color: #3d434d; }
</style>
</head>
<body>
<div class="bar">
<b>LaMa large 512px Inpainting Demo</b><span class="chip">lama_large_512px · ONNX · CPU</span>
<span class="size">Brush Size <span id="size">50</span> px</span>
<button id="undo" disabled>Undo</button>
<button id="fit">Fit</button>
<button id="reset">Reset</button>
</div>
<div class="stage" id="stage"><canvas id="view"></canvas><div class="busy" id="busy" hidden>Inpainting…</div>
<div class="tools" id="tools">
<button data-tool="brush" class="active"><span class="name">인페인트 브러시</span><kbd>W</kbd></button>
<button data-tool="eraser"><span class="name">인페인트 지우개</span><kbd>E</kbd></button>
<button data-tool="paint"><span class="name">덧칠 브러시</span><kbd>R</kbd></button>
<button data-tool="paint_erase"><span class="name">덧칠 지우개</span><kbd>T</kbd></button>
<div class="sep"></div>
<button id="colorBtn"><span class="name">컬러선택</span><span class="swatch" id="swatch"></span></button>
<button id="maskToggle"><span class="name">인페인트 영역 표시</span><kbd>Tab</kbd></button>
</div>
<div class="picker" id="picker" hidden>
<div class="picker-head">덧칠 컬러<button class="x" id="pickerX" aria-label="닫기">×</button></div>
<div class="sv" id="sv"><i id="svDot"></i></div>
<input class="hue" id="hue" type="range" min="0" max="360" value="0">
<div class="presets" id="presets"></div>
<div class="picker-row"><span class="now" id="pickNow"></span><input id="hex" maxlength="7" spellcheck="false"></div>
<div class="picker-btns"><button id="pickerCancel">닫기</button><button class="ok" id="pickerOk">확인</button></div>
</div>
<div class="notice">현재 Gradio를 생성할 수 없어 미리 작업된 이미지로 재연하고 있습니다.<br>Gradio can't be created right now, so this demo replays pre-processed images.</div></div>
<div class="foot">
<span><kbd>Drag</kbd> paint · <kbd>Right drag</kbd> eraser · <kbd>W</kbd><kbd>E</kbd><kbd>R</kbd><kbd>T</kbd> tools · <kbd>Tab</kbd> inpaint area · <kbd>Wheel</kbd> zoom · <kbd>Shift</kbd>/<kbd>Alt</kbd>+<kbd>Wheel</kbd> brush size · <kbd>Middle drag</kbd> pan · <kbd>Ctrl</kbd>+<kbd>Z</kbd> undo</span>
<span class="status" id="status">Loading…</span>
</div>
<script>
(function () {
var BRUSH = "rgb(172,0,187)", BRUSH_ALPHA = 140 / 255, PAD = 50;
var ERASE_PREVIEW = "rgb(255,255,255)", ERASE_ALPHA = 0.45;
var ZOOM = 1.15, MIN_SIZE = 5, MAX_SIZE = 250, MAX_UNDO = 30;
var stage = document.getElementById("stage"), view = document.getElementById("view"), ctx = view.getContext("2d");
var sizeEl = document.getElementById("size"), statusEl = document.getElementById("status"), busyEl = document.getElementById("busy");
var undoBtn = document.getElementById("undo");
var maskToggle = document.getElementById("maskToggle");
// base = current page (inpaint results baked in), mask = inpainted area, overlay = paint-over colors,
// stroke = the inpaint brush/eraser stroke in progress.
var base, bctx, mask, mctx, overlay, octx, stroke, sctx, W = 0, H = 0, sample = new Image(), clean = new Image();
var scale = 1, ox = 0, oy = 0, dpr = 1;
var size = 50, mouse = null, drawing = false, last = null, bbox = null, busy = false;
var panning = false, panFrom = null, spaceDown = false, undo = [];
var tool = "brush", strokeTool = null, paintColor = "#ffffff", showMask = false, overlayBefore = null;
function layer() { var c = document.createElement("canvas"); c.width = W; c.height = H; return c; }
// Copy a region with drawImage (not getImageData): opened as a local file the canvas is "tainted" and can't be read.
function snap(src, x, y, w, h) {
var c = document.createElement("canvas"); c.width = w; c.height = h;
c.getContext("2d").drawImage(src, -x, -y);
return c;
}
function putBack(dctx, img, x, y) { dctx.clearRect(x, y, img.width, img.height); dctx.drawImage(img, x, y); }
function pushUndo(u) { undo.push(u); if (undo.length > MAX_UNDO) undo.shift(); undoBtn.disabled = false; }
function resize() {
dpr = window.devicePixelRatio || 1;
view.width = Math.round(stage.clientWidth * dpr);
view.height = Math.round(stage.clientHeight * dpr);
render();
}
function fit() {
if (!W) return;
var sw = stage.clientWidth, sh = stage.clientHeight, m = 24;
scale = Math.min((sw - m * 2) / W, (sh - m * 2) / H);
ox = (sw - W * scale) / 2; oy = (sh - H * scale) / 2;
render();
}
function render() {
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.clearRect(0, 0, view.width, view.height);
if (!W) return;
ctx.setTransform(dpr * scale, 0, 0, dpr * scale, dpr * ox, dpr * oy);
ctx.imageSmoothingEnabled = scale < 2;
ctx.drawImage(base, 0, 0);
ctx.drawImage(overlay, 0, 0);
if (showMask) { ctx.globalAlpha = BRUSH_ALPHA; ctx.drawImage(mask, 0, 0); }
// strokes are opaque on their own layer, alpha applied once (no stacking)
ctx.globalAlpha = strokeTool === "eraser" ? ERASE_ALPHA : BRUSH_ALPHA;
ctx.drawImage(stroke, 0, 0);
ctx.globalAlpha = 1;
if (mouse && !panning) { // brush size circle
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
var r = Math.max(1, size * scale / 2);
ctx.beginPath(); ctx.arc(mouse.x, mouse.y, r, 0, Math.PI * 2);
ctx.lineWidth = 3; ctx.strokeStyle = "rgba(0,0,0,.55)"; ctx.stroke();
ctx.lineWidth = 1.2; ctx.strokeStyle = "rgba(255,255,255,.95)"; ctx.stroke();
}
}
function screenPos(e) { var r = view.getBoundingClientRect(); return { x: e.clientX - r.left, y: e.clientY - r.top }; }
function toImage(p) { return { x: (p.x - ox) / scale, y: (p.y - oy) / scale }; }
function setSize(v) { size = Math.max(MIN_SIZE, Math.min(MAX_SIZE, v)); sizeEl.textContent = size; render(); }
function setStatus(t) { statusEl.textContent = t; }
function setTool(t) {
tool = t;
document.querySelectorAll("#tools [data-tool]").forEach(function (b) { b.classList.toggle("active", b.getAttribute("data-tool") === t); });
}
function setShowMask(on) {
showMask = on;
maskToggle.classList.toggle("active", on);
render();
}
function setColor(c) { paintColor = c; document.getElementById("swatch").style.background = c; }
function dab(a, b) {
var c, color, erase = false;
if (strokeTool === "paint" || strokeTool === "paint_erase") { c = octx; color = paintColor; erase = strokeTool === "paint_erase"; }
else { c = sctx; color = strokeTool === "eraser" ? ERASE_PREVIEW : BRUSH; }
c.globalCompositeOperation = erase ? "destination-out" : "source-over";
c.lineCap = "round"; c.lineJoin = "round"; c.strokeStyle = erase ? "#000" : color; c.lineWidth = size;
c.beginPath(); c.moveTo(a.x, a.y); c.lineTo(b.x + 0.01, b.y); c.stroke();
c.globalCompositeOperation = "source-over";
var r = size / 2;
bbox = bbox || { x0: Infinity, y0: Infinity, x1: -Infinity, y1: -Infinity };
bbox.x0 = Math.min(bbox.x0, a.x - r, b.x - r); bbox.y0 = Math.min(bbox.y0, a.y - r, b.y - r);
bbox.x1 = Math.max(bbox.x1, a.x + r, b.x + r); bbox.y1 = Math.max(bbox.y1, a.y + r, b.y + r);
}
function region(b, pad) {
var x0 = Math.max(0, Math.floor(b.x0) - pad), y0 = Math.max(0, Math.floor(b.y0) - pad);
var x1 = Math.min(W, Math.ceil(b.x1) + pad), y1 = Math.min(H, Math.ceil(b.y1) + pad);
return (x1 <= x0 || y1 <= y0) ? null : { x: x0, y: y0, w: x1 - x0, h: y1 - y0 };
}
// Pixels of `src` inside the current stroke, cropped to the region.
function strokePatch(src, g) {
var c = document.createElement("canvas"); c.width = g.w; c.height = g.h;
var pc = c.getContext("2d");
pc.drawImage(stroke, -g.x, -g.y);
pc.globalCompositeOperation = "source-in";
pc.drawImage(src, -g.x, -g.y);
return c;
}
function finishStroke() { sctx.clearRect(0, 0, W, H); strokeTool = null; render(); }
// Inpaint brush, like the app: crop the stroke bbox + 50 px, inpaint it, paste it back on the current image.
// Replay: no server. The painted area takes the pre-cleaned page's pixels (the server pasted back only the mask too).
function commitInpaint() {
var g = region(bbox, PAD); bbox = null;
if (!g) { finishStroke(); return; }
busy = true; busyEl.hidden = false; setStatus("Inpainting…");
var t0 = performance.now();
new Promise(function (res) { setTimeout(res, 250 + Math.min(1200, g.w * g.h / 400)); }).then(function () {
pushUndo({ x: g.x, y: g.y, base: snap(base, g.x, g.y, g.w, g.h), mask: snap(mask, g.x, g.y, g.w, g.h) });
bctx.drawImage(strokePatch(clean, g), g.x, g.y);
mctx.drawImage(stroke, 0, 0);
setStatus("Last stroke " + ((performance.now() - t0) / 1000).toFixed(2) + " s · " + g.w + "×" + g.h + " px");
}).catch(function (err) {
setStatus("Inpaint failed (" + err.message + ")");
}).then(function () {
busy = false; busyEl.hidden = true;
finishStroke();
});
}
// Inpaint eraser: removes inpaint area — the original page comes back under the stroke.
function commitErase() {
var g = region(bbox, 2); bbox = null;
if (g) {
pushUndo({ x: g.x, y: g.y, base: snap(base, g.x, g.y, g.w, g.h), mask: snap(mask, g.x, g.y, g.w, g.h) });
bctx.drawImage(strokePatch(sample, g), g.x, g.y);
mctx.globalCompositeOperation = "destination-out"; mctx.drawImage(stroke, 0, 0); mctx.globalCompositeOperation = "source-over";
setStatus("Inpaint area erased · " + g.w + "×" + g.h + " px");
}
finishStroke();
}
// Overlay brush/eraser draw live on the overlay layer; undo keeps the touched region from before the stroke.
function commitOverlay() {
var g = region(bbox, 2); bbox = null;
if (g && overlayBefore) {
pushUndo({ x: g.x, y: g.y, overlay: snap(overlayBefore, g.x, g.y, g.w, g.h) });
setStatus((strokeTool === "paint" ? "Overlay painted · " : "Overlay erased · ") + g.w + "×" + g.h + " px");
}
overlayBefore = null;
finishStroke();
}
function commit() {
if (strokeTool === "brush") commitInpaint();
else if (strokeTool === "eraser") commitErase();
else commitOverlay();
}
function doUndo() {
var u = undo.pop();
if (!u || busy) { if (u) undo.push(u); return; }
if (u.base) putBack(bctx, u.base, u.x, u.y);
if (u.mask) putBack(mctx, u.mask, u.x, u.y);
if (u.overlay) putBack(octx, u.overlay, u.x, u.y);
undoBtn.disabled = !undo.length;
render();
}
function reset() {
if (busy || !W) return;
bctx.drawImage(sample, 0, 0);
mctx.clearRect(0, 0, W, H); octx.clearRect(0, 0, W, H);
undo = []; undoBtn.disabled = true;
setStatus("Reset to original");
render();
}
view.addEventListener("pointerdown", function (e) {
if (!W) return;
var p = screenPos(e);
if (e.button === 1 || (e.button === 0 && spaceDown)) { // middle drag (or Space + drag) pans
e.preventDefault();
panning = true; panFrom = { x: p.x - ox, y: p.y - oy };
stage.classList.add("panning");
view.setPointerCapture(e.pointerId);
return;
}
if ((e.button !== 0 && e.button !== 2) || busy) return;
// like the app: right-click on a brush works as its eraser
strokeTool = tool;
if (e.button === 2) strokeTool = (tool === "brush" || tool === "eraser") ? "eraser" : "paint_erase";
if (strokeTool === "paint" || strokeTool === "paint_erase") overlayBefore = snap(overlay, 0, 0, W, H);
view.setPointerCapture(e.pointerId);
drawing = true; last = toImage(p);
dab(last, last); render();
});
view.addEventListener("pointermove", function (e) {
mouse = screenPos(e);
if (panning) { ox = mouse.x - panFrom.x; oy = mouse.y - panFrom.y; }
else if (drawing) { var q = toImage(mouse); dab(last, q); last = q; }
render();
});
function endPointer() {
if (panning) { panning = false; stage.classList.remove("panning"); render(); return; }
if (drawing) { drawing = false; commit(); }
}
view.addEventListener("pointerup", endPointer);
view.addEventListener("pointercancel", endPointer);
view.addEventListener("pointerleave", function () { mouse = null; render(); });
view.addEventListener("contextmenu", function (e) { e.preventDefault(); });
view.addEventListener("auxclick", function (e) { if (e.button === 1) e.preventDefault(); });
view.addEventListener("mousedown", function (e) { if (e.button === 1) e.preventDefault(); }); // no autoscroll
view.addEventListener("wheel", function (e) {
e.preventDefault();
var d = e.deltaY || e.deltaX; // Shift/Alt+wheel often arrive as horizontal scroll
if (!d || !W) return;
var up = d < 0;
if (e.shiftKey) { var step = size <= 25 ? 5 : 20; setSize(size + (up ? step : -step)); return; }
if (e.altKey) { setSize(size + (up ? 2 : -2)); return; }
var p = screenPos(e), f = up ? ZOOM : 1 / ZOOM;
var ns = Math.max(0.05, Math.min(16, scale * f));
f = ns / scale;
ox = p.x - (p.x - ox) * f; oy = p.y - (p.y - oy) * f; scale = ns;
mouse = p; render();
}, { passive: false });
var KEYS = { w: "brush", e: "eraser", r: "paint", t: "paint_erase" };
document.addEventListener("keydown", function (e) {
if (!picker.hidden) { // picker open: Enter = 확인, Esc = 닫기; typing in the hex box stays there
if (e.key === "Enter") { e.preventDefault(); pickerOk(); }
else if (e.key === "Escape") { e.preventDefault(); closePicker(); }
if (e.target === hexIn) return;
}
if (e.code === "Space") { spaceDown = true; e.preventDefault(); }
if ((e.ctrlKey || e.metaKey) && (e.key === "z" || e.key === "Z")) { e.preventDefault(); doUndo(); return; }
if (e.ctrlKey || e.metaKey || e.altKey) return;
if (e.key === "Tab") { e.preventDefault(); setShowMask(!showMask); return; }
var t = KEYS[(e.key || "").toLowerCase()];
if (t) setTool(t);
});
document.addEventListener("keyup", function (e) { if (e.code === "Space") spaceDown = false; });
document.querySelectorAll("#tools [data-tool]").forEach(function (b) {
b.addEventListener("click", function () { setTool(b.getAttribute("data-tool")); });
});
// ---- color picker popover (HSV square + hue bar + presets + hex) ----
var picker = document.getElementById("picker"), sv = document.getElementById("sv"), svDot = document.getElementById("svDot");
var hueIn = document.getElementById("hue"), hexIn = document.getElementById("hex"), pickNow = document.getElementById("pickNow");
var ph = 0, ps = 0, pv = 1; // picked color while the popover is open
var PRESETS = ["#ffffff", "#f2f2f2", "#d9d9d9", "#a6a6a6", "#737373", "#404040", "#1a1a1a", "#000000",
"#ff4d4f", "#ff9f40", "#ffd84d", "#5ad65a", "#31b9d4", "#4d7cff", "#a24dff", "#ff9fb9"];
function hsvToHex(h, s, v) {
var f = function (n) { var k = (n + h / 60) % 6; return v - v * s * Math.max(0, Math.min(k, 4 - k, 1)); };
return "#" + [f(5), f(3), f(1)].map(function (x) { return ("0" + Math.round(x * 255).toString(16)).slice(-2); }).join("");
}
function hexToHsv(hex) {
var r = parseInt(hex.slice(1, 3), 16) / 255, g = parseInt(hex.slice(3, 5), 16) / 255, b = parseInt(hex.slice(5, 7), 16) / 255;
var max = Math.max(r, g, b), d = max - Math.min(r, g, b), h = 0;
if (d) h = max === r ? ((g - b) / d) % 6 : max === g ? (b - r) / d + 2 : (r - g) / d + 4;
return [(h * 60 + 360) % 360, max ? d / max : 0, max];
}
function showPick(fromHex) {
var hex = hsvToHex(ph, ps, pv);
sv.style.backgroundColor = "hsl(" + ph + ",100%,50%)";
svDot.style.left = (ps * 100) + "%"; svDot.style.top = ((1 - pv) * 100) + "%";
hueIn.value = ph; pickNow.style.background = hex;
if (!fromHex) hexIn.value = hex;
}
function pickHex(hex) { var t = hexToHsv(hex); ph = t[0]; ps = t[1]; pv = t[2]; showPick(); }
function openPicker() { pickHex(paintColor); picker.hidden = false; }
function closePicker() { picker.hidden = true; }
function pickerOk() { setColor(hsvToHex(ph, ps, pv)); closePicker(); }
PRESETS.forEach(function (c) {
var b = document.createElement("button"); b.style.background = c; b.title = c;
b.addEventListener("click", function () { pickHex(c); });
document.getElementById("presets").appendChild(b);
});
function svAt(e) {
var r = sv.getBoundingClientRect();
ps = Math.max(0, Math.min(1, (e.clientX - r.left) / r.width));
pv = 1 - Math.max(0, Math.min(1, (e.clientY - r.top) / r.height));
showPick();
}
sv.addEventListener("pointerdown", function (e) { sv.setPointerCapture(e.pointerId); svAt(e); });
sv.addEventListener("pointermove", function (e) { if (sv.hasPointerCapture(e.pointerId)) svAt(e); });
hueIn.addEventListener("input", function () { ph = +hueIn.value; showPick(); });
hexIn.addEventListener("input", function () {
var v = hexIn.value.trim(); if (v[0] !== "#") v = "#" + v;
if (/^#[0-9a-fA-F]{6}$/.test(v)) { var t = hexToHsv(v.toLowerCase()); ph = t[0]; ps = t[1]; pv = t[2]; showPick(true); }
});
document.getElementById("colorBtn").addEventListener("click", function () { picker.hidden ? openPicker() : closePicker(); });
document.getElementById("pickerOk").addEventListener("click", pickerOk);
document.getElementById("pickerCancel").addEventListener("click", closePicker);
document.getElementById("pickerX").addEventListener("click", closePicker);
maskToggle.addEventListener("click", function () { setShowMask(!showMask); });
undoBtn.addEventListener("click", doUndo);
document.getElementById("fit").addEventListener("click", fit);
document.getElementById("reset").addEventListener("click", reset);
new ResizeObserver(resize).observe(stage);
var loaded = 0;
function onBoth() { if (++loaded === 2) start(); }
function start() {
W = sample.naturalWidth; H = sample.naturalHeight;
base = layer(); bctx = base.getContext("2d"); bctx.drawImage(sample, 0, 0);
mask = layer(); mctx = mask.getContext("2d");
overlay = layer(); octx = overlay.getContext("2d");
stroke = layer(); sctx = stroke.getContext("2d");
resize(); fit();
setStatus("Ready — brush over the text");
}
sample.onload = clean.onload = onBoth;
sample.onerror = clean.onerror = function () { setStatus("Could not load the sample image"); };
sample.src = "images/original.png";
clean.src = "images/clean.png";
})();
</script>
</body>
</html>
|