forked from timdream/wordcloud2.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shape-generator.html
133 lines (109 loc) · 3.95 KB
/
shape-generator.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Get approximate polar equation for given image</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<!-- Le styles -->
<link href="//netdna.bootstrapcdn.com/bootstrap/2.2.2/css/bootstrap.min.css" rel="stylesheet">
<link href="//netdna.bootstrapcdn.com/bootstrap/2.2.2/css/bootstrap-responsive.min.css" rel="stylesheet">
<link href="//fonts.googleapis.com/css?family=Finger+Paint" id="link-webfont" rel="stylesheet">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.2.2/bootstrap.min.js"></script>
<script>
var img, url;
window.onload = function() {
// DOM File object selected
var file = document.getElementById('file').files[0];
if (file) run();
}
function run() {
// DOM File object selected
var file = document.getElementById('file').files[0];
url = window.URL.createObjectURL(file);
img = new Image();
img.src = url;
img.onload = readPixels;
}
function readPixels() {
window.URL.revokeObjectURL(url);
var canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
document.body.appendChild(canvas);
var ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, img.width, img.height);
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
var mask = new Uint8Array(imageData.data.length / 4);
var width = canvas.width;
var height = canvas.height;
var o = [(img.width / 2) | 0, (img.height / 2) | 0];
var d = window.d = [];
// paint red on the center pixel (not really visible)
imageData.data[(o[1] * width + o[0]) * 4] = 254;
for (var theta = 0; theta < 2 * Math.PI; theta += 0.01) {
var dx = 1 * Math.cos(theta);
var dy = - 1 * Math.sin(theta);
var i = 0;
var x = o[0];
var y = o[1];
var intX = x;
var intY = y;
while (true) {
x += dx;
y += dy;
intX = x | 0;
intY = y | 0;
if (intY < 0 || intY > canvas.height || intX < 0 || intX > width) {
break;
}
var ptr = (intY * width + intX) * 4;
var tone = imageData.data[ptr] +
imageData.data[ptr + 1] +
imageData.data[ptr + 2];
var alpha = imageData.data[ptr + 3];
// draw a green line all the way until the boundary
imageData.data[ptr] = 0; // R
imageData.data[ptr + 1] = 254; // G
imageData.data[ptr + 2] = 0; // B
imageData.data[ptr + 3] = 254;
// are we there at the boundary?
if (alpha < 128 || tone > 128 * 3) {
d.push(i);
break;
} else {
i++;
}
}
}
ctx.putImageData(imageData, 0, 0);
var max = d.reduce(function(prev, curr) {
return Math.max(prev, curr);
});
var resultText =
'function(theta) {\n' +
' var max = ' + max + ';\n' +
' var leng = ' + JSON.stringify(d) + ';\n\n' +
' return leng[(theta / (2 * Math.PI)) * leng.length | 0] / max;\n' +
'}\n';
var result = document.getElementById('result');
result.textContent = resultText;
}
</script>
</head>
<body>
<div class="container">
<p class="lead">Get approximate polar equation for given image.</p>
<p>
Please select a black-white image below.
The black part shall be centered and it will be the shape to trace.
You would have to manually save, edit the code if you want any additional processing.
</p>
<form>
<input type="file" id="file" class="input-large" onchange="run()">
</form>
<pre id="result"></pre>
</div>
</body>