Custom Rendering Logic
We provide some basic shape components (Rectangle, Circle, Ellipse, Line, Path) and transforms (Rotate, Translate, Scale) out of the box, but for anything else you want to do we provide a set of utils to handle your own rendering logic.
The key things here are useRenderBeforeChildren and useRenderAfterChildren which provide you direct access to the canvas context and will be executed before and or after your component's children have rendered.
interface RoundedRectangleProps extends RectangleProps {
radius: number;
}
const RoundedRectangle: RCXComponent<RoundedRectangleProps> = (props) => {
useRenderBeforeChildren((renderingContext) => {
const { x, y, width, height, radius, beginPath = true, closePath } = props;
renderingContext.ctx2d.save();
if (beginPath) {
renderingContext.ctx2d.beginPath();
}
renderingContext.ctx2d.moveTo(x + radius, y);
renderingContext.ctx2d.lineTo(x + width - radius, y);
renderingContext.ctx2d.arcTo(x + width, y, x + width, y + radius, radius);
renderingContext.ctx2d.lineTo(x + width, y + height - radius);
renderingContext.ctx2d.arcTo(
x + width,
y + height,
x + width - radius,
y + height,
radius
);
renderingContext.ctx2d.lineTo(x + radius, y + height);
renderingContext.ctx2d.arcTo(x, y + height, x, y + height - radius, radius);
renderingContext.ctx2d.lineTo(x, y + radius);
renderingContext.ctx2d.arcTo(x, y, x + radius, y, radius);
});
useRenderAfterChildren((renderingContext) => {
applyFillAndStrokeStyles(renderingContext, resolveStyles(props.style));
renderingContext.ctx2d.restore();
});
return props.children;
};