-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
49 lines (40 loc) · 1.01 KB
/
test.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
<html>
<head>
<title>test for rgba iteration</title>
</head>
<body>
<canvas id = 'canvas'> </canvas>
<button id = 'btn'>get RBG pixel</button>
</body>
</html>
<script language="JavaScript">
//prepare canvas
var can = document.getElementById('canvas');
var ctx = can.getContext('2d');
//load img
var img = new Image();
//img.src = 'test.png';
//img.src = 'testKeyboard.jpg';
img.src = 'test.jpg';
//img.crossOrigin = "anonymous";
img.onload = function () {
can.width = img.width;
can.height = img.height;
ctx.drawImage(img, 0, 0, img.width, img.height);
}
//
var btn = document.getElementById('btn');
btn.onclick = function () {
var imgData = ctx.getImageData(0,0,can.width,can.height);
var data = imgData.data;
var str="";
for(var i=0; i<data.length; i+=4){
var red = data[i];
var green = data[i+1];
var blue = data[i+2];
var alpha = data[i+3];
str+=red+","+green+","+blue+","+alpha+"\n";
}
alert(str);
}
</script>