在Konva JS

时间:2016-07-16 08:28:41

标签: javascript konvajs

我在我的项目中使用Konva JS。我在按钮点击上添加一个可拖动的形状。点击形状我需要获得形状的X和Y位置。 getXgetY函数返回原始X和Y.如何在拖动后更新X和Y.

以下示例代码。

 var stage = new Konva.Stage({
        container: 'canvas', // id of container <div>
        width: 500,
        height:300
    });
    
 jQuery("#add-shape").click(function(){
 addShape();
 });
 
 var addShape = function(){
 
 console.log("add shape");
 
 var layer = new Konva.Layer();
 var parentContainer = new Konva.Rect({
            x: stage.getWidth() / 2,
            y: stage.getHeight() / 2,
            width: 200,
            height: 60,
            cornerRadius: 10,
            fill: '#ccc'
        });
        
        var smallCircle = new Konva.Circle({
            x: stage.getWidth() / 2 + 200,
            y: stage.getHeight() / 2 + 30,
            radius: 15,
            fill: "#F2784B",
            stroke: "white",
            strokeWidth: 2
        });
        
        smallCircle.on('click', function() {
         console.log(this.getX(),this.getY());
           addArrow(this.getX(),this.getY());
          //get current X and Y here instead of origina X and Y
        });
        layer.add(parentContainer);
        layer.add(smallCircle);
        layer.draggable('true');
        stage.add(layer);
}

var addArrow = function(arrowX,arrowY){
  var connectorLayer = new Konva.Layer();
	var connector = new Konva.Arrow({
            points: [arrowX,arrowY,arrowX+10,arrowY],
            pointerLength: 4,
            pointerWidth: 4,
            fill: 'black',
            stroke: 'black',
            strokeWidth: 2
        });
    connectorLayer.add(connector);
    stage.add(connectorLayer);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/konvajs/konva/1.0.2/konva.min.js"></script>
<button id="add-shape">
      Add shape
      </button>
<div id="canvas">
        
      </div>

4 个答案:

答案 0 :(得分:5)

如果您需要获得鼠标位置,可以使用:

smallCircle.on('click', function() {
     console.log(stage.getPointerPosition());
});

答案 1 :(得分:2)

    box.on('mouseout', function () {
        document.body.style.cursor = 'default';
        console.log(box.attrs.x);
        console.log(box.attrs.y);
    });

答案 2 :(得分:0)

我不知道这是否是您要寻找的内容,为时已晚,但是无论如何我都会将其发布给将来的开发人员。 可以说这是我在图层和bluh内的水印图像,我希望它位于getX()和getY()的位置:我使用这样的组:

首先添加一些常规图片:

        function update(activeAnchor) {
        var group = activeAnchor.getParent();

        var topLeft = group.get('.topLeft')[0];
        var topRight = group.get('.topRight')[0];
        var bottomRight = group.get('.bottomRight')[0];
        var bottomLeft = group.get('.bottomLeft')[0];
        var image = group.get('Image')[0];

        var anchorX = activeAnchor.getX();
        var anchorY = activeAnchor.getY();

        // update anchor positions
        switch (activeAnchor.getName()) {
            case 'topLeft':
                topRight.setY(anchorY);
                bottomLeft.setX(anchorX);
                break;
            case 'topRight':
                topLeft.setY(anchorY);
                bottomRight.setX(anchorX);
                break;
            case 'bottomRight':
                bottomLeft.setY(anchorY);
                topRight.setX(anchorX);
                break;
            case 'bottomLeft':
                bottomRight.setY(anchorY);
                topLeft.setX(anchorX);
                break;
        }

        image.position(topLeft.position());

        var width = topRight.getX() - topLeft.getX();
        var height = bottomLeft.getY() - topLeft.getY();
        if (width && height) {
            image.width(width);
            image.height(height);
        }
    }
    function addAnchor(group, x, y, name) {
        var stage = group.getStage();
        var layer = group.getLayer();

        var anchor = new Konva.Circle({
            x: x,
            y: y,
            stroke: '#666',
            fill: '#ddd',
            strokeWidth: 2,
            radius: 8,
            name: name,
            draggable: true,
            dragOnTop: false
        });

        anchor.on('dragmove', function () {
            update(this);
            layer.draw();
        });
        anchor.on('mousedown touchstart', function () {
            group.setDraggable(false);
            this.moveToTop();
        });
        anchor.on('dragend', function () {
            group.setDraggable(true);
            layer.draw();
        });
        // add hover styling
        anchor.on('mouseover', function () {
            var layer = this.getLayer();
            document.body.style.cursor = 'pointer';
            this.setStrokeWidth(4);
            layer.draw();
        });
        anchor.on('mouseout', function () {
            var layer = this.getLayer();
            document.body.style.cursor = 'default';
            this.setStrokeWidth(2);
            layer.draw();
        });

        group.add(anchor);
    }

    var stage = new Konva.Stage({
        container: 'container',
        width: 595,
        height: 842
    });

    var layer = new Konva.Layer();
    stage.add(layer);
    //WaterMark
    var WaterMarkImg = new Konva.Image({
        width: 595,
        height: 842
    });
    var WaterMarkGroup = new Konva.Group({
        x: 0,
        y: 0,
        draggable: true
    });
    layer.add(WaterMarkGroup);
    WaterMarkGroup.add(WaterMarkImg);
    addAnchor(WaterMarkGroup, 0, 0, 'topLeft');
    addAnchor(WaterMarkGroup, 595, 0, 'topRight');
    addAnchor(WaterMarkGroup, 595, 842, 'bottomRight');
    addAnchor(WaterMarkGroup, 0, 842, 'bottomLeft');

    var imageObj1 = new Image();
    imageObj1.onload = function () {
        WaterMarkImg.image(imageObj1);
        layer.draw();
    };
    imageObj1.src = "some image Url";

这就是getX():非常简单

    var x = WaterMarkGroup.getX();
        alert(x);

我希望这对寻找它的人有所帮助。

答案 3 :(得分:0)

这不是问题的确切答案。 point是可拖动的形状。

point.on('dragmove', (t) => {
    console.log("dragmove", t.target.x(), t.target.y());
});