gpt4 book ai didi

javascript - Canvas 滴管

转载 作者:可可西里 更新时间:2023-11-01 13:35:38 26 4
gpt4 key购买 nike

我需要实现一个吸管工具。我想要它,所以您单击吸管按钮使其处于事件状态,然后使用 onmove 它将更改我的颜色选择器的颜色,当您使用 onclick 单击它时,它将使用以下方法将颜色设置为颜色选择器:

$('#colorpickerHolder').ColorPickerSetColor(eyeDropperColour); 

变量 eyeDropperColour 将使用 onlick 设置,基于您所处的颜色像素。我想知道我是否必须根据我拥有的层来执行此操作:canvas 和 canvasCursor。

我一直在看这个指南,但我不能让它适用于我的项目? http://palebluepixel.org/2011/11/16/html5-canvas-eyedropper/

这是我的项目: http://www.taffatech.com/Paint.html

我有:

var eyeDropperActive = false;
var eyeDropperColour;

和:

$("#brushEyeDropper").click(function() {

if ( eyeDropperActive == true)
{
eyeDropperActive = false;
}

else if ( eyeDropperActive == false)
{
eyeDropperActive = true;
}
});

最佳答案

创建 Canvas “吸管”工具

这是读取 Canvas 上任意 X/Y 像素颜色的方法:

    function getPixelColor(x, y) {
var pxData = ctx.getImageData(x,y,1,1);
return("rgb("+pxData.data[0]+","+pxData.data[1]+","+pxData.data[2]+")");
}

剩下的只是控制何时通过在 Canvas 上单击来接受颜色。

    var eyedropperIsActive=false; 

// Activate reading pixel colors when a #startDropper button is clicked
$("#startDropper").click(function(e){eyedropperIsActive=true;});

// if the tool is active, report the color under the mouse
$("#canvas").mousemove(function(e){handleMouseMove(e);});

// when the user clicks on the canvas, turn off the tool
// (the last color will remain selected)
$("#canvas").click(function(e){eyedropperIsActive=false;});

这里是调用 getPixelColor 并报告颜色的 mousemove 事件处理程序

    // if the tool is active, report any color under the mouse
function handleMouseMove(e){

if(!eyedropperIsActive){return;}

mouseX=parseInt(e.clientX-offsetX);
mouseY=parseInt(e.clientY-offsetY);

// Put your mousemove stuff here
var eyedropColor=getPixelColor(mouseX,mouseY);
$("#results").css("backgroundColor",getPixelColor(mouseX,mouseY));

}

这是代码和 fiddle :http://jsfiddle.net/m1erickson/zpfdv/

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>

<style>
body{ background-color: ivory; padding:30px; }
canvas{border:1px solid red;}
#results{width:30px; height:30px; border:1px solid blue;}
</style>

<script>
$(function(){

var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");

var canvasOffset=$("#canvas").offset();
var offsetX=canvasOffset.left;
var offsetY=canvasOffset.top;

var eyedropperIsActive=false;

drawTestColors(20,20,"red");
drawTestColors(100,20,"green");
drawTestColors(180,20,"blue");

function drawTestColors(x,y,color){
ctx.beginPath();
ctx.fillStyle=color;
ctx.rect(x,y,50,50);
ctx.fill();
}


function getPixelColor(x, y) {
var pxData = ctx.getImageData(x,y,1,1);
return("rgb("+pxData.data[0]+","+pxData.data[1]+","+pxData.data[2]+")");
}


function handleMouseMove(e){

if(!eyedropperIsActive){return;}

mouseX=parseInt(e.clientX-offsetX);
mouseY=parseInt(e.clientY-offsetY);

// Put your mousemove stuff here
var eyedropColor=getPixelColor(mouseX,mouseY);
$("#results").css("backgroundColor",getPixelColor(mouseX,mouseY));

}

$("#canvas").click(function(e){eyedropperIsActive=false;});
$("#canvas").mousemove(function(e){handleMouseMove(e);});
$("#startDropper").click(function(e){eyedropperIsActive=true;});

}); // end $(function(){});
</script>

</head>

<body>
<p>Click "Activate Eyedropper" to read pixel color under the mouse</p>
<p>Click canvas to set the color and de-active the eyedropper</p>
<canvas id="canvas" width=300 height=300></canvas><br>
<button id="startDropper">Activate Eyedropper</button>
<div id="results" width=30 height=30>&nbsp;</div>
</body>
</html>

关于javascript - Canvas 滴管,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17221802/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com