Why do it in the browser? : current applications. 3. How do we accomplish this? The canvas element and its rendering context. Geometric operations on an image : scale, crop and rotation. 4. Pixel manipulation The ImageData object 5. Filters 6. Pixel displacement (just a taste of it) 7. Optimization and performance 8. Libraries
might not be installed automatically • They might not be available for the user device Server technologies • Too much load on the server • Client-server communication brakes the interaction flow
canvas element provides scripts with a resolution-dependent bitmap canvas, which can be used for rendering graphs, game graphics, art, or other visual images on the fly.” http://www.w3.org/TR/html5/the-canvas-element.html
y, w, h); var dataWidth = imagedata.width; var dataHeight = imagedata.height; var data = imagedata.data; //a one-dimensional array containing the data in RGBA order, as integers in the range 0 to 255.
var glum = 0.7152; var blum = 0.0722; // rlum + glum + blum = 1 function greyscale(color){ var brightness= (rlum*color.r) + (glum*color.g) + (blum*color.b); return { r: brightness, g: brightness, b: brightness, a: color.a }; } Find the brightness of the color and apply it equally to the three color channels.
number of cells (it must have a central cell) • It is usually square • The greater the absolute weights around the center, the greater the effect. • The greater the central absolute weight, the lesser the effect. • In JavaScript, represent it as an Array, the weights ordered from left to right and top to down.
sum the products of the color value of the current pixel, or a neighbor of it, with the corresponding value of the filter matrix, and divide that sum by the sum of the weights of the matrix (if not 0).
= buffer.data; var image = context.getImageData(0,0,width,height); Var imageData = image.data; //loop through every pixel in imageData //loop through the matrix and compute the new pixel value //set the new value in bufferData context.putImageData(buffer, x,y); Always use an empty ImageData as “buffer”
of the image, define an enclosing rectangular area (x, y, w, h) and process just that area. var bufferImage = context.createImageData(w,h); var sourceImage = context.getImageData(x, y, w, h) Apply the end result with: context.putImageData(bufferImage,0,0,x,y,w,h)
point and convolution filters. • Supports applying the filter to just a rectangular area. Usage 1: Static call passing an image or canvas element, a filter name and options. A canvas will be returned.
point and convolution filters. • Supports applying the filter to just a rectangular area. Usage 1: Static call passing an image or canvas element, a filter name and options. A canvas will be returned. Usage 2: Declare image or canvas elements with the class “pixastic” and the filter name and options. These elements will be replaced by a canvas.
filters. • Also replaces background images set with css on non image elements. • Not object oriented, nor namespaced. Usage: Declare the elements to be processed with a class naming the filter. Provide the options in “data-” prefixed attribute.
filters. • Also replaces background images set with css on non image elements. • Not object oriented, nor namespaced. Usage: Declare the elements to be processed with a class naming the filter. Provide the options in “data-” prefixed attribute. The element will be hidden, copied, the copy inserted in the same place and its source attribute will be filled with the bytestream output by the toDataUrl canvas method: <img src="data:image/png;base64,iVBORw0...
(applied as a color matrix) and good support for convolution filters, including motion blur. • Supports blend modes (mixing two images) and even pixel displacement based on the Adobe ActionScript version. • The fastest of these libraries.
(applied as a color matrix) and good support for convolution filters, including motion blur. • Supports blend modes (mixing two images) and even pixel displacement based on the Adobe ActionScript version. • The fastest of these libraries. Usage: Object oriented. Object instantiation using a namespace. Supports concatenation of methods by returning the object itself.
“Paint” pixels • Change pixels based on their value • Change pixels based on their neighbors • Do it efficiently • Delegate the hard work to libraries To do next: have fun! Looking back