Spaces:
Running on Zero
Running on Zero
Download brush.js from concisewebsesign/picrest: direct link, hf CLI and curl.
- Browser
- Download file 2.99 kB
-
https://huggingface.co/spaces/concisewebsesign/picrest/resolve/main/brush.js
- Command line
-
hf download hf://spaces/concisewebsesign/picrest/brush.js
-
curl -L -o brush.js https://huggingface.co/spaces/concisewebsesign/picrest/resolve/main/brush.js
2.99 kB
| // PicRest paint-to-fix brush: a plain canvas so it behaves the same in every browser and on touch. | |
| // The photo is drawn on #pr-photo; strokes go on #pr-paint; prMask() exports the strokes as a | |
| // black/white PNG at the photo's real size. | |
| (() => { | |
| const S = { size: 30, drawing: false, last: null, w: 0, h: 0 }; | |
| const $ = (id) => document.getElementById(id); | |
| function setup() { | |
| const paint = $("pr-paint"); | |
| if (!paint || paint.dataset.ready) return; | |
| paint.dataset.ready = "1"; | |
| const pos = (e) => { | |
| const r = paint.getBoundingClientRect(); | |
| return [(e.clientX - r.left) * paint.width / r.width, (e.clientY - r.top) * paint.height / r.height]; | |
| }; | |
| const line = (a, b) => { | |
| const c = paint.getContext("2d"); | |
| c.strokeStyle = "rgba(255,59,48,0.55)"; c.lineWidth = S.size * paint.width / paint.getBoundingClientRect().width; | |
| c.lineCap = c.lineJoin = "round"; c.globalCompositeOperation = "source-over"; | |
| // draw opaque into the canvas so overlapping strokes don't get darker; opacity is set in CSS | |
| c.strokeStyle = "rgb(255,59,48)"; | |
| c.beginPath(); c.moveTo(a[0], a[1]); c.lineTo(b[0], b[1]); c.stroke(); | |
| }; | |
| paint.addEventListener("pointerdown", (e) => { | |
| if (!S.w) return; | |
| paint.setPointerCapture(e.pointerId); S.drawing = true; S.last = pos(e); line(S.last, S.last); e.preventDefault(); | |
| }); | |
| paint.addEventListener("pointermove", (e) => { | |
| if (!S.drawing) return; const p = pos(e); line(S.last, p); S.last = p; e.preventDefault(); | |
| }); | |
| const stop = () => { S.drawing = false; }; | |
| paint.addEventListener("pointerup", stop); paint.addEventListener("pointercancel", stop); | |
| } | |
| window.prLoad = (dataUrl) => { | |
| setup(); | |
| if (!dataUrl) return; | |
| const img = new Image(); | |
| img.onload = () => { | |
| const photo = $("pr-photo"), paint = $("pr-paint"); | |
| S.w = photo.width = paint.width = img.naturalWidth; | |
| S.h = photo.height = paint.height = img.naturalHeight; | |
| photo.getContext("2d").drawImage(img, 0, 0); | |
| const wrap = $("pr-wrap"); | |
| wrap.style.aspectRatio = S.w + " / " + S.h; | |
| wrap.style.width = "min(100%, calc(75vh * " + (S.w / S.h).toFixed(4) + "))"; // keep it on screen | |
| $("pr-empty").style.display = "none"; | |
| }; | |
| img.src = dataUrl; | |
| }; | |
| window.prClear = () => { const p = $("pr-paint"); if (p) p.getContext("2d").clearRect(0, 0, p.width, p.height); }; | |
| window.prSize = (v) => { S.size = Number(v); }; | |
| window.prMask = () => { | |
| const paint = $("pr-paint"); | |
| if (!paint || !S.w) return ""; | |
| const m = document.createElement("canvas"); m.width = S.w; m.height = S.h; | |
| const c = m.getContext("2d"); c.fillStyle = "#000"; c.fillRect(0, 0, S.w, S.h); | |
| c.drawImage(paint, 0, 0); // red strokes on black; the server treats any non-black pixel as painted | |
| return m.toDataURL("image/png"); | |
| }; | |
| new MutationObserver(setup).observe(document.documentElement, { childList: true, subtree: true }); | |
| })(); | |