From 82447b6a92606bcbc29155a4fbf7cd463132a994 Mon Sep 17 00:00:00 2001 From: Moritz Ruth Date: Mon, 13 Jan 2020 22:37:25 +0100 Subject: [PATCH] Use shaped.js --- assets/js/background-canvas.js | 126 --------------------------------- package.json | 3 +- pages/index.vue | 87 ++++++++++++++++++++++- pages/projects.vue | 12 +++- yarn.lock | 26 +++++-- 5 files changed, 120 insertions(+), 134 deletions(-) delete mode 100644 assets/js/background-canvas.js diff --git a/assets/js/background-canvas.js b/assets/js/background-canvas.js deleted file mode 100644 index 4f37a58..0000000 --- a/assets/js/background-canvas.js +++ /dev/null @@ -1,126 +0,0 @@ -/* eslint-disable no-extra-parens */ -const PROBABILITY = 1 / 50000; -const HEIGHT = 2; -const MIN_LINE_LENGTH = 20; -const MAX_LINE_LENGTH = 300; -const SPEED = 0.4; -const APPEAR_PROBABILITY = 1; - -const COLORS = [ - "#000000", - "#00ff64", - "rgba(0,255,100,0.2)", - "#0064ff" -]; - -function randomBetween (min, max) { - return (Math.random() * (max - min)) + min; -} - -export class BackgroundCanvas { - constructor (element) { - this._element = element; - this._ctx = element.getContext("2d"); - this._destroyed = false; - this._animationFrameRequestID = null; - this._lines = null; - this.width = null; - this.height = null; - - this._windowListeners = { - touchstart: () => { - this._isTouch = true; - }, - resize: () => { - this._points = []; - this._init(); - } - }; - - Object.entries(this._windowListeners) - .forEach(([event, listener]) => window.addEventListener(event, listener, { passive: true })); - - this._init(); - this._loop(); - } - - destroy() { - if (this._destroyed) { - throw new Error("Instance is already destroyed."); - } - - this._destroyed = true; - this._element = null; - this._ctx = null; - - Object.entries(this._windowListeners) - .forEach(([event, listener]) => window.removeEventListener(event, listener)); - - if (this._animationFrameRequestID !== null) { - cancelAnimationFrame(this._animationFrameRequestID); - } - } - - get destroyed() { - return this._destroyed; - } - - _init() { - this._element.width = window.innerWidth; - this._element.height = window.innerHeight; - - this.width = this._element.clientWidth; - this.height = this._element.clientHeight; - - while (this._lines === null || this._lines.length < 8) { - this._lines = []; - - for (let x = 0; x < this.width; x += 1) { - for (let y = 0; y < this.height; y += 1) { - if (Math.random() < PROBABILITY) { - const color = Math.round(Math.random() * (COLORS.length - 1)); - const length = 0; - - this._lines.push({ x, y, color, length }); - } - } - } - } - } - - _loop() { - this._animationFrameRequestID = null; - - this._draw(); - - if (!this._destroyed) { - requestAnimationFrame(() => this._loop()); - } - } - - _draw() { - // eslint-disable-next-line unicorn/prevent-abbreviations - const ctx = this._ctx; - - if (ctx === null) return; - - ctx.clearRect(0, 0, this.width, this.height); - - for (const line of this._lines) { - if (line.length === 0) { - if (Math.random() < APPEAR_PROBABILITY) { - line.length = randomBetween(MIN_LINE_LENGTH, MAX_LINE_LENGTH); - } - } - - ctx.fillStyle = COLORS[line.color]; - ctx.fillRect(line.x, line.y, line.length, HEIGHT); - - line.x += SPEED; - - if (line.x >= this.width) { - line.x = -line.length; - } - } - } -} diff --git a/package.json b/package.json index 9182f8d..a25eba2 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ }, "dependencies": { "@nuxtjs/pwa": "^3.0.0-beta.19", - "kiste": "^1.2.6", + "kiste": "^1.2.7", "nuxt": "^2.11.0", "vue": "^2.6.11", "vue-ripple-directive": "^2.0.1" @@ -28,6 +28,7 @@ "sass": "^1.24.4", "sass-loader": "^8.0.1", "serve": "^11.3.0", + "shaped.js": "^1.0.2", "svg-to-vue-component": "^0.3.8" } } diff --git a/pages/index.vue b/pages/index.vue index f959776..1c5357c 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -110,19 +110,102 @@