简体   繁体   English

Javascript mouseup和mousedown事件

[英]Javascript mouseup and mousedown event

i am trying to drag and drop an image of a card on the canvas of the javascript but the mouseup event does not seem to be working even though it is inside the main(). 我试图将一张卡片的图像拖放到javascript的画布上,但是mouseup事件似乎不起作用,即使它位于main()内部。 Once the card is selected, it follows around the mouse but it does not seem to let go when i let go of the mouse. 一旦选择了卡,它就会围绕鼠标移动,但是当我放开鼠标时,它似乎并没有放开。 I also know that the image is repeated from not clearing the screen. 我也知道由于没有清除屏幕而重复了图像。

function main(){
var ctx = cvs.getContext("2d");
var img = new Image();
img.src = "allcards.png";
var imgX = 75;
var imgY = 75;
draw();

function draw(){
    ctx.drawImage(img,0,0,97,129,imgX,imgY,100,100);    
}

cvs.addEventListener("mouseup", function(ev){
        greaterX = false;
        lessX = false;
        greaterY = false;
        lessY = false;
    }
);

cvs.addEventListener("mousedown", function(ev){
        if(ev.clientX <= (imgX + 97)){
            var greaterX = true;
        }
        if(ev.clientY <= (imgY + 129)){
            var greaterY = true;
        }
        if(ev.clientX >= imgX){
            var lessX = true;
        }
        if(ev.clientY >= imgY){
            var lessY = true;
        }
        if(greaterX == true)
            if(greaterY == true)
                if(lessX == true)
                    if(lessY == true)
                        cvs.addEventListener("mousemove", function(ev){
                            var offsetX = (ev.clientX - imgX);
                            var offsetY = (ev.clientY - imgY);
                            imgX = imgX + offsetX;
                            imgY = imgY + offsetY;
                            draw();
                        });
    });
};

greaterX , lessX , etc, are all defined with var inside of your mousedown function, meaning their scope is limited to the mousedown function only. greaterXlessXmousedown函数内部定义为var ,这意味着它们的范围仅限于mousedown函数。

Therefore, it is useless to try and set them back to false inside of your mouseup function. 因此,在mouseup函数中尝试将它们设置回false是没有用的。 You need to declare your variables in the main part of your function: 您需要在函数的主要部分中声明变量:

function main() {
    var greaterX, lessX, greaterY, lessY;
    var ctx = cvs.getContext("2D");
    //etc...

Now, simply setting greaterX , lessX , etc back to false is not enough, because the mousemove event checker inside of mousedown is still active. 现在,仅将greaterXlessX等设置回false是不够的,因为mousedown内部的mousemove事件检查器仍然处于活动状态。 When you apply an event listener, it stays there until you remove it. 当您应用事件监听器时,它会一直保留在那里直到您将其删除。

So, the next step is to separate the mousemove event function into it's own function (I used "mouseMoveHandler" for the name) and remove the event listener using .removeEventListener(type, listener, useCapture) inside of mouseup . 因此,下一步是将mousemove事件函数分离为其自己的函数(我使用“ mouseMoveHandler”作为名称),并在mouseup内部使用.removeEventListener(type, listener, useCapture)删除事件侦听器。

The mousemove function: mousemove函数:

function mouseMoveHandler(ev) {
    offsetX = (ev.clientX - imgX);
    offsetY = (ev.clientY - imgY);
    imgX = imgX + offsetX;
    imgY = imgY + offsetY;
    draw();
}

The mousedown function (important part): mousedown功能(重要部分):

  if (greaterX === true) { //you need the brackets for multi-line if statements
        if (greaterY === true) {
            if (lessX === true) {
                if (lessY === true) {
                    cvs.addEventListener("mousemove",mouseMoveHandler,false);
                }
            }
        }
    }

And finally, the mouseup function: 最后, mouseup函数:

cvs.addEventListener("mouseup", function(ev) {
    greaterX = false;
    lessX = false;
    greaterY = false;
    lessY = false;
    cvs.removeEventListener('mousemove',mouseMoveHandler,false);
});

Here's a jsFiddle with the solution, but not using your image. 这是带有解决方案的jsFiddle ,但不使用您的图像。

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

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