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

fix(Snowflake): Fixed decimal random generation and opacity updates #84

Open
wants to merge 4 commits into
base: main
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
2 changes: 1 addition & 1 deletion packages/demo/src/settings.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SnowfallProps } from 'react-snowfall'
import create from 'zustand'
import { create } from 'zustand'

export interface SnowfallSettings extends SnowfallProps {
update: (settings: Partial<SnowfallProps>) => void
Expand Down
4 changes: 4 additions & 0 deletions packages/react-snowfall/lib/Snowflake.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export interface SnowflakeProps {
* The minimum and maximum opacity of the snowflake image.
*
* This value only applies to snowflakes that are using images.
*
* The values will be randomly selected within this range.
*
* The default value is `[1, 1]`.
*/
opacity: [number, number];
}
Expand Down
8 changes: 7 additions & 1 deletion packages/react-snowfall/lib/Snowflake.js

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

2 changes: 1 addition & 1 deletion packages/react-snowfall/lib/Snowflake.js.map

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

5 changes: 2 additions & 3 deletions packages/react-snowfall/lib/utils.js

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

2 changes: 1 addition & 1 deletion packages/react-snowfall/lib/utils.js.map

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

14 changes: 13 additions & 1 deletion packages/react-snowfall/src/Snowflake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ interface SnowflakeParams {
nextWind: number
nextRotationSpeed: number
opacity: number
hasNextOpacity: boolean
}

/**
Expand Down Expand Up @@ -144,6 +145,7 @@ class Snowflake {
nextWind: random(...wind),
nextRotationSpeed: random(...rotationSpeed),
opacity: random(...opacity),
hasNextOpacity: false,
}

this.framesSinceLastUpdate = 0
Expand All @@ -170,6 +172,10 @@ class Snowflake {
if (!isEqual(this.config.images, previousConfig?.images)) {
this.selectImage()
}

if (previousConfig?.opacity && !isEqual(this.config.opacity, previousConfig?.opacity)) {
this.params.hasNextOpacity = true
}
}

private updateTargetParams(): void {
Expand All @@ -187,7 +193,13 @@ class Snowflake {
this.params.x = (x + wind * framesPassed) % (offsetWidth + radius * 2)
if (this.params.x > offsetWidth + radius) this.params.x = -radius
this.params.y = (y + speed * framesPassed) % (offsetHeight + radius * 2)
if (this.params.y > offsetHeight + radius) this.params.y = -radius
if (this.params.y > offsetHeight + radius) {
if (this.params.hasNextOpacity) {
this.params.opacity = random(...this.config.opacity)
this.params.hasNextOpacity = false
}
this.params.y = -radius
}

// Apply rotation
if (this.image) {
Expand Down
6 changes: 2 additions & 4 deletions packages/react-snowfall/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
* @param max The maximum value
*/
export function random(min: number, max: number): number {
const randomNumber = Math.random() * (max - min + 1) + min

if (!Number.isInteger(min) || !Number.isInteger(max)) {
return randomNumber
return Math.random() * (max - min) + min
} else {
return Math.floor(randomNumber)
return Math.floor(Math.random() * (max - min + 1) + min)
}
}

Expand Down