>Feed aggregator / Sources /
Ajaxian
HTML5 Canvas Image Effects: Black & White
il y a 3h 19 min
Marco Lisci has written a tutorial on creating a black and white image effect using the Canvas tag.
The heart of his tutorial is using getImageData() and looping through the red, green, blue, and alpha values of each pixel to change it's luminance:
So, what can we use to make an image black and white? The luminance. The luminance is how much a color is luminous to the human eye and it’s used to measure the clipping white in video editing systems, for example. In video editing system a white color that “break” the screen is a white that is too white to be represented on a common TV.
This is important because by calculating the average number between red, green and blue values we could obtain a value for every pixel that represent a static mathematical representation of the color luminance.
But there’s a problem, an human eye see colors dynamically. For example, if we take similar shades of blue and green, our eyes will detect the green color more luminous than the blue. In our algorithm we could use the static average formula, but our image will be too flat, and we’ll lose a lot of color depth.
So, let’s introduce the luminance formula: red x 0.3 + green x 0.59 + blue x 0.11.
This formula calculates the luminance of a color as it’s perceived by the human eye, so the green channel has more importance than the red and the blue. In our Javascript code we calculate for every pixel the grayscale number, by using exactly this formula. Then we assign this value to the red, green and blue channels of our pixel. By doing this for every pixel we are able to get a black and white version of our original image. There are obviously other more complex methods to calculate the correct grayscale value, but they could be too heavy to be used in an HTML5 canvas element, and we can say that for an everyday use, the luminance algorithm is good.
Resulting in code that looks as follows:
PLAIN TEXT JAVASCRIPT: var imgd = context.getImageData(0, 0, 500, 300);var pix = imgd.data;
for (var i = 0, n = pix.length; i <n; i += 4) {
var grayscale = pix[i] * .3 + pix[i+1] * .59 + pix[i+2] * .11;
pix[i] = grayscale; // red
pix[i+1] = grayscale; // green
pix[i+2] = grayscale; // blue
// alpha
}
context.putImageData(imgd, 0, 0);
It would be cool to see this combined as a kind of filter that can be applied to do black and white roll-over effects of elements, similar to what you can do with SVG filters.
Catégories: Développement web
Create a Sticky Note Effect in 5 Easy Steps with CSS3 and HTML5
jeu, 09.09.2010 - 12:30
Our very own Christian Heilmann has posted a tutorial on creating a fancy sticky note effect using CSS3 and HTML5:
He breaks it down in five easy steps to produce the final following demo:
Catégories: Développement web
Offline: What does it mean and why should I care?
mer, 08.09.2010 - 12:30
Michael Mahemoff has posted an extremely in-depth tutorial on HTML5Rocks on the subject of offline web apps:
- Introduction: The Meaning of "Offline"
- Application Cache and Offline Storage
- Older Offline Storage Techniques
- Offline Storage in the Era of HTML5
- How to Use the Offline Technologies
- Summary
- Further Reading
(BTW, don't hate me 'cuse of the LOLCat pic -- you know you secretly like it)
Catégories: Développement web
WebPagetest and PageSpeed join up via PageSpeed SDK
mar, 07.09.2010 - 19:47
Steve Souders just pointed me to the great news that two great open source performance projects are working well together:
Pat Meenan just blogged about Page Speed results now available in Webpagetest. This is a great step toward greater consistency in the world of web performance, something that benefits developers and ultimately benefits web users.
The Page Speed SDK gives a path for folks to unify behind standard performance metrics and results. Great work!
Catégories: Développement web