dmraptis
dmraptis

Reputation: 939

How to add border-radius to react-konva Image object?

I have an Image component in react-konva and want to add border-radius: 8px. Which is the easiest way?

Upvotes: 2

Views: 1206

Answers (2)

Babak Yaghoobi
Babak Yaghoobi

Reputation: 1985

Clip your image in Stage:

  // image.width, image.height
  <Stage width={200} height={200} style={{borderRadius: '8px', overflow: 'hidden'}}>
    <Layer >
      <Image image={this.state.image} />
    </Layer>
  </Stage>

Upvotes: 0

dmraptis
dmraptis

Reputation: 939

Having this amazing comment as reference the problem can be easily solved:

  ...

  <Group
    clipFunc={ctx => calcClipFunc(ctx, x, y, width, height, radius)}
  >
    <Image
      image={image}
      width={width}
      height={height}
      x={x}
      y={y}
    />
  </Group>

And the calcClipFunc() function from the previous comment:

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

Upvotes: 3

Related Questions