HTML5网页设计

其他代码

jQuery+CSS+HTML5颜色选择器

时间:2014-12-02 15:15来源:未知作者:user点击:
HTML5 Canvas实现的网页颜色选择器功能,首先用Canvas画一幅图,然后添加鼠标事件 鼠标移动,鼠标点击。在鼠标移动的时候,显示当前划过的颜色预览,在鼠标点击的时候,选中当前颜色,显示颜色的细节。

HTML5 Canvas实现的网页颜色选择器功能,首先用Canvas画一幅图,然后添加鼠标事件 “鼠标移动”,“鼠标点击”。在鼠标移动的时候,显示当前划过的颜色预览,在鼠标点击的时候,选中当前颜色,显示颜色的细节。


<!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" xml:lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery+CSS+HTML5-canvas颜色选择器 - ShejiCool.com</title>
<style>
*{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:400px;}
.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>
<script type="text/javascript" src="http://www.shejicool.com/demo/jquery.min.js"></script>
<script type="text/javascript">
var canvas;
var ctx;
var images = [ 
    'http://www.shejicool.com/images/demo/400_1.jpg',
    'http://www.shejicool.com/images/demo/400_2.jpg',
    'http://www.shejicool.com/images/demo/400_3.jpg',
    'http://www.shejicool.com/images/demo/400_4.jpg',
	'http://www.shejicool.com/images/demo/400_3.jpg'
];
var iActiveImage = 0;
$(function(){
    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];
    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 >= 5) iActiveImage = 0;
        image.src = images[iActiveImage];
    });
});
</script>
    </head>
    <body>
		<div style="text-align:center;color:#333;">请使用支持CSS3/HTML5的浏览器测试效果!</div>
        <div class="container">
            <div class="column1">
                <canvas id="panel" width="400" height="300"></canvas>
            </div>
            <div class="column2">
                <div><input type="button" value="下一张" 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>
        <footer>
            <h2>HTML5 canvas - Image color picker</h2>
            <a href="" class="stuts">Back to original tutorial on <span>Script Tutorials</span></a>
        </footer>
    </body>
</html>
上一篇:上一篇:动态改变input标记类型 上一篇:下一篇:jQuery用户注册表单验证代码
阅完本篇文章?您还可以: 收藏本文 文章挑错 朋友推荐