跳到主要内容

HTML5 Canvas 拖放舞台

如需使用 Konva 拖放舞台,可以在创建组时将配置对象的 draggable 属性 设置为 true,也可以使用 draggable() 方法。

与图形、组和图层等其他节点的拖放不同, 你可以拖动舞台的任意部分来拖动整个舞台。

import Konva from 'konva';

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

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

// create circle
const circle = new Konva.Circle({
  x: stage.width() / 2,
  y: stage.height() / 2,
  radius: 70,
  fill: 'red',
  stroke: 'black',
  strokeWidth: 4
});

// create text
const text = new Konva.Text({
  x: 10,
  y: 10,
  text: 'Drag the stage anywhere',
  fontSize: 20,
  fontFamily: 'Calibri',
  fill: 'black'
});

layer.add(circle);
layer.add(text);