-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2DImageDataExample01.htm
executable file
·55 lines (44 loc) · 1.99 KB
/
2DImageDataExample01.htm
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
<!DOCTYPE html>
<html>
<head>
<title>Canvas Fill Rect Pattern Example</title>
</head>
<body>
<p>This example won't run locally due to security restrictions. You load this example from a web server.</p>
<canvas id="drawing" width="200" height="200">Your browser doesn't support the canvas tag.</canvas>
<img id="smiley" src="smile2.gif" border="1" title="Image tag" />
<script>
window.onload = function(){
var drawing = document.getElementById("drawing");
//make sure <canvas> is completely supported
if (drawing.getContext){
var context = drawing.getContext("2d"),
image = document.images[0],
imageData, data,
i, len, average,
red, green, blue, alpha;
//draw regular size
context.drawImage(image, 0, 0);
//get the image data
imageData = context.getImageData(0, 0, image.width, image.height);
data = imageData.data;
for (i=0, len=data.length; i < len; i+=4){
red = data[i];
green = data[i+1];
blue = data[i+2];
alpha = data[i+3];
//get the average of rgb
average = Math.floor((red + green + blue) / 3);
//set the colors, leave alpha alone
data[i] = average;
data[i+1] = average;
data[i+2] = average;
}
//assign back to image data and display
imageData.data = data;
context.putImageData(imageData, 0, 0);
}
};
</script>
</body>
</html>