victorwesterlund.com/public/assets/js/glitch/Glitch.mjs
Victor Westerlund 36345199e3
Version 11 (#23)
* wip(22w6a): add glitch bg

* wip(22w6b): add content and more glitching

* wip(22w7a): add card backdrop-filter

* wip(22w7b): add visibilitychange event

* wip(22w7c): add link click handler

* wip(22w7d): add webkit backdrop filter support

* wip(22w7e): fix font size

* wip(22w7f): refactor glitch

* wip(22w7g): add forceBg to glitch

* wip(22w7h): add error page

* wip(22w7i): hide contact button

* feat: add OGP tags

Co-authored-by: Cloud Shell <cloud-shell@victor-westerlund.iam.gserviceaccount.com>
2022-02-22 04:46:05 -08:00

41 lines
No EOL
1.1 KiB
JavaScript

export default class Glitch {
constructor(target) {
this.worker = new Worker(this.getWorkerScriptURL());
this.worker.addEventListener("message", event => this.message(event));
this.target = target ? target : document.body;
}
// Update the target CSS background with an image URL
setVisibleBg(image) {
this.target.style.setProperty("background-image", `url(${image})`);
}
// Get URL for the dedicated worker
getWorkerScriptURL() {
const name = "GlitchWorker.js";
const url = new URL(import.meta.url);
// Replace pathname of this file with worker
const path = url.pathname.split("/");
path[path.length - 1] = name;
url.pathname = path.join("/");
return url.toString();
}
// Event handler for messages from worker thread
message(event) {
const data = typeof event.data === "object" ? event.data : [event.data];
switch(data[0]) {
case "READY":
this.worker.postMessage(["START", new URL(location).toString()]);
break;
case "BG_UPDATE":
this.setVisibleBg(data[1]);
break;
}
}
}