Download model_test/index.html from StoneCandy/StoneCandy-Translator: direct link, hf CLI and curl.
- Browser
- Download file 24.5 kB
-
https://huggingface.co/spaces/StoneCandy/StoneCandy-Translator/resolve/main/model_test/index.html
- Command line
-
hf download hf://spaces/StoneCandy/StoneCandy-Translator/model_test/index.html
-
curl -L -o index.html https://huggingface.co/spaces/StoneCandy/StoneCandy-Translator/resolve/main/model_test/index.html
24.5 kB
| <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> | |