-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d9d3ed1
commit 77f30fe
Showing
5 changed files
with
103 additions
and
101 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,83 @@ | ||
const bgCanvas = document.getElementById("bg"); | ||
const bgContext = bgCanvas.getContext("2d"); | ||
{ | ||
const bgCanvas = document.getElementById("bg"); | ||
const bgContext = bgCanvas.getContext("2d"); | ||
|
||
let bgEnabled = localStorage.getItem('bgEnabled'); | ||
if(bgEnabled === null) { | ||
bgEnabled = "true"; | ||
localStorage.setItem('bgEnabled', bgEnabled); | ||
} | ||
bgEnabled = bgEnabled === "true"; | ||
|
||
const bgToggle = document.getElementById("bg-toggle"); | ||
if(bgToggle !== null) { | ||
bgToggle.checked = bgEnabled; | ||
bgToggle.addEventListener('change', () => { | ||
bgEnabled = bgToggle.checked; | ||
let bgEnabled = localStorage.getItem('bgEnabled'); | ||
if(bgEnabled === null) { | ||
bgEnabled = "true"; | ||
localStorage.setItem('bgEnabled', bgEnabled); | ||
}); | ||
} | ||
} | ||
bgEnabled = bgEnabled === "true"; | ||
|
||
let width = bgCanvas.offsetWidth; | ||
let height = bgCanvas.offsetHeight; | ||
const bgToggle = document.getElementById("bg-toggle"); | ||
if(bgToggle !== null) { | ||
bgToggle.checked = bgEnabled; | ||
bgToggle.addEventListener('change', () => { | ||
bgEnabled = bgToggle.checked; | ||
localStorage.setItem('bgEnabled', bgEnabled); | ||
}); | ||
} | ||
|
||
let lastTimestamp = performance.now(); | ||
let stars = []; | ||
let width = bgCanvas.offsetWidth; | ||
let height = bgCanvas.offsetHeight; | ||
|
||
function random(min, max){ return Math.random() * (min - max) + max } | ||
let lastTimestamp = performance.now(); | ||
let stars = []; | ||
|
||
function resize(){ | ||
width = bgCanvas.offsetWidth; | ||
height = bgCanvas.offsetHeight; | ||
function random(min, max){ return Math.random() * (min - max) + max } | ||
|
||
bgCanvas.width = width; | ||
bgCanvas.height = height; | ||
function resize(){ | ||
width = bgCanvas.offsetWidth; | ||
height = bgCanvas.offsetHeight; | ||
|
||
let starCount = Math.floor(width * height * 6e-4) | ||
stars = new Array(starCount); | ||
bgCanvas.width = width; | ||
bgCanvas.height = height; | ||
|
||
for (let i = 0; i < starCount; i++) { | ||
let size = random(2.6, 4) | ||
stars[i] = { | ||
size: size, | ||
x: random(-10, width + 10), | ||
y: random(-10, height + 10), | ||
dx: random(-0.001, 0.001) * size, | ||
dy: random(-0.001, 0.001) * size | ||
let starCount = Math.floor(width * height * 6e-4) | ||
stars = new Array(starCount); | ||
|
||
for (let i = 0; i < starCount; i++) { | ||
let size = random(2.6, 4) | ||
stars[i] = { | ||
size: size, | ||
x: random(-10, width + 10), | ||
y: random(-10, height + 10), | ||
dx: random(-0.001, 0.001) * size, | ||
dy: random(-0.001, 0.001) * size | ||
} | ||
} | ||
} | ||
} | ||
|
||
function draw(timestamp){ | ||
let dt = timestamp - lastTimestamp; | ||
lastTimestamp = timestamp; | ||
function draw(timestamp){ | ||
let dt = timestamp - lastTimestamp; | ||
lastTimestamp = timestamp; | ||
|
||
if(width !== bgCanvas.offsetWidth || height !== bgCanvas.offsetHeight) resize(); | ||
if(width !== bgCanvas.offsetWidth || height !== bgCanvas.offsetHeight) resize(); | ||
|
||
bgContext.clearRect(0, 0, width, height); | ||
bgContext.clearRect(0, 0, width, height); | ||
|
||
if(!bgEnabled) { | ||
return requestAnimationFrame(draw); | ||
} | ||
|
||
// move stars | ||
for (let i = 0; i < stars.length; i++) { | ||
const star = stars[i]; | ||
star.x += (star.dx * dt) | ||
star.y += (star.dy * dt) | ||
star.x %= width + 10 | ||
star.y %= height + 10 | ||
} | ||
if(!bgEnabled) { | ||
return requestAnimationFrame(draw); | ||
} | ||
// move stars | ||
for (let i = 0; i < stars.length; i++) { | ||
const star = stars[i]; | ||
star.x += (star.dx * dt) | ||
star.y += (star.dy * dt) | ||
star.x %= width + 10 | ||
star.y %= height + 10 | ||
} | ||
|
||
// draw stars | ||
bgContext.fillStyle = "#FFFFFFAF" | ||
for (let i = 0; i < stars.length; i++) { | ||
const star = stars[i]; | ||
bgContext.fillRect(star.x, star.y, star.size, star.size); | ||
// draw stars | ||
bgContext.fillStyle = "#FFFFFFAF" | ||
for (let i = 0; i < stars.length; i++) { | ||
const star = stars[i]; | ||
bgContext.fillRect(star.x, star.y, star.size, star.size); | ||
} | ||
requestAnimationFrame(draw); | ||
} | ||
requestAnimationFrame(draw); | ||
} | ||
|
||
resize(); | ||
draw(performance.now()); | ||
resize(); | ||
draw(performance.now()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters