-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcomicifyOnClick.html
More file actions
222 lines (211 loc) · 8.09 KB
/
comicifyOnClick.html
File metadata and controls
222 lines (211 loc) · 8.09 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<html>
<body>
<!--Note: Since this code uses canvas, using images from other domains can throw an error about canvas getting tainted. There are some ways to get around it though-->
<img src="https://rawgit.com/kapyaar/Comicify/master/catFromInternet.jpg" />
<div id="resultHolder"></div>
<p>It is possible the example may not work right away, due to image cross domain canvas contamination.</p>
<button onclick="start()">Comicify me</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
function start() {
var myImg=$("img");
var myDistance=50;// This can be updated from an input field if needed.
var f = function(myImg) {
//do something with returned image
};
var c = new comicify(myImg, myDistance, 0);
};
function comicify(img, dist, outf) {
var t = this;
t.srcimg = img;
t.distance = dist;
t.outfun = outf;
t.container = $("#resultHolder");
t.process();
}
comicify.prototype={
draw:function(container){
var t = this;
t.container = container;
var input = $('<input>').val(t.distance).change(function(){
t.distance=this.value;
if(''!= t.srcimg[0].src){
t.process();
}
});
container.append($('<div>').text("Directions:").append(
$('<ul>')
.append($('<li>').text("Set the distance to distance (manitude between color vecotrs) that defines a single (new) color"))
.append($('<li>').text("Paste an image to the page"))
.append($('<li>').text("Wait -- you're processing an image in Javascript..."))
));
container.append($('<div>').append($('<span>').text("Distance:").width("400px")).append(input))
t.srcimg = $('<img>');
t.srcimg.load(function(){t.process()});
container.on('paste',function(evt){
return(t.handlePaste(evt));
});
},
handlePaste : function (evt) {
var t = this;
var items = evt.originalEvent.clipboardData.items, paste;
for (var i in items){
if('file' ==items[i].kind){
var fr = new FileReader();
console.log("Going");
fr.onload = function(revt){
t.srcimg[0].src = revt.target.result;
}
fr.readAsDataURL(items[i].getAsFile());
}
}
},
process:function(){
var t = this;
var canvas = $('<canvas>');
var w = canvas[0].width = t.srcimg[0].width;
var h = canvas[0].height= t.srcimg[0].height;
console.log(h);
var prog = $('<span>').text("Processing...");
t.container.append(prog);
t.ctx = canvas[0].getContext('2d');
t.ctx.drawImage(t.srcimg[0],0,0);
t.image = t.ctx.getImageData(0,0,w,h); //array [4*w*h] of colors RGBA
t.ctx.clearRect ( 0 , 0 ,w,h);
t.mask = Array(w);
var zz = Array();
for (var i =0;i<w*h;i++){
zz[i]=i;
}
for (var i =0;i<w;i++){
t.mask[i] = Array(h);
for(var j=0;j<h;j++){
t.mask[i][j]=true;
var n = Math.floor(w*h*Math.random());
var z = zz[n];
zz[n] = zz[i*j];
zz[i*j] =z;
}
}
var n =0;
var err = "";
var step = (w*h)/100;
var progress = 0;
var fn = function(){
if (n<(w*h) && err == ""){
var q = Math.floor(step);
while (q > 0 && n<(w*h) && err == ""){
var x = zz[n];
var j = Math.floor(x / w);
var i = x % w;
if(t.mask[i][j]) {
var y = x*4;
var avgct=0;
t.avg=[t.image.data[y++],t.image.data[y++],t.image.data[y++],t.image.data[y++]];
try {
t.doPoint(i,j); // do all the points next to it too...
t.doPaint();
} catch(e){
if (! e.message == "Maximum call stack size exceeded") {
err = e.message;
prog.text(e.message);
} else{
t.doPaint();
} // ignore call stack (we'll try
}
}
n++;
q--;
}
progress ++;
if (err == ""){
prog.text("Processing " + String(progress) + "%");
window.setTimeout(fn,0.0001);
}
} else {
if (err == ""){
prog.remove();
}
t.ctx.putImageData(t.image,0,0);
var outImg = $('<img>');
outImg[0].src =canvas[0].toDataURL();
t.container.append(outImg);
}
};
window.setTimeout(fn,4);
},
doPaint:function(){
var t= this;
var w = t.srcimg[0].width;
var h = t.srcimg[0].height;
var avgct = 1;
for (var i =0;i<w;i++){
for(var j=0;j<h;j++){
var x = 4*(i+(j*w));
if(null==t.mask[i][j]){
for (var k=0;k<4;k++) {
t.avg[k] = ((t.avg[k]*avgct) +t.image.data[x+k]) /(avgct +1);
}
avgct++;
}
}
}
for (var i =0;i<w;i++){
for(var j=0;j<h;j++){
if(null==t.mask[i][j]){
var n = 4*(i+(j*w));
for (var k=0;k<4;k++) {
t.image.data[n+k] = Math.floor(t.avg[k]);
}
t.mask[i][j] = 0;
}
}
}
},
doPoint:function(i,j){
var t = this;
var w = t.srcimg[0].width;
var h = t.srcimg[0].height;
var x = 4*(i+(j*w));
var p = [t.image.data[x++],t.image.data[x++],t.image.data[x++],t.image.data[x++]]
if (t.distance > distance(t.avg,p)){
t.mask[i][j] = null;
if( i+1 < w && t.mask[i+1][j] ) { t.doPoint(i+1,j)}
if( i-1 > 0 && t.mask[i-1][j] ) { t.doPoint(i-1,j)}
if( j+1 < h && t.mask[i][j+1] ) { t.doPoint(i,j+1)}
if( j-1 > 0 && t.mask[i][j-1] ) { t.doPoint(i,j-1)}
}
return;
},
avgPt:function(i,j,ct){
var t = this;
var w = t.srcimg[0].width;
var h = t.srcimg[0].height;
if(i>0 && j>0 && i<w && j<h){
var x = 4*(i+(j*w));
var p = [t.image.data[x++],t.image.data[x++],t.image.data[x++],t.image.data[x++]]
for (var k=0;k<4;k++) {
t.avg[k] = ((t.avg[k]*ct) +p[k]) /(ct +1);
}
}
},
avgct:0,
ctx:0,
mask:0,
image:0,
avg:0,
srcimg:0,
cnv:0,
distance : 35,
container : 0 // container
} // proto
function distance(a,b){
var o =0;
for(i=0;i<4;i++){
o += (a[i]-b[i])*(a[i]-b[i]);
}
return(Math.sqrt(o));
}
</script>
</body>
</html>