2023-09-08 18:05:51 -06:00
|
|
|
function randint(max) {
|
|
|
|
return Math.floor(Math.random() * max)
|
|
|
|
}
|
|
|
|
|
2023-09-13 18:52:52 -06:00
|
|
|
const Millisecond = 1
|
|
|
|
const Second = Millisecond * 1000
|
|
|
|
const FrameRate = 24 / Second // Fast enough for this tomfoolery
|
2023-09-08 18:05:51 -06:00
|
|
|
|
2023-09-11 17:29:14 -06:00
|
|
|
class Point {
|
|
|
|
constructor(x, y) {
|
|
|
|
this.x = x
|
|
|
|
this.y = y
|
|
|
|
}
|
|
|
|
|
2023-09-08 18:05:51 -06:00
|
|
|
/**
|
2023-09-11 17:29:14 -06:00
|
|
|
* Add n to this.
|
|
|
|
*
|
|
|
|
* @param {Point} n What to add to this
|
|
|
|
* @returns {Point}
|
2023-09-08 18:05:51 -06:00
|
|
|
*/
|
2023-09-11 17:29:14 -06:00
|
|
|
Add(n) {
|
|
|
|
return new Point(this.x + n.x, this.y + n.y)
|
2023-09-08 18:05:51 -06:00
|
|
|
}
|
|
|
|
|
2023-09-11 17:29:14 -06:00
|
|
|
/**
|
|
|
|
* Subtract n from this.
|
|
|
|
*
|
|
|
|
* @param {Point} n
|
|
|
|
* @returns {Point}
|
|
|
|
*/
|
|
|
|
Subtract(n) {
|
|
|
|
return new Point(this.x - n.x, this.y - n.y)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add velocity, then bounce point off box defined by points at min and max
|
|
|
|
* @param {Point} velocity
|
|
|
|
* @param {Point} min
|
|
|
|
* @param {Point} max
|
|
|
|
* @returns {Point}
|
|
|
|
*/
|
|
|
|
Bounce(velocity, min, max) {
|
|
|
|
let p = this.Add(velocity)
|
|
|
|
if (p.x < min.x) {
|
|
|
|
p.x += (min.x - p.x) * 2
|
|
|
|
velocity.x *= -1
|
2023-09-08 18:05:51 -06:00
|
|
|
}
|
2023-09-11 17:29:14 -06:00
|
|
|
if (p.x > max.x) {
|
|
|
|
p.x += (max.x - p.x) * 2
|
|
|
|
velocity.x *= -1
|
2023-09-08 18:05:51 -06:00
|
|
|
}
|
2023-09-11 17:29:14 -06:00
|
|
|
if (p.y < min.y) {
|
|
|
|
p.y += (min.y - p.y) * 2
|
|
|
|
velocity.y *= -1
|
|
|
|
}
|
|
|
|
if (p.y > max.y) {
|
|
|
|
p.y += (max.y - p.y) * 2
|
|
|
|
velocity.y *= -1
|
|
|
|
}
|
|
|
|
return p
|
2023-09-08 18:05:51 -06:00
|
|
|
}
|
|
|
|
|
2023-09-11 17:29:14 -06:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {Point} p
|
|
|
|
* @returns {Boolean}
|
|
|
|
*/
|
|
|
|
Equal(p) {
|
|
|
|
return (this.x == p.x) && (this.y == p.y)
|
2023-09-08 18:05:51 -06:00
|
|
|
}
|
2023-09-11 17:29:14 -06:00
|
|
|
}
|
2023-09-08 18:05:51 -06:00
|
|
|
|
2023-09-11 17:29:14 -06:00
|
|
|
class QixLine {
|
|
|
|
/**
|
|
|
|
* @param {Number} hue
|
|
|
|
* @param {Point} a
|
|
|
|
* @param {Point} b
|
|
|
|
*/
|
|
|
|
constructor(hue, a, b) {
|
|
|
|
this.hue = hue
|
|
|
|
this.a = a
|
|
|
|
this.b = b
|
2023-09-08 18:05:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-11 17:29:14 -06:00
|
|
|
/**
|
|
|
|
* Draw a line dancing around the screen,
|
|
|
|
* like the video game "qix"
|
|
|
|
*/
|
|
|
|
class QixBackground {
|
2023-09-13 18:52:52 -06:00
|
|
|
constructor(ctx, frameRate = 6/Second) {
|
2023-09-11 17:29:14 -06:00
|
|
|
this.ctx = ctx
|
|
|
|
this.min = new Point(0, 0)
|
|
|
|
this.max = new Point(this.ctx.canvas.width, this.ctx.canvas.height)
|
|
|
|
this.box = this.max.Subtract(this.min)
|
|
|
|
|
|
|
|
this.lines = [
|
|
|
|
new QixLine(
|
2023-09-12 19:30:53 -06:00
|
|
|
Math.random(),
|
2023-09-11 17:29:14 -06:00
|
|
|
new Point(randint(this.box.x), randint(this.box.y)),
|
|
|
|
new Point(randint(this.box.x), randint(this.box.y)),
|
2023-09-08 18:05:51 -06:00
|
|
|
)
|
2023-09-11 17:29:14 -06:00
|
|
|
]
|
|
|
|
while (this.lines.length < 18) {
|
|
|
|
this.lines.push(this.lines[0])
|
2023-09-08 18:05:51 -06:00
|
|
|
}
|
2023-09-11 17:29:14 -06:00
|
|
|
this.velocity = new QixLine(
|
|
|
|
0.001,
|
2023-09-12 17:30:36 -06:00
|
|
|
new Point(1 + randint(this.box.x / 100), 1 + randint(this.box.y / 100)),
|
|
|
|
new Point(1 + randint(this.box.x / 100), 1 + randint(this.box.y / 100)),
|
2023-09-11 17:29:14 -06:00
|
|
|
)
|
2023-09-08 18:05:51 -06:00
|
|
|
|
2023-09-13 18:52:52 -06:00
|
|
|
this.frameInterval = Millisecond / frameRate
|
2023-09-12 17:30:36 -06:00
|
|
|
this.nextFrame = 0
|
2023-09-08 18:05:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-09-12 17:30:36 -06:00
|
|
|
* Maybe draw a frame
|
2023-09-08 18:05:51 -06:00
|
|
|
*/
|
2023-09-12 17:30:36 -06:00
|
|
|
Animate() {
|
|
|
|
let now = performance.now()
|
|
|
|
if (now < this.nextFrame) {
|
|
|
|
// Not today, satan
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.nextFrame = now + this.frameInterval
|
|
|
|
|
2023-09-11 17:29:14 -06:00
|
|
|
this.lines.shift()
|
|
|
|
let lastLine = this.lines[this.lines.length - 1]
|
|
|
|
let nextLine = new QixLine(
|
|
|
|
(lastLine.hue + this.velocity.hue) % 1.0,
|
|
|
|
lastLine.a.Bounce(this.velocity.a, this.min, this.max),
|
|
|
|
lastLine.b.Bounce(this.velocity.b, this.min, this.max),
|
|
|
|
)
|
|
|
|
|
|
|
|
this.lines.push(nextLine)
|
|
|
|
|
|
|
|
this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height)
|
|
|
|
for (let line of this.lines) {
|
|
|
|
this.ctx.save()
|
2023-09-12 17:30:36 -06:00
|
|
|
this.ctx.strokeStyle = `hwb(${line.hue}turn 0% 0%)`
|
2023-09-11 17:29:14 -06:00
|
|
|
this.ctx.beginPath()
|
|
|
|
this.ctx.moveTo(line.a.x, line.a.y)
|
|
|
|
this.ctx.lineTo(line.b.x, line.b.y)
|
|
|
|
this.ctx.stroke()
|
|
|
|
this.ctx.restore()
|
2023-09-08 18:05:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function init() {
|
2023-09-13 18:52:52 -06:00
|
|
|
// Don't like the background animation? You can disable it by setting a
|
|
|
|
// property in localStorage and reloading.
|
|
|
|
if (localStorage.disableBackgroundAnimation) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-11 17:29:14 -06:00
|
|
|
let canvas = document.createElement("canvas")
|
|
|
|
canvas.width = 640
|
|
|
|
canvas.height = 640
|
|
|
|
canvas.classList.add("wallpaper")
|
|
|
|
document.body.insertBefore(canvas, document.body.firstChild)
|
|
|
|
|
|
|
|
let ctx = canvas.getContext("2d")
|
|
|
|
|
2023-09-12 17:30:36 -06:00
|
|
|
let qix = new QixBackground(ctx)
|
2023-09-12 19:30:53 -06:00
|
|
|
// window.requestAnimationFrame is overkill for something this silly
|
2023-09-13 18:52:52 -06:00
|
|
|
setInterval(() => qix.Animate(), Millisecond/FrameRate)
|
2023-09-08 18:05:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (document.readyState === "loading") {
|
|
|
|
document.addEventListener("DOMContentLoaded", init)
|
|
|
|
} else {
|
|
|
|
init()
|
|
|
|
}
|