Skip to content

Commit

Permalink
refactor(hiccup-canvas): rename internals
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Oct 12, 2021
1 parent 94622fe commit 043e3eb
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 19 deletions.
6 changes: 3 additions & 3 deletions packages/hiccup-canvas/src/arc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { IObjectOf } from "@thi.ng/api";
import { TAU } from "@thi.ng/math/api";
import type { ReadonlyVec } from "@thi.ng/vectors";
import { endShape } from "./internal/end-shape";
import { __endShape } from "./internal/end-shape";

export const circularArc = (
ctx: CanvasRenderingContext2D,
Expand All @@ -14,7 +14,7 @@ export const circularArc = (
) => {
ctx.beginPath();
ctx.arc(pos[0], pos[1], r, start, end, antiCCW);
endShape(ctx, attribs);
__endShape(ctx, attribs);
};

export const ellipticArc = (
Expand All @@ -29,5 +29,5 @@ export const ellipticArc = (
) => {
ctx.beginPath();
ctx.ellipse(pos[0], pos[1], r[0], r[1], axis, start, end, ccw);
endShape(ctx, attribs);
__endShape(ctx, attribs);
};
14 changes: 9 additions & 5 deletions packages/hiccup-canvas/src/draw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import type { DrawState } from "./api";
import { circularArc, ellipticArc } from "./arc";
import { defLinearGradient, defRadialGradient } from "./color";
import { image } from "./image";
import { mergeState, registerGradient, restoreState } from "./internal/state";
import {
__mergeState,
__registerGradient,
__restoreState,
} from "./internal/state";
import { line, lines } from "./line";
import { packedPoints } from "./packed-points";
import { path } from "./path";
Expand All @@ -30,7 +34,7 @@ export const draw = (
}
return;
}
const state = mergeState(ctx, pstate, shape[1]);
const state = __mergeState(ctx, pstate, shape[1]);
const attribs = state ? state.attribs : pstate.attribs;
if (attribs.__skip) return;
switch (shape[0]) {
Expand All @@ -39,14 +43,14 @@ export const draw = (
defs(ctx, state, pstate, shape);
break;
case "linearGradient":
registerGradient(
__registerGradient(
pstate,
shape[1].id,
defLinearGradient(ctx, shape[1], shape[2])
);
break;
case "radialGradient":
registerGradient(
__registerGradient(
pstate,
shape[1].id,
defRadialGradient(ctx, shape[1], shape[2])
Expand Down Expand Up @@ -114,7 +118,7 @@ export const draw = (
);
default:
}
state && restoreState(ctx, pstate, state);
state && __restoreState(ctx, pstate, state);
};

const defs = (
Expand Down
2 changes: 1 addition & 1 deletion packages/hiccup-canvas/src/internal/end-shape.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { IObjectOf } from "@thi.ng/api";

/** @internal */
export const endShape = (
export const __endShape = (
ctx: CanvasRenderingContext2D,
attribs: IObjectOf<any>
) => {
Expand Down
13 changes: 7 additions & 6 deletions packages/hiccup-canvas/src/internal/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,31 @@ const CTX_ATTRIBS: IObjectOf<string> = {
weight: "lineWidth",
};

const newState = (state: DrawState, restore = false) => ({
/** @internal */
const __newState = (state: DrawState, restore = false) => ({
attribs: { ...state.attribs },
grads: { ...state.grads },
edits: [],
restore,
});

/** @internal */
export const mergeState = (
export const __mergeState = (
ctx: CanvasRenderingContext2D,
state: DrawState,
attribs: IObjectOf<any>
) => {
let res: DrawState | undefined;
if (!attribs) return;
if (applyTransform(ctx, attribs)) {
res = newState(state, true);
res = __newState(state, true);
}
for (let id in attribs) {
const k = CTX_ATTRIBS[id];
if (k) {
const v = attribs[id];
if (v != null && state.attribs[id] !== v) {
!res && (res = newState(state));
!res && (res = __newState(state));
res.attribs[id] = v;
res.edits!.push(id);
setAttrib(ctx, state, id, k, v);
Expand All @@ -84,7 +85,7 @@ export const mergeState = (
};

/** @internal */
export const restoreState = (
export const __restoreState = (
ctx: CanvasRenderingContext2D,
prev: DrawState,
curr: DrawState
Expand All @@ -103,7 +104,7 @@ export const restoreState = (
};

/** @internal */
export const registerGradient = (
export const __registerGradient = (
state: DrawState,
id: string,
g: CanvasGradient
Expand Down
4 changes: 2 additions & 2 deletions packages/hiccup-canvas/src/path.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IObjectOf } from "@thi.ng/api";
import type { ReadonlyVec } from "@thi.ng/vectors";
import { endShape } from "./internal/end-shape";
import { __endShape } from "./internal/end-shape";

export const path = (
ctx: CanvasRenderingContext2D,
Expand Down Expand Up @@ -109,5 +109,5 @@ export const path = (
ctx.closePath();
}
}
endShape(ctx, attribs);
__endShape(ctx, attribs);
};
4 changes: 2 additions & 2 deletions packages/hiccup-canvas/src/polygon.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IObjectOf } from "@thi.ng/api";
import type { ReadonlyVec } from "@thi.ng/vectors";
import { endShape } from "./internal/end-shape";
import { __endShape } from "./internal/end-shape";

export const polygon = (
ctx: CanvasRenderingContext2D,
Expand All @@ -10,7 +10,7 @@ export const polygon = (
if (pts.length < 2) return;
__drawPoly(ctx, pts);
ctx.closePath();
endShape(ctx, attribs);
__endShape(ctx, attribs);
};

/**
Expand Down

0 comments on commit 043e3eb

Please sign in to comment.