HTML and CSS Reference
In-Depth Information
var
v
=
0.2126
*
r
+
0.7152
*
g
+
0.0722
*
b
;
d
[
i
] =
d
[
i
+
1
] =
d
[
i
+
2
] =
v
}
return
pixels
;
};
Note
The grayscale function in
listing 8.18
is adapted from the HTML Rocks article on image
filters; see
www.html5rocks.com/en/tutorials/canvas/imagefilters/
for more details.
With the complex math all safely hidden in a general-purpose function, all that remains is
from within your
draw()
function. For this to work, you need to declare a variable
of
false
.
Listing 8.19. index.html—Use the
grayscale()
function within
draw()
Note
The
getImageData()
method will trigger a security error if you access the example
from a file:// URL. If you run into any problems, try accessing the file using a local
web server. In Chrome there's also a bug that causes a security violation when
getImageData()
is called after an SVG image has been drawn on the canvas. Check