fix(Snowflake): Fixed decimal random generation and opacity updates #84
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
opacity
property.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) + min
to generate a pseudorandom number in the rangemin
(inclusive) tomax
(inclusive). Most importantly, the+1
part 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.005
orNumbers.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
hasNextOpacity
flag 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-prettier
andeslint-plugin-prettier
need 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!