Slide 20
Slide 20 text
2D Canvas Color Picking for fast Interaction
3 counties.forEach(function(county, i) {
4 var r = i % 256,
5 g = ((i / 256) >>> 0) % 256,
6 b = ((i / (256 * 256)) >>> 0) % 256;
7
8 county.setAttribute('fill', 'rgb(' + r + ',' + g + ',' + b + ')');
9 });
1 //decode index from image
2 function getCounty(canvas, counties, x, y) {
3 var imageData = canvas.getImageData(),
4 width = imageData.width,
5 data = imageData.data,
6 index = (x + y * width) * 4, //RGBA components
7 r = data[index],
8 g = data[index + 1],
9 b = data[index + 2],
10 i = r + (g + b * 256) * 256;
11
12 return counties[i];
13 }
Offline: Encode index to county data array in colors
Online: Decode RGB color to array index
Thursday, February 28, 13