HTML5 canvas – Image color picker. New interesting tutorial – I will show you how you can create nice and easy Image color picker using HTML5. Main idea – to draw a picture on the canvas, add event handlers to mouse move, to mouse click (for picking colors).
HTML5 canvas –图像颜色选择器。 新的有趣的教程–我将向您展示如何使用HTML5创建美观,简单的图像颜色选择器。 主要思想-在画布上绘制图片,将事件处理程序添加到鼠标移动,鼠标单击(用于拾取颜色)。
Here are our demo and downloadable package:
这是我们的演示和可下载的软件包:
现场演示
[sociallocker]
[社交储物柜]
打包下载
[/sociallocker]
[/ sociallocker]
Ok, download the source files and lets start coding !
好的,下载源文件并开始编码!
步骤1. HTML (Step 1. HTML)
Here are html code of our color picker page
这是我们的颜色选择器页面的html代码
index.html (index.html)
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="utf-8" />
<title>HTML5 canvas - Image color picker | Script Tutorials</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery-1.5.2.min.js"></script>
<script type="text/javascript" src="js/script.js"></script>
</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>
<footer>
<h2>HTML5 canvas - Image color picker</h2>
<a href="https://www.script-tutorials.com/html5-canvas-image-color-picker/" class="stuts">Back to original tutorial on <span>Script Tutorials</span></a>
</footer>
</body>
</html>
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="utf-8" />
<title>HTML5 canvas - Image color picker | Script Tutorials</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery-1.5.2.min.js"></script>
<script type="text/javascript" src="js/script.js"></script>
</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>
<footer>
<h2>HTML5 canvas - Image color picker</h2>
<a href="https://www.script-tutorials.com/html5-canvas-image-color-picker/" class="stuts">Back to original tutorial on <span>Script Tutorials</span></a>
</footer>
</body>
</html>
步骤2. CSS (Step 2. CSS)
Here are used CSS styles
这是使用CSS样式
css / main.css (css/main.css)
*{
margin:0;
padding:0;
}
body {
background-color:#bababa;
color:#fff;
font:14px/1.3 Arial,sans-serif;
}
footer {
background-color:#212121;
bottom:0;
box-shadow: 0 -1px 2px #111111;
display:block;
height:70px;
left:0;
position:fixed;
width:100%;
z-index:100;
}
footer h2{
font-size:22px;
font-weight:normal;
left:50%;
margin-left:-400px;
padding:22px 0;
position:absolute;
width:540px;
}
footer a.stuts,a.stuts:visited{
border:none;
text-decoration:none;
color:#fcfcfc;
font-size:14px;
left:50%;
line-height:31px;
margin:23px 0 0 110px;
position:absolute;
top:0;
}
footer .stuts span {
font-size:22px;
font-weight:bold;
margin-left:5px;
}
.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;
}
*{
margin:0;
padding:0;
}
body {
background-color:#bababa;
color:#fff;
font:14px/1.3 Arial,sans-serif;
}
footer {
background-color:#212121;
bottom:0;
box-shadow: 0 -1px 2px #111111;
display:block;
height:70px;
left:0;
position:fixed;
width:100%;
z-index:100;
}
footer h2{
font-size:22px;
font-weight:normal;
left:50%;
margin-left:-400px;
padding:22px 0;
position:absolute;
width:540px;
}
footer a.stuts,a.stuts:visited{
border:none;
text-decoration:none;
color:#fcfcfc;
font-size:14px;
left:50%;
line-height:31px;
margin:23px 0 0 110px;
position:absolute;
top:0;
}
footer .stuts span {
font-size:22px;
font-weight:bold;
margin-left:5px;
}
.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;
}
步骤3. JS (Step 3. JS)
js / script.js (js/script.js)
var canvas;
var ctx;
var images = [ // predefined array of used images
'images/pic1.jpg',
'images/pic2.jpg',
'images/pic3.jpg',
'images/pic4.jpg',
'images/pic5.jpg',
'images/pic6.jpg',
'images/pic7.jpg',
'images/pic8.jpg',
'images/pic9.jpg',
'images/pic10.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];
});
});
var canvas;
var ctx;
var images = [ // predefined array of used images
'images/pic1.jpg',
'images/pic2.jpg',
'images/pic3.jpg',
'images/pic4.jpg',
'images/pic5.jpg',
'images/pic6.jpg',
'images/pic7.jpg',
'images/pic8.jpg',
'images/pic9.jpg',
'images/pic10.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];
});
});
What I did: firstly I prepared canvas, context objects for our canvas, after – add event handlers to all necessary events. Then, in case of clicking of mouse moving – I obtaining hovered pixel details (RGBA values). All pretty easy, isn’t it? In result – our color picker ready!
我做了什么:首先,我准备了画布,画布的上下文对象,之后–将事件处理程序添加到所有必要的事件中。 然后,在单击鼠标移动的情况下–我获得了悬停的像素详细信息(RGBA值)。 一切都很容易,不是吗? 结果–我们的拾色器准备就绪!
现场演示
结论 (Conclusion)
Hope that today`s lesson was interesting for you. We made another one nice html5 sample. I will be glad to see your thanks and comments. Good luck!
希望今天的课程对您很有趣。 我们制作了另一个不错的html5示例。 看到您的感谢和评论,我将非常高兴。 祝好运!
翻译自: https://www.script-tutorials.com/creating-an-html5-canvas-image-color-picker/