如何指定mousemove触发的不同位置?

时间:2014-03-15 02:30:28

标签: javascript canvas

代码

<!DOCTYPE html>
<html>
<head>
<title>TEST</title>
<style>
body {
background-color:#E4E0FF;
}
#canvas {
border:10px solid red;
background-color:white;
}
#canvas1 {
border:10px solid red;
background-color:white;
}
#canvas2 {
border:10px solid red;
background-color:white;
}
</style>
</head>
<body>
<h1>My canvas Art Gallery</h1>
<canvas id="canvas" width="400" height="300">
</canvas>
<canvas id="canvas1" width="400" height="300">
</canvas>
<canvas id="canvas2" width="400" height="300">
</canvas>
<script src="http://code.jquery.com/jquery-latest.min.js"
    type="text/javascript"></script>

<script type="text/javascript">
var c = document.getElementById("canvas");
var ctx = c.getContext("2d");
var $canvas = $("#canvas");
var canvasOffset = $canvas.offset();
var offsetX = canvasOffset.left;
var offsetY = canvasOffset.top;
var scrollX = $canvas.scrollLeft();
var scrollY = $canvas.scrollTop();
var changeRadius;
var changeWidth;
var isDown = false;
var startX;
var startY;

var toggle=0;
var x=150;
var y=100;
var w=100;
var h=100;
var r=60;
var wasInside=false;

ctx.fillStyle = "#000000";
ctx.fillRect(x, y, w, h);

function changeColor() {
if (toggle == 0) {
ctx.fillStyle = "#04B45F";
toggle = 1;
} else if (toggle ==1){
ctx.fillStyle = "#04B45F";
toggle = 2;
}else if (toggle == 2){
ctx.fillStyle = "#0000FF";
toggle = 3;
}else if (toggle == 3){
ctx.fillStyle == "#190707";
toggle = 4;
}else if (toggle == 4){
ctx.fillStyle = "#210B61";
toggle = 5;
}else if (toggle == 5){
ctx.fillStyle = "#FA58AC";
toggle = 6;
}else if (toggle ==6){
ctx.fillStyle = "#FFFF00";
toggle = 7;
}else{
ctx.fillStyle = "#F5A9D0";
toggle = 0;
}

ctx.fillRect(x, y, w, h);
}



(function () {

var c1 = document.getElementById("canvas1");
var ctx = c1.getContext("2d");
var $canvas = $("#canvas1");
var toggle = 0;
var x = 150;
var y = 100;
var w = 100;
var h = 100;
var r = 60;
var wasInside = false;

ctx.beginPath();
ctx.arc(200, 150, r, 0, 2 * Math.PI);
ctx.stroke();
changeRadius = function changeRadius() {
    ctx.fillStyle = "#FFFFFF";
    ctx.fillRect(0, 0, 400, 300);
    r = Math.floor((Math.random() * 80) + 20);
    ctx.beginPath();
    ctx.arc(200, 150, r, 0, 2 * Math.PI);
    ctx.stroke();
 }
})();

(function () {
var c = document.getElementById("canvas2");
var ctx = c.getContext("2d");
var $canvas = $("#canvas2");

var toggle = 0;
var x = 150;
var y = 100;
var w = 100;
var h = 100;
var width = 2;
var wasInside = false;

ctx.lineWidth = width;
ctx.moveTo(150,100);
ctx.lineTo(250,200);
ctx.stroke();


    changeWidth = function changeWidth() {
ctx.fillStyle = "#FFFFFF";
ctx.fillRect(0,0, 400, 300);    
width = Math.floor((Math.random()*50)+1);
ctx.lineWidth=width;
ctx.stroke();

}
})();

function handleMouseMove(e, canv) {
console.log(canv);
var mx = parseInt(e.clientX - offsetX);
var my = parseInt(e.clientY - offsetY);

var isInsideNow = (mx > x && mx < x + w && my > y && my <= y + h);

if (isInsideNow && !wasInside) {
    switch(canv.id){
        case 'canvas':
    changeColor();
            break;
        case 'canvas1' :
    changeRadius();
            break;
        case 'canvas2':
    changeWidth();
            break;
    }
    wasInside = true;
 } else if (!isInsideNow && wasInside) {
    wasInside = false;
 }

 }

$("#canvas, #canvas1, #canvas2").mousemove(function (e) {
handleMouseMove(e,this);
});

</script>
</body>
</html>

目标是改变第一个颜色,第二个圆圈尺寸和第三个画布中的线宽。 所以,问题是x,y,w和h定义的位置在第一个画布中,我无法通过鼠标得到第2和第3个画布的任何反应。这是小提琴 http://jsfiddle.net/x5xH5/23/ 我怎么解决这个问题?

1 个答案:

答案 0 :(得分:0)

在这里,我可以使用.mouseenter并删除那些isInside和其他人,但我不确定它是否满足您的要求:

DEMO

我改变了什么:

// Just the last few lines
function handleMouseMove(e, canv) {
console.log(canv);
    switch(canv.id){
        case 'canvas':
            changeColor();
            break;
        case 'canvas1' :
            changeRadius();
            break;
        case 'canvas2':
            changeWidth();
            break;
    }

}

$("#canvas, #canvas1, #canvas2").mouseenter(function (e) {
    handleMouseMove(e, this);
});