1
我目前有一个简单的颜色选择器,它载入我创建的颜色渐变图像(PNG),当用户悬停或点击它时,获取光标下的颜色值。如何“遮罩”画布区域以执行其他操作?
在图像中,我在渐变的左侧内置了一个14x14像素区域,表示透明度,当用户在此区域中悬停或单击时,我想要清除它们的颜色选择(如单击透明) 。问题是我无法弄清楚这个部分,所以我正在寻找任何人的帮助。
希望这是有道理的。
这里是我的代码:
var img = new Image();
img.src = '/assets/images/results/colourpicker.png';
// Copy image (img) to canvas
img.onload = function() {
var c = document.getElementById('colourPickerBar');
var ctx = c.getContext('2d');
c.width = img.width;
c.height = img.height;
ctx.drawImage(img,0,0);
}
var rgb;
// get color on hover
$('#colourPickerBar').bind('mousemove', function(e){
var pos = findPos(this);
var x = e.pageX - pos.x; // get the x value of the cursor
var y = e.pageY - pos.y; // get the y value of the cursor
var ctx = document.getElementById('colourPickerBar').getContext('2d');
var img_data = ctx.getImageData(x, y, 1, 1).data;
var hex = "#" + ("000000" + rgbToHex(img_data[0], img_data[1], img_data[2])).slice(-6);
$('#colourPickerSample').css('background', 'none').css('background-color', hex); //sets the color block value
});
// get color on click
$('#colourPickerBar').bind('click', function(e){
var pos = findPos(this);
var x = e.pageX - pos.x; // get the x value of the cursor
var y = e.pageY - pos.y; // get the y value of the cursor
var ctx = document.getElementById('colourPickerBar').getContext('2d');
var img_data = ctx.getImageData(x, y, 1, 1).data;
var hex = ("000000" + rgbToHex(img_data[0], img_data[1], img_data[2])).slice(-6);
$('#colourPickerSample').css('background-color', hex); //sets the color block value
$('#colourSelectorInput').val(hex); //sets the color value in the search input
});
function findPos(obj) {
var curleft = 0, curtop = 0;
if (obj.offsetParent) {
do {
curleft += obj.offsetLeft;
curtop += obj.offsetTop;
} while (obj = obj.offsetParent);
return { x: curleft, y: curtop };
}
return undefined;
}
function rgbToHex(r, g, b) {
if (r > 255 || g > 255 || b > 255)
throw "Invalid color component";
return ((r << 16) | (g << 8) | b).toString(16);
}
编辑:解决由于以下一些帮助。现在,使用if ... else语句来决定何时采取在该地区采取什么行动:
// get color on hover
$('#colourPickerBar').bind('mousemove', function(e){
var pos = findPos(this);
var x = e.pageX - pos.x; // get the x value of the cursor
var y = e.pageY - pos.y; // get the y value of the cursor
var ctx = document.getElementById('colourPickerBar').getContext('2d');
var img_data = ctx.getImageData(x, y, 1, 1).data;
var hex = "#" + ("000000" + rgbToHex(img_data[0], img_data[1], img_data[2])).slice(-6);
if ((0 <= x) && (14 >= x) && (0 <= y) && (14 >= y)) {
$('#colourPickerSample').css('background-color', 'none').addClass("defaultBg");
} else {
$('#colourPickerSample').removeClass("defaultBg").css('background-color', hex);
}
});
// get color on click
$('#colourPickerBar').bind('click', function(e){
var pos = findPos(this);
var x = e.pageX - pos.x; // get the x value of the cursor
var y = e.pageY - pos.y; // get the y value of the cursor
var ctx = document.getElementById('colourPickerBar').getContext('2d');
var img_data = ctx.getImageData(x, y, 1, 1).data;
var hex = ("000000" + rgbToHex(img_data[0], img_data[1], img_data[2])).slice(-6);
if ((0 <= x) && (14 >= x) && (0 <= y) && (14 >= y)) {
$('#colourPickerSample').css('background-color', 'none').addClass("defaultBg");
$('#colourSelectorInput').val('HEX');
} else {
$('#colourPickerSample').removeClass("defaultBg").css('background-color', hex);
$('#colourSelectorInput').val(hex);
}
});
另外如果鼠标在那里,取消正常执行的代码可能是一个好主意。 – pimvdb
我很困惑,我会把它放在哪里。我对JS不是很好,我只知道拼凑其他人的东西来让它为我工作。假设我想遮挡的区域是图像的最左上角,所以从0,0开始到14,14。我想取消此区域的默认操作并执行其他操作(更改其他对象的CSS属性),而不是上述代码中的内容。 – mikestecker
想通了。我接受了你的公式并做出了一个if else语句: 'if((0 <= x)&&(14> = x)&&(0 <= y)&&(14> = y)){ $ #colourPickerSample')。css('background-color','none')。addClass(“defaultBg”); ('#colourPickerSample')。removeClass(“defaultBg”).css('background-color',hex);其他{ } \t}其他{ \t $('#colourPickerSample')。 //设置颜色块值 \t}' – mikestecker