Skip to content

Commit

Permalink
feat(renderer): control finer rendering details in the render function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernankez committed Mar 22, 2024
1 parent badb016 commit 55c7b8c
Showing 1 changed file with 27 additions and 25 deletions.
52 changes: 27 additions & 25 deletions packages/core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ export interface CreateTextRendererOptions {
export interface RenderContext {
ctx: CanvasRenderingContext2D;
text: string;
rowIndex: number;
columnIndex: number;
renderItemWidth: number;
renderItemHeight: number;
rowGap: number;
Expand All @@ -18,13 +16,19 @@ export interface RenderContext {
columnCount: number;
width: number;
height: number;
angle: number;
degree: number;
}

export function textRender(context: RenderContext) {
const { ctx, text, rowIndex, columnIndex, renderItemWidth, renderItemHeight, rowGap, columnGap } = context;
const x = columnIndex * (renderItemWidth + columnGap);
const y = rowIndex * (renderItemHeight + rowGap);
ctx.fillText(text, x, y);
const { ctx, text, renderItemWidth, renderItemHeight, rowGap, columnGap, columnCount, rowCount } = context;
for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
for (let columnIndex = 0; columnIndex < columnCount; columnIndex++) {
const x = columnIndex * (renderItemWidth + columnGap);
const y = rowIndex * (renderItemHeight + rowGap);
ctx.fillText(text, x, y);
}
}
}

export function createTextRenderer(text: string, options?: CreateTextRendererOptions) {
Expand All @@ -37,29 +41,27 @@ export function createTextRenderer(text: string, options?: CreateTextRendererOpt
}
ctx.textBaseline = "top";
const { x, y } = calculateTranslate();
ctx.save();
ctx.translate(x, y);
ctx.rotate(angle);
const { width: renderItemWidth, height: renderItemHeight } = measureText(text);
const [rowCount, columnCount] = calculateRenderCount(renderItemWidth, renderItemHeight);
for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
for (let columnIndex = 0; columnIndex < columnCount; columnIndex++) {
const renderContext: RenderContext = {
ctx,
text,
rowIndex,
columnIndex,
renderItemWidth,
renderItemHeight,
rowGap,
columnGap,
rowCount,
columnCount,
width,
height,
};
render(renderContext);
}
}
const renderContext: RenderContext = {
ctx,
text,
renderItemWidth,
renderItemHeight,
rowGap,
columnGap,
rowCount,
columnCount,
width,
height,
angle,
degree,
};
render(renderContext);
ctx.restore();
};
return renderer;
}

0 comments on commit 55c7b8c

Please sign in to comment.