-
Notifications
You must be signed in to change notification settings - Fork 0
/
input-low-poly.html
134 lines (107 loc) · 4.29 KB
/
input-low-poly.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
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="utf-8">
<meta name="mobileoptimized" content="0"/>
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<meta name="screen-orientation" content="portrait">
<meta name="browsermode" content="application">
<title></title>
<style>
canvas {
width: 600px;
height: 600px;
cursor: pointer;
background: #ededed;
}
.hide {
display: none;
}
</style>
</head>
<body>
<canvas width="600" height="600"></canvas>
<input type="file" class="hide">
<script src="delaunay.js"></script>
<script src="filter-desaturate.js"></script>
<script src="filter-sobel.js"></script>
<script>
(function () {
var file = document.querySelector( "input" ),
canvas = document.querySelector( "canvas" ),
gc = canvas.getContext( "2d" );
canvas.onclick = function () {
file.click();
};
function getPoint( imgdata, w, h ) {
var result = [];
var pre = 0;
for ( var y = 0; y < h; y += 1 ) {
for ( var x = 0; x < w; ) {
var index = (y * w + x) * 4;
var cur = imgdata[index] + imgdata[index + 1] + imgdata[index + 2];
if ( cur > 150 && Math.abs( cur - pre ) > 50 ) {
result.push( [x, y] );
pre = cur;
x += 10;
}
else {
x += 1;
}
}
}
return result;
}
var width = 600, height = 600;
function lowPoly( src, callback ) {
var img = new Image();
img.src = src;
gc.clearRect( 0, 0, 600, 600 );
img.onload = function () {
var startTime = (new Date()).getTime();
var nw = img.naturalWidth, nh = img.naturalHeight;
if ( nw > nh ) {
gc.drawImage( img, 0, 0, nh, nh, 0, 0, width, height );
}
else {
gc.drawImage( img, 0, 0, nw, nw, 0, 0, width, height );
}
var ogcdata = gc.getImageData( 0, 0, width, height );
gc.putImageData( Sobel( ogcdata ), 0, 0 );
var point = getPoint( gc.getImageData( 0, 0, width, height ).data, width, height );
var getPointTime = (new Date()).getTime();
gc.clearRect( 0, 0, width, height );
gc.lineWidth = 1;
var line = delaunay.triangulate( point );
for ( var i = 0; i < line.length; i += 3 ) {
gc.beginPath();
var x = point[line[i]][0], y = point[line[i]][1];
gc.moveTo( x, y );
gc.lineTo( point[line[i + 1]][0], point[line[i + 1]][1] );
gc.lineTo( point[line[i + 2]][0], point[line[i + 2]][1] );
gc.closePath();
var my = ((y + point[line[i + 1]][1] + point[line[i + 2]][1]) / 3) << 0;
var mx = ((x + point[line[i + 1]][0] + point[line[i + 2]][0]) / 3) << 0;
var oindex = (my * width + mx) * 4;
gc.strokeStyle = "rgba(" + ogcdata.data[oindex] + "," + ogcdata.data[oindex + 1] + "," + ogcdata.data[oindex + 2] + ",1)";
gc.fillStyle = "rgba(" + ogcdata.data[oindex] + "," + ogcdata.data[oindex + 1] + "," + ogcdata.data[oindex + 2] + ",1)";
gc.stroke();
gc.fill();
}
var endTime = (new Date()).getTime();
callback && callback( {
sum : (endTime - startTime) / 1000,
drawLine : (endTime - getPointTime) / 1000,
getPoint : (getPointTime - startTime) / 1000
} );
};
}
file.onchange = function () {
lowPoly( window.URL.createObjectURL( file.files[0] ) );
}
})();
</script>
</body>
</html>