Skip to main content

HTML5 Canvas 线条教程

要使用 Konva 创建一条简单的线条,我们可以实例化一个 Konva.Line() 对象。

有关属性和方法的完整列表,请参见 Line API 参考

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 redLine = new Konva.Line({
  points: [5, 70, 140, 23, 250, 60, 300, 20],
  stroke: 'red',
  strokeWidth: 15,
  lineCap: 'round',
  lineJoin: 'round'
});

// 线段长度为 33px,间隔为 10px
const greenLine = new Konva.Line({
  points: [5, 70, 140, 23, 250, 60, 300, 20],
  stroke: 'green',
  strokeWidth: 2,
  lineJoin: 'round',
  dash: [33, 10]
});

// 线段长度为 29px,间隔为 20px
// 后跟一个点(0.001px)和另一个 20px 的间隔
const blueLine = new Konva.Line({
  points: [5, 70, 140, 23, 250, 60, 300, 20],
  stroke: 'blue',
  strokeWidth: 10,
  lineCap: 'round',
  lineJoin: 'round',
  dash: [29, 20, 0.001, 20]
});

redLine.move({ x: 0, y: 5 });
greenLine.move({ x: 0, y: 55 });
blueLine.move({ x: 0, y: 105 });

layer.add(redLine, greenLine, blueLine);