跳到主要内容

HTML5 Canvas 简单拖动边界教程

如需限制使用 Konva 拖放的图形的移动范围, 可以使用 dragmove 事件,并在事件处理程序中重新设置拖放位置。

可以使用此事件以多种方式限制拖放时的移动。例如,只允许水平、垂直、对角线或径向移动,甚至可以将节点 限制在方框、圆形或其他路径内。

shape.on('dragmove', () => {
// lock position of the shape on x axis
// keep y position as is
shape.x(0);
});

提示:你可以使用 shape.absolutePosition() 方法获取或设置节点的绝对位置,而不是使用相对的 xy

操作说明: 拖放横向文本,并观察它只能沿水平方向移动。 拖放纵向文本,并观察它只能沿垂直方向移动。

import Konva from 'konva';

const stage = new Konva.Stage({
  container: 'container',
  width: window.innerWidth,
  height: window.innerHeight,
});

const layer = new Konva.Layer();
stage.add(layer);

const horizontalText = new Konva.Text({
  x: 50,
  y: 50,
  text: 'Drag me horizontally',
  fontSize: 16,
  draggable: true,
  fill: 'black',
});

horizontalText.on('dragmove', function () {
  // horizontal only
  this.y(50);
});

const verticalText = new Konva.Text({
  x: 200,
  y: 50,
  text: 'Drag me vertically',
  fontSize: 16,
  draggable: true,
  fill: 'black',
});

verticalText.on('dragmove', function () {
  // vertical only
  this.x(200);
});

layer.add(horizontalText);
layer.add(verticalText);