简体   繁体   English

使用mousemove绘制形状,拖放和使用dynamicjs调整大小-KineticJS

[英]Draw shapes with mousemove, drag and drop and resize with kineticjs- KineticJS

I am working on a simple paint program with KineticJS. 我正在用KineticJS开发一个简单的绘画程序。 What I have to achieve is to draw a rectangle, line or circle with mouse movement and the drawn shapes can be dragged or resized by mouse. 我要实现的是通过鼠标移动来绘制矩形,直线或圆形,并且所绘制的形状可以通过鼠标拖动或调整大小。 My problem is, I have to use mousedown and mousemove event to do the drawing, but I cannot achieve drag and drop or resize at the same time. 我的问题是,我必须使用mousedownmousemove事件进行绘制,但是我无法同时实现拖放或调整大小。

Here is my code for the drawing: 这是我的绘图代码:

stage.on("mousedown", function() {
    if ((drawFlag == 1) && (dragFlag == 0)) {
        if (moving) {
            moving = false;
            layer.draw();
        } else {
            var mousePos = stage.getMousePosition();
            line = new Kinetic.Line({
                points: [0, 0, 50, 50],
                stroke: "red"
            });
            layer.add(line);

            line.getPoints()[0].x = mousePos.x;
            line.getPoints()[0].y = mousePos.y;
            line.getPoints()[1].x = mousePos.x;
            line.getPoints()[1].y = mousePos.y;

            moving = true;
            layer.drawScene();
        }
    }

    if ((drawFlag == 2) && (dragFlag == 0)) {
        if (moving) {
            moving = false;
            layer.draw();
        } else {
            var mousePos = stage.getMousePosition();
            rect = new Kinetic.Rect({
                x: 20,
                y: 20,
                fill: "red",
                stroke: "black",
                strokeWidth: 2,
                draggle: true,
                width: 0,
                height: 0
            });

            rect.setX(mousePos.x);
            rect.setY(mousePos.y);
            rect.setWidth(0);
            rect.setHeight(0);

            moving = true;
            layer.drawScene();



            layer.add(rect);

            Rects.push(rect);
        }
    }
});

stage.on("mousemove", function() {
    if ((drawFlag == 1) && (dragFlag == 0)) {
        if (moving) {
            var mousePos = stage.getMousePosition();
            var x = mousePos.x;
            var y = mousePos.y;
            line.getPoints()[1].x = mousePos.x;
            line.getPoints()[1].y = mousePos.y;
            moving = true;
            layer.drawScene();
        }
    }
    if ((drawFlag == 2) && (dragFlag == 0)) {
        if (moving) {
            var mousePos = stage.getMousePosition();
            var x = mousePos.x;
            var y = mousePos.y;
            rect.setWidth(mousePos.x - rect.getX());
            rect.setHeight(mousePos.y - rect.getY());
            moving = true;
            layer.drawScene();
        }
    }
});

stage.on("mouseup", function() {
    moving = false;
});

After drawing a rectangle, when I click on it, it is supposed to be dragged with mouse movement. 绘制矩形后,当我单击它时,应该通过鼠标移动来拖动它。 But, in my program, clicking on a drawn rectangle results in drawing another one instead of dragging it. 但是,在我的程序中,单击绘制的矩形会导致绘制另一个矩形而不是拖动它。 So I tried to use a dragFlag to indicate whether I am drawing or dragging. 因此,我尝试使用dragFlag指示我正在绘制还是拖动。 And I use a function to check whether mouse cursor is over any of the drawn rectangles: 我使用一个函数来检查鼠标光标是否在任何绘制的矩形上:

for (var n = 0; n < Rects.length; n++) {
    (function() {
        Rects[n].on('mouseover', function() {
            dragFlag = 1;
            document.body.style.cursor = "pointer";
        });

        Rects[n].on('mouseout', function() {
            dragFlag = 0;
            document.body.style.cursor = "default";
        });
        if (dragFlag == 1) {
            Rects[n].on("dragstart", function() {
                Rects[n].moveToTop();
                layer.draw();
            });

            Rects[n].on("dragmove", function() {
                document.body.style.cursor = "pointer";
            });
        }

    });

But it wouldn't work. 但这是行不通的。 The position of my mouse cursor cannot be checked so that I am stilling drawing a new rectangle while clicking on a drawn one. 无法检查鼠标光标的位置,因此在单击绘制的矩形时,我仍在绘制新的矩形。 Anyone knows how to achieve drawing with mouse movement, and drag and drop on drawn shapes? 有谁知道如何通过鼠标移动来实现绘制以及拖放绘制的形状?

I appreciate your help. 我感谢您的帮助。

Well, try debugging first with something simple like: 好吧,首先尝试使用类似以下的简单调试:

Rects[n].on('mouseover', function() {
    dragFlag = 1;
    alert('mouse over rectangle'); //check if event fired
    document.body.style.cursor = "pointer";
});

The real problem is that your logic is getting complicated and fast, you need to simplify it and refactor. 真正的问题是您的逻辑变得复杂且快速,您需要对其进行简化和重构。

Think of Paint, you can use the selection tool, or you can use the line tool, or the rectangle tool, that is not determined by the event on the stage, but by the mode the user is in. 考虑一下“画图”,您可以使用选择工具,也可以使用线条工具或矩形工具,它们不是由舞台上的事件决定的,而是由用户所处的模式决定的。

So what you should do is create buttons and detach/reattach functions to each event any time that the buttons are clicked. 因此,您应该做的就是在每次单击按钮时创建按钮并为每个事件分离/重新附加功能。

Example

drawLineButton.on('click', function() {
    stage.off(); // or whatever function to remove other events from stage
    stage.on('mousedown', function() {
        draw line logic
    });
});

drawRectButton.on('click', function() {
    stage.off(); // or whatever function to remove other events from stage
    stage.on('mousedown', function() {
        draw rectangle logic
    });
});

dragButton.on('click', function() {
    stage.off(); // or whatever function to remove other events from stage
    // now you dont have to worry about stage events firing and can drag shapes
});

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

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