简体   繁体   中英

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

I am working on a simple paint program with 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.

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. 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
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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