-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo3.js
92 lines (78 loc) · 2.5 KB
/
demo3.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
var canvasWidth = 320;
var canvasHeight = 510;
var canvasClipRaduis = 20;
var c = document.getElementById("blur-canvas");
var ctx = c.getContext("2d");
c.width = canvasWidth;
c.height = canvasHeight;
var clipRegion = {
x : Math.floor(Math.random() * (canvasWidth - canvasClipRaduis * 2)) + canvasClipRaduis + 1,
y : Math.floor(Math.random() * (canvasHeight - canvasClipRaduis * 2)) + canvasClipRaduis + 1,
r : canvasClipRaduis
};
var img = new Image();
img.src = 'image2.jpg'
img.onload = function () {
draw(img, clipRegion);
}
function draw(img, clipRegion) {
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
ctx.drawImage(img, 0, 0, canvasWidth, canvasHeight);
}
$("#blur-canvas").mousedown(function (e) {
$("#blur-canvas").mousemove(function (e) {
clipRegion.x = e.clientX - c.getBoundingClientRect().left;
clipRegion.y = e.clientY - c.getBoundingClientRect().top;
mosaic(clipRegion.x-clipRegion.r,clipRegion.y-clipRegion.r,clipRegion.r*2,clipRegion.r*2)
})
})
$("#blur-canvas").mouseup(function (e) {
$("#blur-canvas").unbind("mousemove");
})
function mosaic(mx,my,mw,mh) {
var tmpImageData=ctx.getImageData(mx, my, mw, mh);
var tmpPixelData=tmpImageData.data;
var imageData=ctx.getImageData(mx,my, mw, mh);
var pixelData=imageData.data;
var size=10;
var totalnum=size*size;
for(var i=0;i<mh;i+=size)
{
for(var j=0;j<mw;j+=size)
{
var totalr=0,totalg=0;totalb=0;
for(var dx=0;dx<size;dx++)
{
for(var dy=0;dy<size;dy++)
{
var x=i+dx;
var y=j+dy;
var p=x*mw+y;
totalr+=tmpPixelData[p*4+0];
totalg+=tmpPixelData[p*4+1];
totalb+=tmpPixelData[p*4+2];
}
}
var p=i*mw+j;
var resr=totalr/totalnum;
var resg=totalg/totalnum;
var resb=totalb/totalnum;
for( var dx=0;dx<size;dx++)
{
for(var dy=0;dy<size;dy++)
{
var x=i+dx;
var y=j+dy;
var p=x*mw+y
pixelData[p*4+0]=resr
pixelData[p*4+1]=resg
pixelData[p*4+2]=resb
}
}
}
}
ctx.putImageData(imageData,mx,my);
}
function reset(){
draw(img, clipRegion);
}