fix(Snowflake): Fixed decimal random generation and opacity updates#84
Merged
cahilfoley merged 5 commits intocahilfoley:mainfrom Jul 25, 2025
Merged
fix(Snowflake): Fixed decimal random generation and opacity updates#84cahilfoley merged 5 commits intocahilfoley:mainfrom
cahilfoley merged 5 commits intocahilfoley:mainfrom
Conversation
A side-effect of this change is that random() is now exclusive of the maximum for decimals
There was a problem hiding this comment.
Pull Request Overview
This PR fixes two issues with the snowflake library: incorrect decimal random number generation and opacity range updates not affecting existing snowflakes. The changes ensure decimal random values stay within the intended range and make snowflakes update their opacity when the opacity configuration changes.
- Fixed decimal random number generation to exclude the maximum value instead of including it with an offset
- Added mechanism to update snowflake opacities when opacity range configuration changes
- Updated Zustand import syntax to use named import
Reviewed Changes
Copilot reviewed 5 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| packages/react-snowfall/src/utils.ts | Fixed random number generation logic for decimal values |
| packages/react-snowfall/src/Snowflake.ts | Added opacity update mechanism with hasNextOpacity flag |
| packages/react-snowfall/lib/utils.js | Compiled version of utils.ts changes |
| packages/react-snowfall/lib/Snowflake.js | Compiled version of Snowflake.ts changes with incomplete implementation |
| packages/demo/src/settings.tsx | Updated Zustand import to use named import syntax |
Owner
|
Thank you very much for the contribution! really appreciate it :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #83
This PR adds two related improvements that I discovered while playing around with the
opacityproperty.Fix decimal random number generation.
The current decimal random number generation does not work as intended. The current implementation uses
Math.random() * (max - min + 1) + minto generate a pseudorandom number in the rangemin(inclusive) tomax(inclusive). Most importantly, the+1part is what allows the range to be inclusive of the maximum. However, when returning decimals, this results in the largest outputs being off by up to 1. In other words, when users currently supplyopacity={[0, 0.5]}, this library sets the opacities between 0 and 1.5 (specifically, 1.499 with 9 repeated).You could do some trickery to keep the max inclusive, including replacing the 1 with
0.005orNumbers.EPSILON, but I am not satisfied with such approaches and feel it is simpler to make random() return numbers exclusive of the maximum when dealing with decimals.Update opacities to reflect new range when changed.
Currently, when the opacity range is changed, the snowflakes are not affected. From my understanding, the snowflake image opacity is only set once, upon initialization. Therefore, even if the opacity config is changed, the snowflake opacities only reflect the range that was specified when they were initialized.
I found two approaches to solve this issue. The first, before commit #9cc6669, re-generates all the snowflake opacities once a new opacity range is detected. The second way (current) sets a
hasNextOpacityflag during the opacity config change, and the snowflake's opacity is only re-generated when this flag is set and it is reset to the top of the canvas.The first aoproach is more reactive, while the second one is more gradual and maintains a consistent snowflake opacity while it's visible. You should consider both approaches before settling on a behaviour.
Additionally, I wanted to mention that the codebase needs to be formated with prettier (it is out-of-date), and your eslint is broken because
eslint-config-prettierandeslint-plugin-prettierneed to be updated to latest.Feel free to leave comments for me to make any changes or additions to the code before accepting and merging this PR.
Cheers!