mymuse / mask-tool.html
Crocody's picture
Upload mask-tool.html
eca2f30 verified
Raw History Blame Contribute Delete
12 kB
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>이미지 가리기 도구</title>
<style>
:root{
--bg:#14161a; --panel:#1d2027; --panel-2:#252932; --line:#333845;
--ink:#e7e9ec; --muted:#9299a6; --accent:#5b8def; --danger:#d9534f;
--radius:10px;
}
*{box-sizing:border-box}
html,body{margin:0;height:100%}
body{
background:var(--bg); color:var(--ink);
font-family:"Pretendard","Apple SD Gothic Neo","Noto Sans KR",system-ui,sans-serif;
font-size:15px; line-height:1.55;
display:flex; flex-direction:column;
}
header{
padding:14px 20px; border-bottom:1px solid var(--line);
display:flex; align-items:baseline; gap:14px; flex-wrap:wrap;
}
header h1{font-size:17px; font-weight:600; margin:0; letter-spacing:-.01em}
header p{margin:0; color:var(--muted); font-size:13px}
main{flex:1; display:flex; min-height:0}
@media (max-width:860px){ main{flex-direction:column} }
/* ---- workspace ---- */
#stage{
flex:1; min-height:0; display:flex; align-items:center; justify-content:center;
padding:20px; overflow:auto; position:relative;
}
#drop{
width:min(520px,100%); border:1.5px dashed var(--line); border-radius:var(--radius);
padding:48px 28px; text-align:center; background:var(--panel);
}
#drop.over{border-color:var(--accent); background:var(--panel-2)}
#drop h2{margin:0 0 6px; font-size:16px; font-weight:600}
#drop p{margin:0 0 20px; color:var(--muted); font-size:13px}
canvas{
max-width:100%; max-height:calc(100vh - 160px);
background:#000; border-radius:6px; cursor:crosshair; touch-action:none;
box-shadow:0 0 0 1px var(--line);
}
#canvasWrap{display:none}
/* ---- panel ---- */
aside{
width:290px; flex-shrink:0; border-left:1px solid var(--line);
background:var(--panel); padding:18px; overflow:auto;
}
@media (max-width:860px){ aside{width:auto; border-left:0; border-top:1px solid var(--line)} }
.group{margin-bottom:22px}
.group > span{display:block; font-size:12px; color:var(--muted); margin-bottom:8px}
.seg{display:flex; gap:6px}
.seg button{flex:1}
button{
font:inherit; font-size:13.5px; color:var(--ink); background:var(--panel-2);
border:1px solid var(--line); border-radius:8px; padding:9px 12px; cursor:pointer;
}
button:hover{border-color:#4a5162}
button:focus-visible{outline:2px solid var(--accent); outline-offset:2px}
button[aria-pressed="true"]{background:var(--accent); border-color:var(--accent); color:#fff}
button:disabled{opacity:.4; cursor:default}
.full{width:100%}
.row{display:flex; gap:6px}
.row button{flex:1}
#save{background:var(--accent); border-color:var(--accent); color:#fff; font-weight:600; padding:12px}
#save:hover{background:#4d7fe0}
#clear{color:var(--danger); border-color:#4a3237}
input[type=range]{width:100%; accent-color:var(--accent)}
.tips{font-size:12.5px; color:var(--muted); line-height:1.7; margin:0; padding-left:16px}
.tips li{margin-bottom:4px}
.count{font-size:12px; color:var(--muted)}
.note{
font-size:12.5px; color:var(--muted); border-top:1px solid var(--line);
padding-top:14px; margin-top:4px;
}
</style>
</head>
<body>
<header>
<h1>이미지 가리기 도구</h1>
<p>사진은 업로드되지 않고 이 브라우저 안에서만 처리됩니다.</p>
</header>
<main>
<div id="stage">
<div id="drop">
<h2>이미지를 끌어다 놓으세요</h2>
<p>JPG, PNG, WebP</p>
<button id="pick">파일 선택</button>
<input id="file" type="file" accept="image/*" hidden>
</div>
<div id="canvasWrap"><canvas id="cv"></canvas></div>
</div>
<aside>
<div class="group">
<span>가리는 방식</span>
<div class="seg">
<button data-mode="solid" aria-pressed="true">검정 원</button>
<button data-mode="blur" aria-pressed="false">블러</button>
<button data-mode="pixel" aria-pressed="false">모자이크</button>
</div>
</div>
<div class="group">
<span>영역 크기 (다음 원에도 그대로 적용)</span>
<input id="size" type="range" min="10" max="600" value="90" disabled>
</div>
<div class="group">
<div class="row">
<button id="undo" disabled>되돌리기</button>
<button id="del" disabled>선택 삭제</button>
</div>
<div class="row" style="margin-top:6px">
<button id="clear" class="full" disabled>전부 지우기</button>
</div>
<p class="count" id="count" style="margin:10px 0 0">가린 영역 0개</p>
</div>
<div class="group">
<button id="save" class="full" disabled>PNG로 저장</button>
<div class="row" style="margin-top:6px">
<button id="reset" class="full">다른 이미지 열기</button>
</div>
</div>
<ul class="tips">
<li>빈 곳을 누른 채 끌면 그 크기의 원이 생깁니다.</li>
<li>원을 끌면 위치가 옮겨집니다.</li>
<li>원 위에서 마우스 휠을 돌리면 크기가 바뀝니다.</li>
<li>Delete 키로 선택한 원을 지웁니다.</li>
<li>마지막으로 쓴 크기가 다음 원에 그대로 이어집니다.</li>
</ul>
<p class="note">저장한 파일에는 가린 결과만 남고, 원본 화소는 지워져서 복원되지 않습니다.</p>
</aside>
</main>
<script>
(function(){
const $ = id => document.getElementById(id);
const cv = $('cv'), ctx = cv.getContext('2d');
const tmp = document.createElement('canvas'), tctx = tmp.getContext('2d');
let img = null, shapes = [], sel = -1, mode = 'solid', lastR = null;
let drag = null; // {type:'new'|'move', ...}
/* ---------- load ---------- */
function load(file){
if(!file || !file.type.startsWith('image/')) return;
const url = URL.createObjectURL(file);
const im = new Image();
im.onload = () => {
img = im; shapes = []; sel = -1;
cv.width = im.naturalWidth; cv.height = im.naturalHeight;
$('drop').style.display = 'none';
$('canvasWrap').style.display = 'block';
$('size').max = Math.round(Math.min(cv.width, cv.height) * .75);
if(lastR === null) lastR = Math.max(12, Math.round(cv.width/22));
lastR = Math.min(lastR, +$('size').max);
redraw(); sync();
URL.revokeObjectURL(url);
};
im.src = url;
}
$('pick').onclick = () => $('file').click();
$('file').onchange = e => load(e.target.files[0]);
const drop = $('drop');
['dragenter','dragover'].forEach(t => document.addEventListener(t, e => {
e.preventDefault(); if(!img) drop.classList.add('over');
}));
['dragleave','drop'].forEach(t => document.addEventListener(t, e => {
e.preventDefault(); drop.classList.remove('over');
}));
document.addEventListener('drop', e => { if(e.dataTransfer.files[0]) load(e.dataTransfer.files[0]); });
/* ---------- drawing ---------- */
function paintShapes(c, withRing){
c.drawImage(img, 0, 0);
shapes.forEach((s, i) => {
c.save();
c.beginPath(); c.arc(s.x, s.y, s.r, 0, Math.PI*2); c.closePath();
if(s.mode === 'solid'){
c.fillStyle = '#000'; c.fill();
} else if(s.mode === 'blur'){
c.clip(); c.filter = 'blur(' + Math.max(6, s.r/3) + 'px)';
c.drawImage(img, 0, 0);
} else {
c.clip();
const size = s.r*2, px = Math.max(6, Math.round(s.r/4));
const w = Math.max(1, Math.round(size/px));
tmp.width = w; tmp.height = w;
tctx.imageSmoothingEnabled = true;
tctx.clearRect(0,0,w,w);
tctx.drawImage(img, s.x-s.r, s.y-s.r, size, size, 0, 0, w, w);
c.imageSmoothingEnabled = false;
c.drawImage(tmp, 0, 0, w, w, s.x-s.r, s.y-s.r, size, size);
}
c.restore();
if(withRing && i === sel){
c.save();
c.strokeStyle = '#5b8def';
c.lineWidth = Math.max(2, cv.width/400);
c.setLineDash([c.lineWidth*3, c.lineWidth*3]);
c.beginPath(); c.arc(s.x, s.y, s.r, 0, Math.PI*2); c.stroke();
c.restore();
}
});
}
function redraw(){ if(img) paintShapes(ctx, true); }
/* ---------- pointer ---------- */
function toImg(e){
const r = cv.getBoundingClientRect();
return { x:(e.clientX - r.left) * (cv.width / r.width),
y:(e.clientY - r.top) * (cv.height / r.height) };
}
function hit(p){
for(let i = shapes.length - 1; i >= 0; i--){
const s = shapes[i];
if(Math.hypot(p.x - s.x, p.y - s.y) <= s.r) return i;
}
return -1;
}
cv.addEventListener('pointerdown', e => {
if(!img) return;
cv.setPointerCapture(e.pointerId);
const p = toImg(e), i = hit(p);
if(i >= 0){
sel = i;
drag = { type:'move', dx:p.x - shapes[i].x, dy:p.y - shapes[i].y };
} else {
shapes.push({ x:p.x, y:p.y, r:lastR, mode });
sel = shapes.length - 1;
drag = { type:'new', ox:p.x, oy:p.y, grew:false };
}
redraw(); sync();
});
cv.addEventListener('pointermove', e => {
if(!drag) return;
const p = toImg(e), s = shapes[sel];
if(drag.type === 'move'){ s.x = p.x - drag.dx; s.y = p.y - drag.dy; }
else {
const d = Math.hypot(p.x - drag.ox, p.y - drag.oy);
if(d > 12){ s.r = d; drag.grew = true; }
}
redraw();
});
const endDrag = e => {
if(drag && sel >= 0){
if(drag.type === 'new' && drag.grew) lastR = shapes[sel].r;
$('size').value = Math.round(shapes[sel].r);
}
drag = null;
};
cv.addEventListener('pointerup', endDrag);
cv.addEventListener('pointercancel', endDrag);
cv.addEventListener('wheel', e => {
if(!img) return;
const i = hit(toImg(e));
if(i < 0) return;
e.preventDefault();
sel = i;
shapes[i].r = Math.max(10, shapes[i].r * (e.deltaY < 0 ? 1.1 : 0.9));
lastR = shapes[i].r;
$('size').value = Math.round(lastR);
redraw(); sync();
}, { passive:false });
document.addEventListener('keydown', e => {
if(e.key === 'Delete' || e.key === 'Backspace'){
if(sel >= 0){ e.preventDefault(); shapes.splice(sel,1); sel = -1; redraw(); sync(); }
} else if((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'z'){
e.preventDefault(); $('undo').click();
}
});
/* ---------- controls ---------- */
document.querySelectorAll('[data-mode]').forEach(b => b.onclick = () => {
mode = b.dataset.mode;
document.querySelectorAll('[data-mode]').forEach(x =>
x.setAttribute('aria-pressed', String(x === b)));
if(sel >= 0){ shapes[sel].mode = mode; redraw(); }
});
$('size').oninput = e => {
lastR = +e.target.value;
if(sel >= 0){ shapes[sel].r = lastR; redraw(); }
};
$('undo').onclick = () => { shapes.pop(); sel = -1; redraw(); sync(); };
$('del').onclick = () => { if(sel >= 0){ shapes.splice(sel,1); sel = -1; redraw(); sync(); } };
$('clear').onclick = () => { shapes = []; sel = -1; redraw(); sync(); };
$('reset').onclick = () => {
img = null; shapes = []; sel = -1;
$('canvasWrap').style.display = 'none';
$('drop').style.display = 'block';
$('file').value = ''; sync();
};
$('save').onclick = () => {
const out = document.createElement('canvas');
out.width = cv.width; out.height = cv.height;
paintShapes(out.getContext('2d'), false);
out.toBlob(blob => {
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = 'masked-' + Date.now() + '.png';
a.click();
setTimeout(() => URL.revokeObjectURL(a.href), 1000);
}, 'image/png');
};
function sync(){
const has = !!img, n = shapes.length;
$('save').disabled = !has || n === 0;
$('undo').disabled = $('clear').disabled = n === 0;
$('del').disabled = sel < 0;
$('size').disabled = !has;
$('size').value = Math.round(sel >= 0 ? shapes[sel].r : (lastR || 90));
$('count').textContent = '가린 영역 ' + n + '개';
}
sync();
})();
</script>
</body>
</html>