Skip to content

Commit

Permalink
feat(snowflake): update speed interpolation smoothing
Browse files Browse the repository at this point in the history
  • Loading branch information
cahilfoley committed Dec 9, 2018
1 parent 68ff323 commit 816d4d7
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 41 deletions.
4 changes: 0 additions & 4 deletions lib/Snowfall.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ function (_React$Component) {
_this.setState({
height: document.body.clientHeight,
width: document.body.clientWidth
}, function () {
return _this.snowflakes.forEach(function (snowflake) {
return snowflake.resized();
});
});
});

Expand Down
4 changes: 3 additions & 1 deletion lib/Snowflake.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ interface SnowflakeParams {
nextSpeed: number;
nextWind: number;
}
/**
* An individual snowflake that will update it's location every call to `draw`
*/
declare class Snowflake {
config: SnowflakeProps;
params: SnowflakeParams;
framesSinceLastUpdate: number;
constructor(canvas: HTMLCanvasElement, config?: SnowflakeConfig);
updateData: (canvas: HTMLCanvasElement) => void;
resized: () => boolean;
draw: (canvas: HTMLCanvasElement) => void;
translate: () => void;
Expand Down
20 changes: 6 additions & 14 deletions lib/Snowflake.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ var defaultConfig = {
radius: [0.5, 3.0],
speed: [1, 3],
wind: [-0.5, 2],
changeFrequency: 250
changeFrequency: 200
};

/**
* An individual snowflake that will update it's location every call to `draw`
*/
var Snowflake = function Snowflake(_canvas, config) {
var _this = this;

Expand All @@ -40,11 +43,6 @@ var Snowflake = function Snowflake(_canvas, config) {

_defineProperty(this, "framesSinceLastUpdate", void 0);

_defineProperty(this, "updateData", function (canvas) {
_this.params.x = (0, _utils.random)(0, canvas.offsetWidth);
_this.params.y = (0, _utils.random)(-canvas.offsetHeight, 0);
});

_defineProperty(this, "resized", function () {
return _this.params.isResized = true;
});
Expand All @@ -64,8 +62,8 @@ var Snowflake = function Snowflake(_canvas, config) {
_defineProperty(this, "translate", function () {
_this.params.y += _this.params.speed;
_this.params.x += _this.params.wind;
_this.params.speed = (0, _utils.lerp)(_this.params.speed, _this.params.nextSpeed, 0.005);
_this.params.wind = (0, _utils.lerp)(_this.params.wind, _this.params.nextWind, 0.005);
_this.params.speed = (0, _utils.lerp)(_this.params.speed, _this.params.nextSpeed, 0.01);
_this.params.wind = (0, _utils.lerp)(_this.params.wind, _this.params.nextWind, 0.01);

if (_this.framesSinceLastUpdate++ > _this.config.changeFrequency) {
_this.updateTargetParams();
Expand All @@ -87,12 +85,6 @@ var Snowflake = function Snowflake(_canvas, config) {
if (_this.params.y > canvas.offsetHeight) {
_this.params.y = 0;
}

if (_this.params.isResized) {
_this.updateData(canvas);

_this.params.isResized = false;
}
});

_defineProperty(this, "update", function (canvas) {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cahil/snowfall",
"version": "0.1.2",
"version": "0.1.3",
"description": "A react component that creates a snowfall effect",
"main": "./lib/Snowfall.js",
"scripts": {
Expand Down
11 changes: 4 additions & 7 deletions src/Snowfall.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,10 @@ export default class Snowfall extends React.Component<Props, State> {
}

resize = () => {
this.setState(
{
height: document.body.clientHeight,
width: document.body.clientWidth
},
() => this.snowflakes.forEach(snowflake => snowflake.resized())
)
this.setState({
height: document.body.clientHeight,
width: document.body.clientWidth
})
}

get canvas() {
Expand Down
19 changes: 6 additions & 13 deletions src/Snowflake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const defaultConfig: SnowflakeProps = {
radius: [0.5, 3.0],
speed: [1, 3],
wind: [-0.5, 2],
changeFrequency: 250
changeFrequency: 200
}

interface SnowflakeParams {
Expand All @@ -36,6 +36,9 @@ interface SnowflakeParams {
nextWind: number
}

/**
* An individual snowflake that will update it's location every call to `draw`
*/
class Snowflake {
config: SnowflakeProps
params: SnowflakeParams
Expand Down Expand Up @@ -65,11 +68,6 @@ class Snowflake {
this.framesSinceLastUpdate = 0
}

updateData = (canvas: HTMLCanvasElement) => {
this.params.x = random(0, canvas.offsetWidth)
this.params.y = random(-canvas.offsetHeight, 0)
}

resized = () => (this.params.isResized = true)

draw = (canvas: HTMLCanvasElement) => {
Expand All @@ -87,8 +85,8 @@ class Snowflake {
this.params.y += this.params.speed
this.params.x += this.params.wind

this.params.speed = lerp(this.params.speed, this.params.nextSpeed, 0.005)
this.params.wind = lerp(this.params.wind, this.params.nextWind, 0.005)
this.params.speed = lerp(this.params.speed, this.params.nextSpeed, 0.01)
this.params.wind = lerp(this.params.wind, this.params.nextWind, 0.01)

if (this.framesSinceLastUpdate++ > this.config.changeFrequency) {
this.updateTargetParams()
Expand All @@ -109,11 +107,6 @@ class Snowflake {
if (this.params.y > canvas.offsetHeight) {
this.params.y = 0
}

if (this.params.isResized) {
this.updateData(canvas)
this.params.isResized = false
}
}

update = (canvas: HTMLCanvasElement) => {
Expand Down

0 comments on commit 816d4d7

Please sign in to comment.