-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.html
81 lines (65 loc) · 1.64 KB
/
image.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="nonogram.css">
</head>
<body>
<canvas id="canvas">
</canvas>
<script>
function imageWorks(imgAddr) {
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
let l = 7;
img = new Image();
img.crossOrigin = "Anonymous";
img.src = imgAddr;
function getPixel(x, y) {
// returns RGB and Alpha
return ctx.getImageData(x, y, 1, 1).data;
}
function getImageAverage() {
let count = 0;
let total = 0;
for (let i = 0; i < l; i++) {
for (let j = 0; j < l; j++) {
let p = getPixel(i, j);
total += p[0] + p[1] + p[2] + p[3];
count++;
}
}
let avg = total / count;
return avg;
}
function processImage() {
let avg = getImageAverage();
for (let i = 0; i < l; i++) {
for (let j = 0; j < l; j++) {
let p = getPixel(i, j);
let total = p[0] + p[1] + p[2] + p[3];
if (total <= avg) {
console.log('on');
}
else {
console.log('off');
}
}
}
}
// will trigger automatically when the imageworks is called and the image has loaded
img.onload = function () {
var oc = document.createElement('canvas'), octx = oc.getContext('2d');
oc.width = l; // needed for the canvas
oc.height = l; // needed for the canvas
octx.drawImage(img, 0, 0, l, l);
ctx.drawImage(oc, 0, 0, l, l);
processImage();
}
}
</script>
</body>
</html>