简体   繁体   English

在HTML5画布上创建颜色选择器

[英]Creating colorpicker on HTML5 canvas

如何在HTML5画布上绘制颜色选择器?

A basic example would be using getImageData : http://jsfiddle.net/eGjak/60/ . 一个基本的示例是使用getImageDatahttp : //jsfiddle.net/eGjak/60/

var ctx = $('#cv').get(0).getContext('2d');

for(var i = 0; i < 30; i++) {
    for(var j = 0; j < 30; j++) {
        ctx.fillStyle = 'rgb(' + 
            ((i/30*255)|0) + ',' + 
            ((j/30*255)|0) + ',' +
            '0)';

        ctx.fillRect(i * 10, j * 10, 10, 10);
    }
}

$('#cv').click(function(e) {
    // get pixel under mouse cursor
    var data = ctx.getImageData(e.offsetX, e.offsetY, 1, 1).data;
    alert('rgb: ' + [].slice.call(data, 0, 3).join());
});

I created a solution for you on HCT. 我为您在HCT上创建了一个解决方案。 You can see it here: 在这里你可以看到它:

http://www.html5canvastutorials.com/labs/html5-canvas-color-picker/ http://www.html5canvastutorials.com/labs/html5-canvas-color-picker/

The idea is to find a color picker image that you like and then draw it on the canvas. 这个想法是找到您喜欢的颜色选择器图像,然后将其绘制在画布上。 On the mousedown event, we can get the mouse coordinates and then use the image data of the color picker image to pick out the color. 在mousedown事件中,我们可以获取鼠标坐标,然后使用颜色选择器图像的图像数据来选择颜色。

Hope this helps! 希望这可以帮助!

You have to draw the colors manually. 您必须手动绘制颜色。 Then you need to listen for mouseclick in that area, and then get the color at the click position. 然后,您需要在该区域中监听mouseclick,然后在单击位置获得颜色。

The biggest problem is how to draw the colors. 最大的问题是如何绘制颜色。 There are a few examples in Drawing Color Spectrums . 在“ 绘制色谱图”中有一些示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM