跳到主要内容

HTML5 Canvas 设置图形描边颜色和宽度教程

要使用 Konva 设置图形的描边和描边宽度,可以在实例化图形时设置 strokestrokeWidth 属性,也可以使用 stroke()strokeWidth() 方法。

操作说明:将鼠标移到五边形上以更改其描边颜色和宽度。

import Konva from 'konva';

var width = window.innerWidth;
var height = window.innerHeight;

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

var pentagon = new Konva.RegularPolygon({
x: stage.width() / 2,
y: stage.height() / 2,
sides: 5,
radius: 70,
fill: 'red',
stroke: 'black',
strokeWidth: 4,
});

pentagon.on('mouseover', function () {
this.stroke('blue');
this.strokeWidth(20);
});

pentagon.on('mouseout', function () {
this.stroke('black');
this.strokeWidth(4);
});
// add the shape to the layer
layer.add(pentagon);

// add the layer to the stage
stage.add(layer);