Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hub
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Package registry
Operate
Terraform modules
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
thomasDOTwtf
hub
Commits
db692267
Commit
db692267
authored
Dec 26, 2021
by
Dj
Browse files
Options
Downloads
Patches
Plain Diff
feat(parallax-css-testpage): Performance improvement (memoize) and add easing functions.
parent
f69dd5ae
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/parallax-css-testpage/particles.js
+70
-49
70 additions, 49 deletions
src/parallax-css-testpage/particles.js
src/parallax-css-testpage/template.html
+5
-5
5 additions, 5 deletions
src/parallax-css-testpage/template.html
with
75 additions
and
54 deletions
src/parallax-css-testpage/particles.js
+
70
−
49
View file @
db692267
...
@@ -165,12 +165,18 @@ return exports
...
@@ -165,12 +165,18 @@ return exports
// Create the Particles Module | Namespace.
// Create the Particles Module | Namespace.
var
Particles
=
(
function
()
{
var
Particles
=
(
function
()
{
//
//
// Helper
// Helper
//
//
var
MathRandom
=
Math
.
random
var
MathAbs
=
Math
.
abs
var
MathTrunc
=
Math
.
trunc
var
MathPow
=
Math
.
pow
function
randomValue
(
min
,
max
)
{
function
randomValue
(
min
,
max
)
{
var
value
=
(
Math
.
r
andom
()
*
(
+
max
-
+
min
)
+
+
min
)
var
value
=
(
Math
R
andom
()
*
(
+
max
-
+
min
)
+
+
min
)
// Prevent values that can be possible boring
// Prevent values that can be possible boring
return
(
min
!==
0
&&
max
!==
0
&&
(
value
===
0
||
value
===
1
))
return
(
min
!==
0
&&
max
!==
0
&&
(
value
===
0
||
value
===
1
))
?
randomValue
(
min
,
max
)
?
randomValue
(
min
,
max
)
...
@@ -192,35 +198,40 @@ function calcParticleCount(scene, particleDensity) {
...
@@ -192,35 +198,40 @@ function calcParticleCount(scene, particleDensity) {
var
sceneVolume
=
scene
.
width
*
scene
.
height
var
sceneVolume
=
scene
.
width
*
scene
.
height
var
scale
=
100
var
scale
=
100
var
particleFactor
=
(
sceneVolume
/
referenceArea
)
/
scale
var
particleFactor
=
(
sceneVolume
/
referenceArea
)
/
scale
var
particleCount
=
Math
.
a
bs
(
Math
.
t
runc
(
var
particleCount
=
Math
A
bs
(
Math
T
runc
(
particleFactor
*
particleDensity
particleFactor
*
particleDensity
))
))
return
particleCount
||
0
return
particleCount
||
0
}
}
function
normalize
(
val
)
{
var
normalize
=
Memoize
(
function
doNormalize
(
val
)
{
var
min
=
0
var
min
=
0
var
max
=
1
var
max
=
1
var
delta
=
max
-
min
var
delta
=
max
-
min
return
(
val
-
min
)
/
delta
return
(
val
-
min
)
/
delta
}
}
)
// Easing functions from: https://easings.net/
// Easing functions from: https://easings.net/
function
easeOutQuint
(
x
)
{
var
easeOutQuint
=
Memoize
(
return
1
-
Math
.
pow
(
1
-
x
,
5
);
function
doEaseOutQuint
(
x
)
{
}
return
1
-
MathPow
(
1
-
x
,
5
);
function
easeInQuint
(
x
)
{
return
x
*
x
*
x
*
x
*
x
;
}
}
)
var
easeOutExpo
=
Memoize
(
function
easeOutExpo
(
x
)
{
function
easeOutExpo
(
x
)
{
return
x
==
=
1
?
1
:
1
-
Math
.
p
ow
(
2
,
-
10
*
x
);
return
x
>
=
1
?
1
:
1
-
Math
P
ow
(
2
,
-
10
*
x
);
}
}
)
function
easeInExpo
(
x
)
{
function
easing
(
value
)
{
return
x
===
0
?
0
:
Math
.
pow
(
2
,
10
*
x
-
10
);
return
(
value
<
0
)
?
-
easeOutExpo
(
-
value
)
:
easeOutExpo
(
value
)
}
}
...
@@ -237,10 +248,6 @@ function MinMax(min, max) {
...
@@ -237,10 +248,6 @@ function MinMax(min, max) {
return
{
min
,
max
}
return
{
min
,
max
}
}
}
function
MaxValue
(
value
,
max
)
{
return
{
value
,
max
}
}
function
RandomPoint2D
(
minMaxX
,
minMaxY
)
{
function
RandomPoint2D
(
minMaxX
,
minMaxY
)
{
var
x
=
randomValue
(
minMaxX
.
min
,
minMaxX
.
max
)
var
x
=
randomValue
(
minMaxX
.
min
,
minMaxX
.
max
)
var
y
=
randomValue
(
minMaxY
.
min
,
minMaxY
.
max
)
var
y
=
randomValue
(
minMaxY
.
min
,
minMaxY
.
max
)
...
@@ -259,11 +266,9 @@ function Update(scene, particle) {
...
@@ -259,11 +266,9 @@ function Update(scene, particle) {
var
height
=
scene
.
height
var
height
=
scene
.
height
var
color
=
particle
.
color
var
color
=
particle
.
color
var
particleSize
=
particle
.
size
var
particleSize
=
particle
.
size
var
particleSizeValue
=
particleSize
.
value
var
particleSizeBound
=
particleSize
.
bound
var
particleSizeBoundMin
=
particleSizeBound
.
min
var
particleSizeBoundMax
=
particleSizeBound
.
max
var
particleSizeStep
=
particleSize
.
step
var
particleSizeStep
=
particleSize
.
step
var
particleSizeAnimValue
=
particleSize
.
animValue
var
particleSizeScale
=
particleSize
.
scale
var
particleSizeDirection
=
particleSize
.
direction
var
particleSizeDirection
=
particleSize
.
direction
var
particlePosition
=
particle
.
position
var
particlePosition
=
particle
.
position
var
particlePositionX
=
particlePosition
.
x
var
particlePositionX
=
particlePosition
.
x
...
@@ -274,14 +279,18 @@ function Update(scene, particle) {
...
@@ -274,14 +279,18 @@ function Update(scene, particle) {
// Animate the pulse effect of the particle
// Animate the pulse effect of the particle
//
//
var
isMaxSize
=
particleSizeValue
>
particleSizeBoundMax
// var absSizeAnimValue = particleSizeAnimValue * particleSizeDirection
var
isMinSize
=
particleSizeValue
<
particleSizeBoundMin
var
isMaxSize
=
particleSizeAnimValue
>
1
var
isMinSize
=
particleSizeAnimValue
<
-
1
var
sizeDirection
=
var
sizeDirection
=
(
isMaxSize
||
isMinSize
)
(
isMaxSize
||
isMinSize
)
?
-
particleSizeDirection
?
-
particleSizeDirection
:
particleSizeDirection
:
particleSizeDirection
var
sizeValue
=
var
animValue
=
particleSizeAnimValue
+
(
particleSizeStep
*
sizeDirection
)
particleSizeValue
+
particleSizeStep
*
sizeDirection
// The operator ~~ is a more performant shorthand for Math.trunc()
var
sizeValue
=
~~
(
easing
(
animValue
)
*
particleSizeScale
)
// Animate the movement of the particle
// Animate the movement of the particle
//
//
...
@@ -316,9 +325,11 @@ function Update(scene, particle) {
...
@@ -316,9 +325,11 @@ function Update(scene, particle) {
direction
:
particleDirection
,
direction
:
particleDirection
,
size
:
{
size
:
{
value
:
sizeValue
,
value
:
sizeValue
,
bound
:
particleSizeBound
,
animValue
:
animValue
,
step
:
particleSizeStep
,
step
:
particleSizeStep
,
direction
:
sizeDirection
scale
:
particleSizeScale
,
direction
:
sizeDirection
,
offset
:
sizeOffset
},
},
color
:
color
color
:
color
}
}
...
@@ -329,11 +340,13 @@ function Draw(scene, particle) {
...
@@ -329,11 +340,13 @@ function Draw(scene, particle) {
var
particlePosition
=
particle
.
position
var
particlePosition
=
particle
.
position
var
particlePositionX
=
particlePosition
.
x
var
particlePositionX
=
particlePosition
.
x
var
particlePositionY
=
particlePosition
.
y
var
particlePositionY
=
particlePosition
.
y
var
particleSize
=
particle
.
size
var
particleSizeValue
=
particleSize
.
value
var
drawOffset
=
particleSize
.
offset
// Draw calculations --
// Draw calculations --
var
yOffset
=
scene
.
yOffset
*
scene
.
depthScale
var
yOffset
=
scene
.
yOffset
*
scene
.
depthScale
var
size
=
particle
.
size
.
value
var
size
=
particleSizeValue
var
drawOffset
=
size
*
0.5
var
x
=
particlePositionX
-
drawOffset
var
x
=
particlePositionX
-
drawOffset
var
y
=
yOffset
+
(
particlePositionY
-
drawOffset
)
var
y
=
yOffset
+
(
particlePositionY
-
drawOffset
)
...
@@ -412,15 +425,15 @@ function CreateScene(cfg) {
...
@@ -412,15 +425,15 @@ function CreateScene(cfg) {
ctx
.
canvas
.
height
=
window
.
innerHeight
ctx
.
canvas
.
height
=
window
.
innerHeight
return
{
return
{
width
,
width
:
width
,
height
:
depthScaledHeight
,
height
:
depthScaledHeight
,
yOffset
,
yOffset
:
yOffset
,
depthScale
:
depth
,
depthScale
:
depth
,
ctx
,
fps
:
fps
,
fps
,
fpsInterval
:
fpsInterval
,
fpsInterval
,
then
:
performance
.
now
(),
then
:
performance
.
now
(),
anchorNode
:
contentNode
anchorNode
:
contentNode
,
ctx
:
ctx
,
}
}
}
}
...
@@ -432,8 +445,11 @@ function CreateParticles(scene, cfg) {
...
@@ -432,8 +445,11 @@ function CreateParticles(scene, cfg) {
speed
=
cfg
.
speed
,
speed
=
cfg
.
speed
,
lifespan
=
cfg
.
lifespan
lifespan
=
cfg
.
lifespan
var
initAnimValue
=
randomValue
(
-
1
,
1
)
return
Array
.
from
(
return
Array
.
from
(
{
{
// length: 1
length
:
calcParticleCount
(
scene
,
density
)
length
:
calcParticleCount
(
scene
,
density
)
},
},
function
Particle
()
{
function
Particle
()
{
...
@@ -449,15 +465,20 @@ function CreateParticles(scene, cfg) {
...
@@ -449,15 +465,20 @@ function CreateParticles(scene, cfg) {
MinMax
(
-
speed
,
speed
)
MinMax
(
-
speed
,
speed
)
),
),
size
:
{
size
:
{
value
:
randomValue
(
-
size
,
size
),
value
:
size
*
initAnimValue
,
bound
:
MinMax
(
-
size
,
size
),
animValue
:
initAnimValue
,
step
:
size
/
((
lifespan
)
/
scene
.
fps
),
scale
:
size
,
step
:
1
/
((
lifespan
)
/
scene
.
fps
),
direction
:
direction
:
// 50% Chance for the particle floating in one
// 50% Chance for the particle floating in one
// direction or the other.
// direction or the other.
(
randomValue
(
0
,
100
)
<
50
)
(
randomValue
(
0
,
100
)
<
50
)
?
-
1
?
-
1
:
+
1
,
:
+
1
,
// Offset is needed for boundary calculation and for
// drawing the pixels. We cache it here, to prevent
// double calculation of the same value.
offset
:
size
*
initAnimValue
*
0.5
},
},
color
color
}
}
...
...
This diff is collapsed.
Click to expand it.
src/parallax-css-testpage/template.html
+
5
−
5
View file @
db692267
...
@@ -95,11 +95,11 @@
...
@@ -95,11 +95,11 @@
depth
:
2.5
,
depth
:
2.5
,
},
},
particles
:
{
particles
:
{
density
:
1
5
,
density
:
5
,
color
:
'
white
'
,
color
:
'
white
'
,
size
:
40
,
size
:
40
,
speed
:
0.
5
,
speed
:
0.
7
,
lifespan
:
12
000
// in milliseconds
lifespan
:
4
000
// in milliseconds
}
}
}
}
var
bgParticleConfig
=
{
var
bgParticleConfig
=
{
...
@@ -116,9 +116,9 @@
...
@@ -116,9 +116,9 @@
particles
:
{
particles
:
{
density
:
100
,
density
:
100
,
color
:
'
white
'
,
color
:
'
white
'
,
size
:
7
,
size
:
5
,
speed
:
0.2
,
speed
:
0.2
,
lifespan
:
8
000
// in milliseconds
lifespan
:
4
000
// in milliseconds
}
}
}
}
var
animation
=
Particles
([
var
animation
=
Particles
([
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment