javascript 使用Konva.层拖动边界

gudnpqoy  于 2022-11-20  发布在  Java
关注(0)|答案(1)|浏览(384)

我在我的项目中使用KonvaJs。我需要实现拖动绑定到Konva.Layer。我的层有这么多其他形状和图像。我需要限制层的移动高达50%的宽度和高度。我在这个plunkr.中所做的方法当用户使用鼠标滚轮放大或缩小层时会出现问题。缩放后,我不知道为什么拖动边界的行为不同。似乎我不能正确地做数学。我需要有相同的行为,即层的移动方式受到限制时,用户不执行缩放。这是我正在做的:

//... a helper object for zooming
var zoomHelper = {
    stage: null,
    scale: 1,
    zoomFactor: 1.1,
    origin: {
        x: 0,
        y: 0
    },
    zoom: function(event) {
        event.preventDefault();
        var delta;
        if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
            if (event.originalEvent.detail > 0) {
                //scroll down
                delta = 0.2;
            } else {
                //scroll up
                delta = 0;
            }
        } else {
            if (event.originalEvent.wheelDelta < 0) {
                //scroll down
                delta = 0.2;
            } else {
                //scroll up
                delta = 0;
            }
        }
        var evt = event.originalEvent,
            mx = evt.clientX - zoomHelper.stage.getX(),
            my = evt.clientY - zoomHelper.stage.getY(),
            zoom = (zoomHelper.zoomFactor - delta),
            newscale = zoomHelper.scale * zoom;
        zoomHelper.origin.x = mx / zoomHelper.scale + zoomHelper.origin
            .x - mx / newscale;
        zoomHelper.origin.y = my / zoomHelper.scale + zoomHelper.origin
            .y - my / newscale;
        zoomHelper.stage.setOffset({
            x: zoomHelper.origin.x,
            y: zoomHelper.origin.y
        });
        zoomHelper.stage.setScale({
            x: newscale,
            y: newscale
        });
        zoomHelper.stage.draw();
        zoomHelper.scale *= zoom;
        preCalculation();
    }
};

// Code goes here
var w = window.innerWidth;
var h = window.innerHeight;
var height, minX, minY, maxX, maxY;
var stage = new Konva.Stage({
  container: 'container',
  width: w,
  height: h
});
zoomHelper.stage =stage;
var layer = new Konva.Layer({
  draggable: true,
  dragBoundFunc: function(pos) {
    console.log('called');
    var X = pos.x;
    var Y = pos.y;
    if (X < minX) {
      X = minX;
    }
    if (X > maxX) {
      X = maxX;
    }
    if (Y < minY) {
      Y = minY;
    }
    if (Y > maxY) {
      Y = maxY;
    }
    return ({
      x: X,
      y: Y
    });
  }
});

stage.add(layer);

function preCalculation(){
  // pre-calc some bounds so dragBoundFunc has less calc's to do
height = layer.getHeight();
minX = stage.getX() - layer.getWidth() / 2;
maxX = stage.getX() + stage.getWidth() - layer.getWidth() / 2;
minY = stage.getY() - layer.getHeight() / 2;
maxY = stage.getY() + stage.getHeight() - layer.getHeight() / 2;
console.log(height, minX, minY, maxX, maxY);
}
preCalculation();

var img = new Image();
img.onload = function() {
  var floorImage = new Konva.Image({
    image: img,
    width: w,
    height: h
  });
  layer.add(floorImage);
  layer.draw();
};
img.src = 'https://s.yimg.com/pw/images/coverphoto02_h.jpg.v3';

$(stage.container).on('mousewheel DOMMouseScroll', zoomHelper.zoom);
5sxhfpxr

5sxhfpxr1#

当使用dragBoundFunc时,您必须返回层的绝对位置。当您更改顶部节点(舞台)的属性时,可能很难保持绝对位置。因此,您可以尝试在'dragmove'事件中设置绑定函数:

layer.on('dragmove', function() {
  var x = Math.max(minX, Math.min(maxX, layer.x()));
  var y = Math.max(minY, Math.min(maxY, layer.y()));
  layer.x(x);
  layer.y(y);
});

http://plnkr.co/edit/31MUmOjXBUVuaHVJsL3c?p=preview

相关问题