-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample4.js
27 lines (22 loc) · 1008 Bytes
/
example4.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
var c2d, exp, imgs;
function init(){
var c=document.querySelector('canvas'), c2d=c.getContext('2d'); // our canvas node and its 2d context.
imgs=document.querySelectorAll('.cycle img');
c2d.drawImage(document.querySelector('.mask img'),0,0); // draw the photo starting top left.
function changeExposure(desiredExp){
if(desiredExp>exp){
for(var i=exp;i<desiredExp;i++) imgs[i+1].className='show';
}else if(desiredExp<exp){
for(var i=exp;i>desiredExp;i--) imgs[i].className='';
}
exp=desiredExp;
}
function getPixelColor(e){
var colors_images={36:0,80:1,132:2,191:3,255:4} // I wanted to have stronger colors than #010101, #020202 etc
var pixel=c2d.getImageData(e.layerX,e.layerY,1,1).data; // get the pixel for this position
var luminance=pixel[0]; // RGBA but we only need one sample since it's grayscale
var desiredExp=colors_images[luminance]; // look it up
changeExposure(desiredExp);
}
c.addEventListener('mousemove', getPixelColor);
}