-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheyedropper.js
62 lines (49 loc) · 1.87 KB
/
eyedropper.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
$(function () {
var $canvas = $("canvas.eyedropper"),
ctx = $canvas[0].getContext('2d'),
loadCanvas = function ($canvas, ctx, image) {
$canvas.attr({
width: image.width,
height: image.height
}).css({
width: image.width + 'px',
height: image.height + 'px'
});
ctx.drawImage(image, 0, 0);
},
displayPixelInfo = function (x, y) {
var data = ctx.getImageData(x, y, 1, 1).data;
$(".level.red").text(data[0]);
$(".level.green").text(data[1]);
$(".level.blue").text(data[2]);
$(".level.pixel").text(data[0] + " " + data[1] + " " + data[2]);
$(".swatch.red").css({
'background-color': "rgb(" + data[0] + ", 0, 0)"
});
$(".swatch.green").css({
'background-color': "rgb(0, " + data[1] + ", 0)"
});
$(".swatch.blue").css({
'background-color': "rgb(0, 0, " + data[2] + ")"
});
$(".swatch.pixel").css({
'background-color': "rgb(" + data[0] + ", " + data[1] +", " + data[2] + ")"
});
},
img = new Image();
img.addEventListener('load', function () {
loadCanvas($canvas, ctx, img);
}, false);
$canvas.mousemove(function (event) {
var offset = $(this).offset();
displayPixelInfo(event.pageX - Math.round(offset.left), event.pageY - offset.top);
});
$canvas[0].addEventListener('touchmove', function (event) {
var offset = $canvas.offset(),
touch = event.changedTouches[0];
event.preventDefault();
event.stopPropagation();
displayPixelInfo(touch.pageX - Math.round(offset.left), touch.pageY - offset.top);
});
img.src = 'images/dress.jpg';
});