Skip to content

Commit

Permalink
Store Math.PI * 2 as a constant to avoid calculating for every snowflake
Browse files Browse the repository at this point in the history
  • Loading branch information
Tetramputechture authored and cahilfoley committed Sep 6, 2024
1 parent b45bb19 commit a029a5d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/react-snowfall/src/Snowflake.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import isEqual from 'react-fast-compare'
import { lerp, random, randomElement } from './utils.js'
import { lerp, random, randomElement, TWO_PI } from './utils.js'

export interface SnowflakeProps {
/** The color of the snowflake, can be any valid CSS color. */
Expand Down Expand Up @@ -231,7 +231,7 @@ class Snowflake {
} else {
// Not using images so no need to use transforms, just draw an arc in the right location
ctx.beginPath()
ctx.arc(x, y, radius, 0, 2 * Math.PI)
ctx.arc(x, y, radius, 0, TWO_PI)
ctx.fillStyle = this.config.color
ctx.fill()
}
Expand Down
7 changes: 7 additions & 0 deletions packages/react-snowfall/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,10 @@ export function getSize(element?: HTMLElement | null) {
width: element.offsetWidth,
}
}

/**
* Store the value of PI * 2.
*
* This is so we can avoid calculating this value every time we draw a circle.
*/
export const TWO_PI = Math.PI * 2

0 comments on commit a029a5d

Please sign in to comment.