roundedRect helper with support for different radius
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
Hey, I want to work on the issue #6500
I need to implement the function to draw a curved rectangle right?
would love to see this implemented! 🙌
The roundedRect helper was removed in #6734 as it was unused in the core.
CC: @benmccann @kurkle
Closing per my comment above. We removed this helper in v3