Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

roundedRect helper with support for different radius

Open tomasdev opened this issue 7 years ago • 4 comments

As per the TODO at https://github.com/chartjs/Chart.js/blob/master/src/helpers/helpers.canvas.js#L33

I have implemented the following, which works good so far:

function roundedRect (ctx, x, y, width, height, radius) {
  var TL, TR, BR, BL;

  // Minimum horizontal value: half of the width
  var mH = r => Math.min(r, width / 2);
  // Minimum vertical value: half of the width
  var mV = r => Math.min(r, height / 2);

  if (radius instanceof Array) {
    // [TL, TR, BR, BL]
    TL = radius[0];
    TR = radius[1];
    BR = radius[2];
    BL = radius[3];
  } else {
    TL = TR = BR = BL = radius;
  }

  ctx.beginPath();

  ctx.moveTo(x + mH(TL), y);
  ctx.lineTo(x + width - mH(TR), y);
  ctx.quadraticCurveTo(x + width, y, x + width, y + mV(TR));
  ctx.lineTo(x + width, y + height - mV(BR));
  ctx.quadraticCurveTo(x + width, y + height, x + width - mH(BR), y + height);
  ctx.lineTo(x + mH(BL), y + height);
  ctx.quadraticCurveTo(x, y + height, x, y + height - mV(BL));
  ctx.lineTo(x, y + mV(TL));
  ctx.quadraticCurveTo(x, y, x + mH(TL), y);

  ctx.closePath();
}

Perhaps this is useful to someone. I was working based off the old version of the helper, the new one uses .arc() instead of .quadraticCurveTo()

If I have time (no promises) I will create a PR following the style of the current version of the helper. Please point me towards the right test suite for this. :-)

tomasdev avatar Sep 05 '19 18:09 tomasdev

@tomasdev
Hey, I want to work on the issue #6500 I need to implement the function to draw a curved rectangle right?

jatiny17 avatar Sep 07 '19 10:09 jatiny17

would love to see this implemented! 🙌

alex-benoit avatar Oct 11 '19 20:10 alex-benoit

The roundedRect helper was removed in #6734 as it was unused in the core.

CC: @benmccann @kurkle

etimberg avatar May 31 '20 23:05 etimberg

Closing per my comment above. We removed this helper in v3

etimberg avatar Oct 04 '20 15:10 etimberg