-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
171 lines (142 loc) · 4.88 KB
/
renderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const { ipcRenderer } = require("electron");
const QRCode = require("qrcode");
const lockImagePath = "img/lock_black_48dp.svg";
const titleElement = document.querySelector("#title");
const descriptionElement = document.querySelector("#description");
const artistElement = document.querySelector("#artist");
const qrImageElement = document.querySelector("#qr");
const slideableBackgroundElement = document.querySelector(
"#slideable-background"
);
const qrBoxElement = document.querySelector("#qr-box");
const qrHintContentContainerElement = document.querySelector(
"#qr-hint-content-container"
);
const qrHintContentElement = document.querySelector("#qr-hint-content");
const qrHintContentHeightElement = document.querySelector("#qr-hint-content-height");
const qrHintIconElement = document.querySelector("#qr-hint-icon");
const FULL_WIDTH = 574;
const HIDE_TRANSLATE_X = `translateX(-${FULL_WIDTH + 4}px)`;
import { Gradient } from "./scripts/Gradient.js";
const createContainerWidthStyle = (widthPercent) =>
`${FULL_WIDTH * widthPercent}px`;
// https://stackoverflow.com/a/11765731/1979008
function setQRCode(url) {
if (url == null) {
qrImageElement.src = lockImagePath;
qrImageElement.classList.add("locked");
return;
}
qrImageElement.classList.remove("locked");
const svg = QRCode.toString(url, { type: "svg" }, (error) => {
if (error) alert(error);
});
const blob = new Blob([svg], { type: "image/svg+xml" });
const imageUrl = URL.createObjectURL(blob);
qrImageElement.crossOrigin = "Anonymous";
qrImageElement.src = imageUrl;
qrImageElement.addEventListener("load", () => URL.revokeObjectURL(imageUrl), {
once: true,
});
}
function updateExperience(experience) {
const { title, description, artist } = experience;
titleElement.innerHTML = title;
descriptionElement.innerHTML = description;
if (artist != null) {
artistElement.style.display = "";
artistElement.innerHTML = artist;
} else {
artistElement.style.display = "none";
}
}
function updateActionHints(hints) {
window.experienceActionHints = hints ?? [];
window.experienceActionHintsShuffle = [];
window.showingActionHint = true;
updateQrHint()
}
ipcRenderer.on("updateExperience", (event, args) => {
updateExperience(args);
});
ipcRenderer.on("updateActionHints", (event, hints) => {
updateActionHints(hints);
});
ipcRenderer.on("updateUrl", (event, url) => {
setQRCode(url);
});
ipcRenderer.on("updateLayout", (event, layout) => {
if (layout === "full") {
slideableBackgroundElement.style.width = createContainerWidthStyle(1);
slideableBackgroundElement.style.transform = "";
qrBoxElement.style.transform = "";
} else if (layout === "slim") {
slideableBackgroundElement.style.width = createContainerWidthStyle(0.6);
slideableBackgroundElement.style.transform = "";
} else if (layout === "hidden") {
slideableBackgroundElement.style.transform = HIDE_TRANSLATE_X;
qrBoxElement.style.transform = "";
// window.innerWidth is kind of a random position, but we want it not to feel like it's
// floating out when nothing else is
}
});
window.addEventListener("load", (event) => {
setQRCode(null);
updateExperience({
title: "Loading...",
description: "Hang tight for something cool!",
});
});
window.addEventListener("load", init);
function pullFromShuffle(original, shuffle) {
if (!original.length) {
return undefined;
}
if (!shuffle.length) {
shuffle.push(...original);
}
const index = Math.floor(Math.random() * shuffle.length);
const item = shuffle[index];
shuffle.splice(index, 1);
return item;
}
async function updateQrHint() {
let shuffleItem;
let showActionName = false;
if (window.showingActionHint) {
shuffleItem = pullFromShuffle(experienceActionHints, experienceActionHintsShuffle);
if (shuffleItem) {
showActionName = true;
} else {
shuffleItem = "control<br>this experience"
}
} else {
shuffleItem = "explore other experiences"
}
window.showingActionHint = !window.showingActionHint;
if (shuffleItem !== qrHintContentElement.innerHTML) {
qrHintContentElement.style.opacity = "0";
}
setTimeout(() => {
qrHintContentElement.innerHTML = shuffleItem;
if (showActionName) {
qrHintContentElement.classList.add("action-name");
} else {
qrHintContentElement.classList.remove("action-name");
}
qrHintContentContainerElement.style.height = `${qrHintContentHeightElement.scrollHeight}px`;
}, 600);
setTimeout(() => {
qrHintContentElement.style.opacity = "1";
}, 1000);
}
async function init() {
const gradient = new Gradient();
gradient.initGradient("#qr-bg-canvas");
window.experienceActionHints = [];
window.experienceActionHintsShuffle = [];
window.showingActionHint = false;
qrHintContentContainerElement.style.height = `${qrHintContentHeightElement.offsetHeight}px`;
await updateQrHint()
setInterval(updateQrHint, 10000);
}