PHP前端开发

使用HTML5技术开发的超酷颜色选择器

百变鹏仔 2个月前 (10-18) #H5教程
文章标签 超酷

可能大家见过很多使用jquery/js开发的颜色选择器,今天这里我们将使用HTML5技术来自己实现一个更棒的颜色选择器。希望大家喜欢!

HTML代码

<!-- preview element --><p class="preview"></p><!-- colorpicker element --><p class="colorpicker" style="display:none">    <canvas id="picker" var="1" width="300" height="300"></canvas>    <p class="controls">        <p><label>r</label> <input type="text" id="rval" /></p>        <p><label>G</label> <input type="text" id="gVal" /></p>        <p><label>B</label> <input type="text" id="bVal" /></p>        <p><label>RGB</label> <input type="text" id="rgbVal" /></p>        <p><label>HEX</label> <input type="text" id="hexVal" /></p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/cb6835dc7db1" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">前端免费学习笔记(深入)</a>”;</p>    </p></p>

代码很简单,包含了2个部分,一个点击元素,一个用来展示颜色选择器的元素。

Javascript代码

$(function(){    var bCanPreview = true; // can preview    // create canvas and context objects    var canvas = document.getElementById(&#39;picker&#39;);    var ctx = canvas.getContext(&#39;2d&#39;);    // 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    }    // select desired colorwheel    var imagesrc="images/colorwheel1.png";    switch ($(canvas).attr(&#39;var&#39;)) {        case &#39;2&#39;:            imagesrc="images/colorwheel2.png";            break;        case &#39;3&#39;:            imagesrc="images/colorwheel3.png";            break;        case &#39;4&#39;:            imagesrc="images/colorwheel4.png";            break;        case &#39;5&#39;:            imagesrc="images/colorwheel5.png";            break;    }    image.src = imageSrc;    $(&#39;#picker&#39;).mousemove(function(e) { // mouse move handler        if (bCanPreview) {            // get coordinates of current position            var canvasOffset = $(canvas).offset();            var canvasX = Math.floor(e.pageX - canvasOffset.left);            var canvasY = Math.floor(e.pageY - canvasOffset.top);            // get current pixel            var imageData = ctx.getImageData(canvasX, canvasY, 1, 1);            var pixel = imageData.data;            // update preview color            var pixelColor = "rgb("+pixel[0]+", "+pixel[1]+", "+pixel[2]+")";            $(&#39;.preview&#39;).css(&#39;backgroundColor&#39;, pixelColor);            // update controls            $(&#39;#rVal&#39;).val(pixel[0]);            $(&#39;#gVal&#39;).val(pixel[1]);            $(&#39;#bVal&#39;).val(pixel[2]);            $(&#39;#rgbVal&#39;).val(pixel[0]+&#39;,&#39;+pixel[1]+&#39;,&#39;+pixel[2]);            var dColor = pixel[2] + 256 * pixel[1] + 65536 * pixel[0];            $(&#39;#hexVal&#39;).val(&#39;#&#39; + (&#39;0000&#39; + dColor.toString(16)).substr(-6));        }    });    $(&#39;#picker&#39;).click(function(e) { // click event handler        bCanPreview = !bCanPreview;    });    $(&#39;.preview&#39;).click(function(e) { // preview click        $(&#39;.colorpicker&#39;).fadeToggle("slow", "linear");        bCanPreview = true;    });});

大家可以看到,这是一个非常短的js代码,用来创建新的画布和对象,然后我们画出一个圆形的颜色板。你可以选择不同颜色底板。 这里使用一个参数来设定不同的选择。如下:

<canvas id="picker" var="1" width="300" height="300"></canvas><canvas id="picker" var="2" width="300" height="300"></canvas><canvas id="picker" var="3" width="300" height="300"></canvas><canvas id="picker" var="4" width="300" height="300"></canvas><canvas id="picker" var="5" width="300" height="300"></canvas>

下面我们添加事件:mousemove,click事件。这里使用jQuery来实现选择器的展现和隐藏。

$(&#39;.preview&#39;).click(function(e) { // preview click     $(&#39;.colorpicker&#39;).fadeToggle("slow", "linear");     bCanPreview = true; });
$(&#39;#picker&#39;).mousemove(function(e) { // mouse move handler    if (bCanPreview) {        // get coordinates of current position        var canvasOffset = $(canvas).offset();        var canvasX = Math.floor(e.pageX - canvasOffset.left);        var canvasY = Math.floor(e.pageY - canvasOffset.top);        // get current pixel        var imageData = ctx.getImageData(canvasX, canvasY, 1, 1);        var pixel = imageData.data;        // update preview color        var pixelColor = "rgb("+pixel[0]+", "+pixel[1]+", "+pixel[2]+")";        $(&#39;.preview&#39;).css(&#39;backgroundColor&#39;, pixelColor);        // update controls        $(&#39;#rVal&#39;).val(pixel[0]);        $(&#39;#gVal&#39;).val(pixel[1]);        $(&#39;#bVal&#39;).val(pixel[2]);        $(&#39;#rgbVal&#39;).val(pixel[0]+&#39;,&#39;+pixel[1]+&#39;,&#39;+pixel[2]);        var dColor = pixel[2] + 256 * pixel[1] + 65536 * pixel[0];        $(&#39;#hexVal&#39;).val(&#39;#&#39; + (&#39;0000&#39; + dColor.toString(16)).substr(-6));    }});$(&#39;#picker&#39;).click(function(e) { // click event handler    bCanPreview = !bCanPreview;});

CSS代码

不同颜色底板的CSS:

/* colorpicker styles */.colorpicker {    background-color: #222222;    border-radius: 5px 5px 5px 5px;    box-shadow: 2px 2px 2px #444444;    color: #FFFFFF;    font-size: 12px;    position: absolute;    width: 460px;}#picker {    cursor: crosshair;    float: left;    margin: 10px;    border: 0;}.controls {    float: right;    margin: 10px;}.controls > p {    border: 1px solid #2F2F2F;    margin-bottom: 5px;    overflow: hidden;    padding: 5px;}.controls label {    float: left;}.controls > p input {    background-color: #121212;    border: 1px solid #2F2F2F;    color: #DDDDDD;    float: right;    font-size: 10px;    height: 14px;    margin-left: 6px;    text-align: center;    text-transform: uppercase;    width: 75px;}.preview {    background: url("../images/select.png") repeat scroll center center transparent;    border-radius: 3px;    box-shadow: 2px 2px 2px #444444;    cursor: pointer;    height: 30px;    width: 30px;}

希望大家喜欢!