-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmozaic.html
141 lines (120 loc) · 4.2 KB
/
mozaic.html
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
<!doctype HTML>
<html>
<head>
<!-- favicon.ico -->
<link rel="apple-touch-icon-precomposed" href="">
<style>
html, body{
margin:0;
padding:0;
}
#divCanvas {
position:relative;
padding: 5rem;
display:inline-block;
width:70%;
height: 400px;
}
#divCanvas canvas{
position: absolute;
left:0px;
width:400;
height: 400;
}
#divCanvas #canvas{
z-index:1;
}
#divCanvas #mosaic{
z-index:2;
}
#divCanvas #hover{
z-index:3;
}
</style>
<title>모자이크</title>
</head>
<body>
<section>
<div id="divCanvas">
<canvas id="canvas"></canvas> <!--원본-->
<canvas id="mosaic"></canvas> <!--모자이크 영역-->
<canvas id="hover"></canvas> <!--마우스영역-->
</div>
<button type="button" id="btnMosaic">모자이크</button>
<button type="button" id="btnEraser">지우개</button>
<button type="button" id="btnSave">저장하기</button>
</section>
<script>
let isMozaic = false;
/* canvas setting*/
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let img = new Image()
img.src = "./1.jpg"
img.onload = function(){
ctx.clearRect(0,0, canvas.width, canvas.height);
ctx.drawImage(img, 0 , 0, canvas.width, canvas.height);
mosaicCtx = document.getElementById('mosaic').getContext('2d');
mosaicCtx.clearRect(0,0, canvas.width, canvas.height);
mosaicCtx.drawImage(img, 0 , 0, canvas.width, canvas.height);
};
/* event */
document.getElementById('hover').addEventListener('mousemove', function (e) {
var x = event.clientX - ctx.canvas.offsetLeft -15;
var y = event.clientY - ctx.canvas.offsetTop -15;
x = x < 0 ? 0 : x;
y = y < 0 ? 0 : y;
ctxHover = this.getContext('2d');
ctxHover.clearRect(0,0, this.width, this.height)
ctxHover.strokeStyle = 'black';
ctxHover.strokeWidth= 3;
ctxHover.fillStyle = 'rgba(255, 255, 255, 0.5)';
ctxHover.fillRect(x, y, 30, 30);
});
document.getElementById('hover').addEventListener('mouseout', function (e) {
ctxHover = this.getContext('2d');
ctxHover.clearRect(0,0, this.width, this.height);
});
document.getElementById('hover').addEventListener('click', function (e) {
var x = event.clientX - ctx.canvas.offsetLeft - 15;
var y = event.clientY - ctx.canvas.offsetTop - 15;
x = x < 0 ? 0 : x;
y = y < 0 ? 0 : y;
mosaic = document.getElementById('mosaic');
mosaicCtx = mosaic.getContext('2d');
var imageData = ctx.getImageData(x, y, 30, 30);
if(isMozaic){
for(var i=0;i<imageData.data.length;i=i+4){
r = imageData.data[i];
g = imageData.data[i+1];
b = imageData.data[i+2];
a = imageData.data[i+3];
for(var j=1;j<=9;j++){
imageData.data[i+4] = r;
imageData.data[i+5] = g;
imageData.data[i+6] = b;
imageData.data[i+7] = a;
i=i+4;
}
}
}
mosaicCtx.putImageData(imageData, x , y);
})
/* btn */
document.getElementById("btnMosaic").onclick = function(){
isMozaic = true;
};
document.getElementById("btnEraser").onclick = function(){
isMozaic = false;
};
document.getElementById("btnSave").onclick = function(e){
mosaic = document.getElementById('mosaic');
image = mosaic.toDataURL("image/png", 1.0).replace("image/png", "image/octet-stream");
var link = document.createElement('a');
link.download = "mozaic.png";
link.href = image;
link.click();
};
</script>
</body>
</html>