forked from marijnh/Eloquent-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path19_2_color_picker.html
More file actions
32 lines (28 loc) · 782 Bytes
/
19_2_color_picker.html
File metadata and controls
32 lines (28 loc) · 782 Bytes
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
<!doctype html>
<base href="http://eloquentjavascript.net/">
<script src="code/chapter/19_paint.js"></script>
<script>
function colorAt(cx, x, y) {
var pixel = cx.getImageData(x, y, 1, 1).data;
return "rgb(" + pixel[0] + ", " + pixel[1] + ", " + pixel[2] + ")";
}
tools["Pick color"] = function(event, cx) {
var pos = relativePos(event, cx.canvas);
try {
var color = colorAt(cx, pos.x, pos.y);
} catch(e) {
if (e instanceof SecurityError) {
alert("Unable to access your picture's pixel data");
return;
} else {
throw e;
}
}
cx.fillStyle = color;
cx.strokeStyle = color;
};
</script>
<link rel="stylesheet" href="css/paint.css">
<body>
<script>createPaint(document.body);</script>
</body>