<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>HTML5 画廊和颜色选择器</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
background-color: #bababa;
color: #fff;
font: 14px/1.3 Arial, sans-serif;
}
.container {
color: #000;
margin: 20px auto;
position: relative;
width: 730px;
}
.column1 {
float: left;
width: 500px;
}
.column2 {
float: left;
padding-left: 20px;
width: 170px;
}
#panel {
border: 1px #000 solid;
box-shadow: 4px 6px 6px #444444;
cursor: crosshair;
}
.column2 > div {
margin-bottom: 10px;
}
#swImage {
border: 1px #000 solid;
box-shadow: 2px 3px 3px #444444;
cursor: pointer;
height: 25px;
line-height: 25px;
border-radius: 3px;
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
}
#swImage:hover {
margin-left: 2px;
}
#preview {
border: 1px #000 solid;
box-shadow: 2px 3px 3px #444444;
height: 80px;
width: 80px;
border-radius: 3px;
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
}
.column2 input[type=text] {
float: right;
width: 110px;
}
</style>
</head>
<body>
<div class="container">
<div class="column1">
<canvas id="panel" width="500" height="333"></canvas>
</div>
<div class="column2">
<div>
<input type="button" value="Next image" id="swImage" />
</div>
<div>Preview:</div>
<div id="preview"></div>
<div>Color:</div>
<div>R:
<input type="text" id="rVal" />
</div>
<div>G:
<input type="text" id="gVal" />
</div>
<div>B:
<input type="text" id="bVal" />
</div>
<div>RGB:
<input type="text" id="rgbVal" />
</div>
<div>RGBA:
<input type="text" id="rgbaVal" />
</div>
<div>HEX:
<input type="text" id="hexVal" />
</div>
<hr />
</div>
<div style="clear:both;"></div>
</div>
<script src="js/jquery-1.11.1.min.js"></script>
<script>
var canvas;
var ctx;
var images = [ // predefined array of used images
'images/pirate_01.jpg',
'images/pirate_02.jpg',
];
var iActiveImage = 0;
$(function() {
// drawing active image
var image = new Image();
image.onload = function() {
ctx.drawImage(image, 0, 0, image.width, image.height); // draw the image on the canvas
}
image.src = images[iActiveImage];
// creating canvas object
canvas = document.getElementById('panel');
ctx = canvas.getContext('2d');
$('#panel').mousemove(function(e) { // mouse move handler
var canvasOffset = $(canvas).offset();
var canvasX = Math.floor(e.pageX - canvasOffset.left);
var canvasY = Math.floor(e.pageY - canvasOffset.top);
var imageData = ctx.getImageData(canvasX, canvasY, 1, 1);
var pixel = imageData.data;
var pixelColor = "rgba(" + pixel[0] + ", " + pixel[1] + ", " + pixel[2] + ", " + pixel[3] + ")";
$('#preview').css('backgroundColor', pixelColor);
});
$('#panel').click(function(e) { // mouse click handler
var canvasOffset = $(canvas).offset();
var canvasX = Math.floor(e.pageX - canvasOffset.left);
var canvasY = Math.floor(e.pageY - canvasOffset.top);
var imageData = ctx.getImageData(canvasX, canvasY, 1, 1);
var pixel = imageData.data;
$('#rVal').val(pixel[0]);
$('#gVal').val(pixel[1]);
$('#bVal').val(pixel[2]);
$('#rgbVal').val(pixel[0] + ',' + pixel[1] + ',' + pixel[2]);
$('#rgbaVal').val(pixel[0] + ',' + pixel[1] + ',' + pixel[2] + ',' + pixel[3]);
var dColor = pixel[2] + 256 * pixel[1] + 65536 * pixel[0];
$('#hexVal').val('#' + dColor.toString(16));
});
$('#swImage').click(function(e) { // switching images
iActiveImage++;
if (iActiveImage >= 10) iActiveImage = 0;
image.src = images[iActiveImage];
});
});
</script>
</body>
</html>