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
4b17eda1
Commit
4b17eda1
authored
Dec 26, 2021
by
Dj
Browse files
Options
Downloads
Patches
Plain Diff
fix(parallax-css-testpage): Some small performance tweaks.
parent
b001137f
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/parallax-css-testpage/particles.js
+41
-34
41 additions, 34 deletions
src/parallax-css-testpage/particles.js
with
41 additions
and
34 deletions
src/parallax-css-testpage/particles.js
+
41
−
34
View file @
4b17eda1
...
...
@@ -77,13 +77,17 @@ function Particle({ position, direction, size, color }) {
function
Update
(
scene
,
particle
)
{
// Cache object access
// Cache object access
-> Performance
var
width
=
scene
.
width
var
height
=
scene
.
height
var
color
=
particle
.
color
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
particleSizeDirection
=
particleSize
.
direction
var
particlePosition
=
particle
.
position
var
particlePositionX
=
particlePosition
.
x
var
particlePositionY
=
particlePosition
.
y
...
...
@@ -93,12 +97,12 @@ function Update(scene, particle) {
// Animate the pulse effect of the particle
//
var
isMaxSize
=
particleSizeValue
>
=
particleSizeBound
.
m
ax
var
isMinSize
=
particleSizeValue
<
=
particleSizeBound
.
m
in
var
isMaxSize
=
particleSizeValue
>
particleSizeBound
M
ax
var
isMinSize
=
particleSizeValue
<
particleSizeBound
M
in
var
sizeDirection
=
(
isMaxSize
||
isMinSize
)
?
particleSize
.
d
irection
*
-
1
:
particleSize
.
d
irection
?
-
particleSize
D
irection
:
particleSize
D
irection
var
sizeValue
=
particleSizeValue
+
particleSizeStep
*
sizeDirection
...
...
@@ -110,10 +114,10 @@ function Update(scene, particle) {
var
left
=
particlePositionX
-
sizeOffset
var
right
=
particlePositionX
+
sizeOffset
var
isRightEdge
=
(
left
>
width
)
var
isLeftEdge
=
(
right
<
0
)
var
isTopEdge
=
(
bottom
<
0
)
var
isBottomEdge
=
(
top
>
height
)
var
isRightEdge
=
(
left
>
(
width
+
1
)
)
var
isLeftEdge
=
(
right
<
-
1
)
var
isTopEdge
=
(
bottom
<
-
1
)
var
isBottomEdge
=
(
top
>
(
height
+
1
)
)
var
positionX
=
(
isRightEdge
)
...
...
@@ -121,49 +125,52 @@ function Update(scene, particle) {
:
(
isLeftEdge
)
?
width
+
sizeOffset
:
particlePositionX
+
particleDirectionX
var
positionY
=
(
isBottomEdge
)
?
-
sizeOffset
:
(
isTopEdge
)
?
height
+
sizeOffset
:
particlePositionY
+
particleDirectionY
return
Particle
({
return
{
position
:
{
x
:
positionX
,
y
:
positionY
,
},
direction
:
{
x
:
particleDirectionX
,
y
:
particleDirectionY
,
},
direction
:
particleDirection
,
size
:
{
value
:
sizeValue
,
bound
:
particleSizeBound
,
step
:
particleSizeStep
,
direction
:
sizeDirection
},
color
:
particle
.
color
}
)
color
:
color
}
}
function
Draw
(
scene
,
particle
)
{
// Cache object access
var
size
=
particle
.
size
.
value
var
position
=
particle
.
position
var
particlePosition
=
particle
.
position
var
particlePositionX
=
particlePosition
.
x
var
particlePositionY
=
particlePosition
.
y
// Draw calculations
// Draw calculations
--
var
yOffset
=
scene
.
yOffset
*
scene
.
depthScale
var
size
=
particle
.
size
.
value
var
drawOffset
=
size
*
0.5
var
y
=
yOffset
+
(
position
.
y
-
drawOffset
)
var
x
=
particlePositionX
-
drawOffset
var
y
=
yOffset
+
(
particlePositionY
-
drawOffset
)
var
ctx
=
scene
.
ctx
var
isVisible
=
(
size
>
0
)
&&
(
y
<
window
.
innerHeight
)
&&
(
y
>
0
)
// var isVisible = (size > 0 && y < window.innerHeight && y > 0)
var
isVisible
=
(
size
>
0
)
// Draw
if
(
isVisible
)
{
ctx
.
fillStyle
=
particle
.
color
ctx
.
fillRect
(
position
.
x
-
drawOffset
,
y
,
size
,
size
)
// Round to full integer to prevent subpixel rendering
// - The ~~ operator is equivalent to Math.trunc() but faster.
// ctx.fillRect(~~x, ~~y, size, size)
ctx
.
fillRect
(
x
,
y
,
size
,
size
)
}
}
...
...
@@ -201,7 +208,7 @@ function Animate(scene, particles) {
})
// Function to animate the next frame
return
function
()
{
return
function
nextFrame
()
{
return
Animate
(
scene
,
updatedParticles
...
...
@@ -290,7 +297,7 @@ function Particles(configs) {
function
animate
(
rendererList
)
{
if
(
isRunning
)
{
rafId
=
requestAnimationFrame
(
function
()
{
rafId
=
requestAnimationFrame
(
function
doRaf
()
{
animate
(
rendererList
.
map
(
function
doRender
(
render
)
{
return
render
()
...
...
@@ -326,7 +333,7 @@ function Particles(configs) {
init
:
init
,
run
:
run
,
stop
:
stop
,
onScreenSizeChange
:
function
()
{
onScreenSizeChange
:
function
onResize
()
{
stop
()
if
(
!
throttled
)
{
throttled
=
true
...
...
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