Skip to content

Commit

Permalink
feat: update snowflake location based on time between frames
Browse files Browse the repository at this point in the history
  • Loading branch information
cahilfoley committed Dec 11, 2018
1 parent 62391be commit 49c2916
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 19 deletions.
3 changes: 2 additions & 1 deletion lib/Snowfall.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 19 additions & 4 deletions lib/Snowfall.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/Snowflake.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions lib/Snowflake.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 18 additions & 3 deletions src/Snowfall.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ export interface State {
const requestAnimationFrame =
window.requestAnimationFrame || window.webkitRequestAnimationFrame

// Target of 60 frames per second
const targetFrameTime = 1000 / 60

export default class Snowfall extends React.Component<Props, State> {
canvasRef: React.RefObject<HTMLCanvasElement>
snowflakes: Array<Snowflake> = []
snowflakeCount: number
snowflakeConfig: SnowflakeConfig
lastUpdate: number

state = {
width: document.body.clientWidth,
Expand All @@ -30,6 +34,7 @@ export default class Snowfall extends React.Component<Props, State> {
super(props)
this.snowflakeCount = props.snowflakeCount || 150
this.snowflakeConfig = {}
this.lastUpdate = Date.now()
if (props.color) {
this.snowflakeConfig.color = props.color
}
Expand Down Expand Up @@ -77,17 +82,27 @@ export default class Snowfall extends React.Component<Props, State> {
}
}

update = () => {
update = (framesPassed: number = 1) => {
const canvas = this.canvas
if (canvas) {
this.snowflakes.forEach(snowflake => snowflake.update(canvas))
this.snowflakes.forEach(snowflake =>
snowflake.update(canvas, framesPassed)
)
}
}

loop = () => {
if (this) {
// Update based on time passed so that a slow frame rate won't slow down the snowflake
const now = Date.now()
const msPassed = Date.now() - this.lastUpdate
this.lastUpdate = now

// Frames that would have passed if running at 60 fps
const framesPassed = msPassed / targetFrameTime

this.update(framesPassed)
this.draw()
this.update()
requestAnimationFrame(this.loop)
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/Snowflake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ class Snowflake {
}
}

translate = () => {
this.params.y += this.params.speed
this.params.x += this.params.wind
translate = (framesPassed: number = 1) => {
this.params.y += this.params.speed * framesPassed
this.params.x += this.params.wind * framesPassed

this.params.speed = lerp(this.params.speed, this.params.nextSpeed, 0.01)
this.params.wind = lerp(this.params.wind, this.params.nextWind, 0.01)
Expand All @@ -109,8 +109,8 @@ class Snowflake {
}
}

update = (canvas: HTMLCanvasElement) => {
this.translate()
update = (canvas: HTMLCanvasElement, framesPassed?: number) => {
this.translate(framesPassed)
this.handleOffScreen(canvas)
}
}
Expand Down

0 comments on commit 49c2916

Please sign in to comment.