Skip to content

Commit

Permalink
chore: optimize draw func
Browse files Browse the repository at this point in the history
  • Loading branch information
peterklingelhofer committed Mar 12, 2023
1 parent 6aa9839 commit c8096ec
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,31 @@ function progressState(state: State): State {
}

function draw(): void {
let elapsed = 0;
ctx.fillStyle = BACKDROP_COLOR;
ctx.fillRect(0, 0, canvasWidth, canvasHeight);

switch (state) {
case State.INHALE:
elapsed = calculateElapsed(frameCount);
radius = Math.min(
map(elapsed, 0, durationInhale, 0, halfCanvasHeight),
map(
calculateElapsed(frameCount),
0,
durationInhale,
0,
halfCanvasHeight
),
halfCanvasHeight
);
break;
case State.EXHALE:
elapsed = calculateElapsed(frameCount);
radius = Math.max(
map(elapsed, 0, durationExhale, halfCanvasHeight, 0),
map(
calculateElapsed(frameCount),
0,
durationExhale,
halfCanvasHeight,
0
),
0
);
break;
Expand All @@ -119,8 +128,12 @@ function draw(): void {
}

const twiceRadius = radius * 2;
ctx.fillStyle = color;

if (color !== ctx.fillStyle) {
ctx.fillStyle = color;
}
ctx.fillRect(0, canvasHeight - twiceRadius, canvasWidth, twiceRadius);

if (frameCount >= endFrame) {
startFrame = frameCount;
state = progressState(state);
Expand Down

0 comments on commit c8096ec

Please sign in to comment.