我在Image
中有一个react-konva
组件,想添加border-radius: 8px
。哪一种是最简单的方法?
以this amazing comment为参考,可以轻松解决问题:
...
<Group
clipFunc={ctx => calcClipFunc(ctx, x, y, width, height, radius)}
>
<Image
image={image}
width={width}
height={height}
x={x}
y={y}
/>
</Group>
以及先前注释中的calcClipFunc()
函数:
function calcClipFunc(ctx, x, y, width, height, radius) {
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.lineTo(x + width - radius, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
ctx.lineTo(x + width, y + height - radius);
ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
ctx.lineTo(x + radius, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
ctx.lineTo(x, y + radius);
ctx.quadraticCurveTo(x, y, x + radius, y);
ctx.closePath();
}
在舞台上剪切图像:
// image.width, image.height
<Stage width={200} height={200} style={{borderRadius: '8px', overflow: 'hidden'}}>
<Layer >
<Image image={this.state.image} />
</Layer>
</Stage>