Skip to content

Commit

Permalink
Added wind
Browse files Browse the repository at this point in the history
  • Loading branch information
pouyamer committed Feb 1, 2024
1 parent fe7ac3e commit 0c9b27a
Show file tree
Hide file tree
Showing 15 changed files with 163 additions and 22 deletions.
4 changes: 3 additions & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
<link rel="stylesheet" href="./style.css" />

<!-- Add these three files to your project -->
<script src="../dist/firefly.js" defer></script>
<script src="../dist//Classes/firefly.js" defer></script>
<script src="../dist//Classes/wind.js" defer></script>
<script src="../dist/Config/windConfig.js" defer></script>
<script src="../dist/Config/boundsConfig.js" defer></script>
<script src="../dist/Config/movementConfig.js" defer></script>
<script src="../dist/Config/startingPositioningConfig.js" defer></script>
Expand Down
34 changes: 31 additions & 3 deletions src/firefly.ts → src/Classes/firefly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ class FireFly {
speedY: 0,
accelerationX: 0,
accelerationY: 0,
quarterCircleCenterLocation: "bottom-left"
quarterCircleCenterLocation: "bottom-left",
wind: new Wind(windConfig),
windResistance: 0,
windAffectStrength: 0
}

this.config = this.initializedConfig
Expand Down Expand Up @@ -117,6 +120,8 @@ class FireFly {
: startingAngle
}

const fireflySize = this.utilGetRandomNumberBetween(size)

this.config = {
x: 0,
y: 0,
Expand Down Expand Up @@ -156,12 +161,15 @@ class FireFly {
),
opacityChangeMode,
// size gets randomized based on your config
size: this.utilGetRandomNumberBetween(size),
size: fireflySize,
speedX: this.utilGetRandomNumberBetween(speedX),
speedY: this.utilGetRandomNumberBetween(speedY),

// Where the center of the quarter circle is located
quarterCircleCenterLocation: getQuarterCircleCenterLocation()
quarterCircleCenterLocation: getQuarterCircleCenterLocation(),
wind: new Wind(windConfig),
windResistance: 0,
windAffectStrength: 0
}

const { x: startingX, y: startingY } = this.getNewPosition(
Expand Down Expand Up @@ -957,6 +965,25 @@ class FireFly {
this.config.rotationSpeed += this.config.rotationAcceleration
}

handleWind = () => {
const { wind, windAffectStrength } = this.config
if (wind) {
this.config.windAffectStrength =
wind.windConfig.calculateWindAffectionFunction(
wind.config.sourceX,
wind.config.sourceY,
this.config.x,
this.config.y,
this.canvasSize.width,
this.canvasSize.height
)
this.config.x +=
(windAffectStrength - this.config.windResistance) * wind.config.speedX
this.config.y +=
(windAffectStrength - this.config.windResistance) * wind.config.speedY
}
}

update(ctx: CanvasRenderingContext2D, hueShiftAmount: number) {
this.draw(ctx, hueShiftAmount)
this.handleRotation()
Expand All @@ -965,6 +992,7 @@ class FireFly {
// handle Opacity change
if (this.config.opacityChangeMode === "fade") this.handleFade()
else this.handleGlow()
this.handleWind()

this.handleMove()
this.handleAcceleration()
Expand Down
57 changes: 57 additions & 0 deletions src/Classes/wind.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class Wind {
windConfig: WindConfigType
config
constructor(windConfig: WindConfigType) {
this.windConfig = windConfig
this.config = {
speedX: this.utilGetRandomNumberBetween(windConfig.speedX),
speedY: this.utilGetRandomNumberBetween(windConfig.speedY),
sourceX: this.utilGetRandomNumberBetween({
min: 0,
max: config.canvasSize.width
}),
sourceY: this.utilGetRandomNumberBetween({
min: 0,
max: config.canvasSize.height
})
}

this.updateWind()
}

utilGetRandomNumberBetween = (
range: RangeType,
getAsInteger: boolean = false
) => {
const { min, max } = range
return getAsInteger
? Math.floor(Math.random() * (max - min + 1)) + min
: Math.random() * (max - min) + min
}

affectFirefly(firefly: FireFly, strength: number = 1) {
firefly.config.wind = this
firefly.config.windAffectStrength = strength
}

updateWind() {
setInterval(() => {
this.config.sourceX = this.utilGetRandomNumberBetween({
min: 0,
max: config.canvasSize.width
})

this.config.sourceY = this.utilGetRandomNumberBetween({
min: 0,
max: config.canvasSize.height
})

this.config.speedX = this.utilGetRandomNumberBetween(
this.windConfig.speedX
)
this.config.speedY = this.utilGetRandomNumberBetween(
this.windConfig.speedY
)
}, this.windConfig.speedResetInterval)
}
}
4 changes: 2 additions & 2 deletions src/Config/boundsConfig.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const boundsConfig: BoundsConfigType = {
toggleBounds: {
top: true,
top: false,
right: false,
bottom: true,
left: false
},
afterImpactSpeedMultiplier: {
top: 1,
right: 1,
bottom: 1,
bottom: 0.75,
left: 1
},
hueIncreaseAmountAfterImpact: {
Expand Down
8 changes: 4 additions & 4 deletions src/Config/fadeConfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fadeConfig: OpacityChangeConfigType = {
rate: {
min: 0,
max: 0
min: 0.001,
max: 0.009
},

// TODO: change this to new opacity after fade (and grow)
Expand All @@ -14,12 +14,12 @@ const fadeConfig: OpacityChangeConfigType = {
x: 0,
y: 0
},
positioningMethod: "randomX",
positioningMethod: "random",
resetColorAfterOpacityChange: true,
resetRateAfterOpacityChange: true,
resetSizeAfterOpacityChange: true,
sizeChange: {
mode: "none",
mode: "grow",
frequency: 1
}
}
4 changes: 2 additions & 2 deletions src/Config/glowConfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const glowConfig: OpacityChangeConfigType = {
rate: {
min: 0.003,
max: 0.012
max: 0.009
},

opacityAfterOpacityChange: {
Expand All @@ -17,7 +17,7 @@ const glowConfig: OpacityChangeConfigType = {
resetRateAfterOpacityChange: true,
resetSizeAfterOpacityChange: false,
sizeChange: {
mode: "grow",
mode: "none",
frequency: 1
}
}
6 changes: 3 additions & 3 deletions src/Config/movementConfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const movementConfig: MovementConfigType = {
speedX: {
min: 1,
max: 5
min: 0,
max: 0
},
speedY: {
min: 0,
Expand All @@ -14,7 +14,7 @@ const movementConfig: MovementConfigType = {
},
accelerationY: {
min: 0,
max: 0
max: 0.4
},
accelerateInCurrentMovingDirection: false,

Expand Down
2 changes: 1 addition & 1 deletion src/Config/opacityChangeOptionsConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const opacityChangeOptionsConfig: OpacityChangeOptionsConfigType = {
fadeRatio: 1,
fadeRatio: 0,

fade: fadeConfig,
glow: glowConfig
Expand Down
2 changes: 1 addition & 1 deletion src/Config/shapingConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const shapingConfig: ShapingConfigType = {
shape: "regularPolygon",
shape: "circle",
regularPolygon: {
sideCount: {
min: 3,
Expand Down
23 changes: 23 additions & 0 deletions src/Config/windConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const windConfig: WindConfigType = {
speedX: {
min: -10,
max: 10
},
speedY: {
min: -10,
max: 10
},
speedResetInterval: 3000,
calculateWindAffectionFunction: (
sourceX: number,
sourceY: number,
x: number,
y: number,
width: number,
height: number
) => {
return (
(1 - Math.abs(x - sourceX) / width) * (1 - Math.abs(y - sourceY) / height)
)
}
}
2 changes: 2 additions & 0 deletions src/Interfaces/IConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ interface IConfig {

rainbowMode: boolean // true = rainbow mode, false = normal mode
skyColor: IHSLColor

wind: WindConfigType
fireflies: {
// Number of fireflies
count: number
Expand Down
9 changes: 9 additions & 0 deletions src/Interfaces/ISingleFireflyConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,13 @@ interface ISingleFireflyConfig {
jitterY: number

quarterCircleCenterLocation: TwoDimentionalDirectionType

// wind properties will be set once wind object is created
wind: Wind

// how much firefly resists wind [0-1]
windResistance: number

// how much firefly is affected by wind [0-1]
windAffectStrength: number
}
13 changes: 13 additions & 0 deletions src/Types/WindConfigType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type WindConfigType = {
speedX: RangeType
speedY: RangeType
speedResetInterval: number
calculateWindAffectionFunction: (
sourceX: number,
sourceY: number,
x: number,
y: number,
width: number,
height: number
) => number
}
8 changes: 5 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ const config: IConfig = {
l: 0,
a: 1
},
wind: windConfig,

fireflies: {
count: 3000,
count: 500,

size: {
min: 4,
max: 50
min: 1,
max: 30
},

movement: movementConfig,
Expand Down
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,13 @@ const addCanvas = (
}

let fireflies: FireFly[] = []
const wind = new Wind(finalConfig.wind)

// creating fireflies based on finalConfig
for (let i = 0; i < finalConfig.fireflies.count; i++) {
fireflies.push(new FireFly(finalConfig))
const newFirefly = new FireFly(finalConfig)
wind.affectFirefly(newFirefly, newFirefly.config.x / canvasSize.width)
fireflies.push(newFirefly)
}

// ============== TEST ============*/
Expand Down Expand Up @@ -170,7 +173,9 @@ const addCanvas = (
ctx.fillRect(0, 0, canvasSize.width, canvasSize.height)

// Fireflies
fireflies.forEach(firefly => firefly.update(ctx, hueShiftAmount))
for (let i = 0; i < fireflies.length; i++) {
fireflies[i].update(ctx, hueShiftAmount)
}

// requestAnimationFrame causes the browser to call the function again and again
requestAnimationFrame(render)
Expand Down

0 comments on commit 0c9b27a

Please sign in to comment.