Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui-charts: simplify fillRect() with getPoint() #964

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions ui-charts/src/spaceTimeChart/components/ZoomRect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ export const ZoomRect = (rect: CanvasRect) => {
(ctx, context) => {
ctx.save();
ctx.fillStyle = '#0000000D'; /* black5 */
const { areaTimeSize, areaSpaceSize } = fillRect(ctx, rect, context);
if (areaTimeSize && areaSpaceSize) {
const width = context.timeAxis === 'x' ? areaTimeSize : areaSpaceSize;
const height = context.timeAxis === 'x' ? areaSpaceSize : areaTimeSize;

const { width, height } = fillRect(ctx, rect, context);
if (width && height) {
ctx.lineWidth = LINE_WIDTH;

for (let i = 0; Math.abs(i) < Math.abs(width); i += SPACING * Math.sign(width)) {
Expand Down
30 changes: 11 additions & 19 deletions ui-charts/src/spaceTimeChart/utils/canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,27 +348,19 @@ export type CanvasRect = {

export function fillRect(
ctx: CanvasRenderingContext2D,
rect: CanvasRect,
spaceTimeContext: SpaceTimeChartContextType
{ timeStart, timeEnd, spaceStart, spaceEnd }: CanvasRect,
{ getPoint }: SpaceTimeChartContextType
) {
const { getTimePixel, getSpacePixel, timeAxis } = spaceTimeContext;
const { timeStart, timeEnd, spaceStart, spaceEnd } = rect;
const topLeft = getPoint({ time: Number(timeStart), position: spaceStart });
const bottomRight = getPoint({ time: Number(timeEnd), position: spaceEnd });

const timeStartPixel = getTimePixel(Number(timeStart));
const endTimePixel = getTimePixel(Number(timeEnd));
const spaceStartPixel = getSpacePixel(spaceStart);
const spaceEndPixel = getSpacePixel(spaceEnd);
const width = bottomRight.x - topLeft.x;
const height = bottomRight.y - topLeft.y;

const areaSpaceSize = spaceEndPixel - spaceStartPixel;
const areaTimeSize = endTimePixel - timeStartPixel;
if (!areaSpaceSize || !areaTimeSize) return {};

if (timeAxis === 'x') {
ctx.translate(timeStartPixel, spaceStartPixel);
ctx.fillRect(0, 0, areaTimeSize, areaSpaceSize);
} else {
ctx.translate(spaceStartPixel, timeStartPixel);
ctx.fillRect(0, 0, areaSpaceSize, areaTimeSize);
if (width !== 0 && height !== 0) {
ctx.translate(topLeft.x, topLeft.y);
ctx.fillRect(0, 0, width, height);
}
return { areaTimeSize, areaSpaceSize };

return { width, height };
}