如何向反应对象图像对象添加边界半径?

问题描述 投票:0回答:2

我在Image中有一个react-konva组件,想添加border-radius: 8px。哪一种是最简单的方法?

reactjs konvajs react-konva
2个回答
0
投票

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();
}

0
投票

在舞台上剪切图像:

  // image.width, image.height
  <Stage width={200} height={200} style={{borderRadius: '8px', overflow: 'hidden'}}>
    <Layer >
      <Image image={this.state.image} />
    </Layer>
  </Stage>
© www.soinside.com 2019 - 2024. All rights reserved.