Création de sites web et développement d'applications

 

Ajaxian


Syndiquer le contenu
Cleaning up the web with Ajax
Mis à jour : il y a 1h 5 min

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.

[via Mark Mildner]

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

Contact

Pour toute question ou remarque, n'hésitez pas à me contacter.

A propos...

Yves Bresson, ingénieur en informatique, consultant freelance, spécialisé dans la création de sites web (PHP, web 2.0, CMS, Ajax, MySQL), d'applications Java (Swing, Spring, etc) et aussi mobiles (iPhone, Android).
Voir le profil de Yves Bresson sur LinkedIn