-
Notifications
You must be signed in to change notification settings - Fork 0
/
decollager.js
96 lines (83 loc) · 2.66 KB
/
decollager.js
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
const el = {
smallStep: document.getElementById('smallStep'),
canv: document.getElementById('canv'),
inp: {
w: document.getElementById('inpW'),
h: document.getElementById('inpH'),
},
output: document.getElementById('output'),
clipCount: document.getElementById('clip-counter'),
imgPath: document.getElementById('img-path'),
srcImg: document.getElementById('src-img'),
template: document.querySelector('body > div:last-of-type'),
};
let ctx = el.canv.getContext('2d');
let clip = {
w: parseInt(el.inp.w.value),
h: parseInt(el.inp.h.value),
xOfs: 0,
yOfs: 0,
old: {w:-1, h:-1},
}
function deleteClips()
{
while(el.output.firstChild)
el.output.removeChild(el.output.firstChild);
el.clipCount.innerHTML = '';
}
function deleteSelectedClips()
{
el.output.querySelectorAll('input[type="checkbox"]:checked').forEach(cl => el.output.removeChild(cl.parentElement.parentElement))
el.clipCount.innerHTML = "Clip Count: " + el.output.childElementCount;
}
function updateCanvas(xx, yy)
{
clip.xOfs = xx;
clip.yOfs = yy;
if (clip.h!=clip.old.h || clip.w!=clip.old.w) {
el.canv.width = clip.w;
el.canv.height = clip.h;
clip.old.h = clip.h;
clip.old.w = clip.w;
el.output.style.width = ''+clip.w+'px';
}
ctx.clearRect(0, 0, el.canv.width, el.canv.height);
ctx.drawImage(el.srcImg, xx, yy, clip.w, clip.h, 0, 0, clip.w, clip.h);
}
el.srcImg.addEventListener('mousemove', e => updateCanvas(e.offsetX, e.offsetY));
el.srcImg.addEventListener("wheel", e => {
if (!e.shiftKey)
return;
let step = el.smallStep.checked? 1:10;
let delta = e.wheelDelta<0? -step : step;
if (e.altKey) {
clip.h += delta;
if (clip.h<5)
clip.h = 5;
el.inp.h.setAttribute('value', clip.h);
}
else {
clip.w += delta;
if (clip.w<5)
clip.w = 5;
el.inp.w.setAttribute('value', clip.w);
}
updateCanvas(clip.xOfs, clip.yOfs);
});
el.srcImg.addEventListener('click', e => {
let newClip = el.template.cloneNode(true)
let newCanv = newClip.querySelector('canvas');
newCanv.width = clip.w;
newCanv.height = clip.h;
newCanv.getContext('2d').drawImage(el.srcImg, e.offsetX, e.offsetY, clip.w, clip.h, 0, 0, clip.w, clip.h);
el.output.prepend(newClip);
el.clipCount.innerHTML = "Clip Count: " + el.output.childElementCount;
});
document.body.addEventListener("keydown", e => {
if (e.keyCode == 113)
el.smallStep.checked = !el.smallStep.checked;
});
function loadImage()
{
el.srcImg.setAttribute('src', el.imgPath.value );
}