Add basic frontend animation

This commit is contained in:
Dennis Schoepf 2020-11-24 16:27:50 +01:00
parent 85998c634c
commit 84c7c12a3f
8 changed files with 8466 additions and 28 deletions

BIN
dii-hackathon-extension.zip Normal file

Binary file not shown.

View file

@ -5,7 +5,7 @@
"permissions": ["storage", "activeTab"], "permissions": ["storage", "activeTab"],
"background": { "background": {
"scripts": ["dist/background/index.js"], "scripts": ["dist/background/index.js"],
"persistent": false "persistent": true
}, },
"content_scripts": [ "content_scripts": [
{ {

8407
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -5,10 +5,10 @@
"main": "background.js", "main": "background.js",
"scripts": { "scripts": {
"clean": "rm -rf dist/background & rm -rf dist/frontend", "clean": "rm -rf dist/background & rm -rf dist/frontend",
"serve": "parcel watch src/background/index.js --out-dir dist/background & parcel watch src/frontend/index.js --out-dir dist/frontend", "serve": "parcel watch src/**/* --out-dir dist/background & parcel watch src/frontend/index.js --out-dir dist/frontend",
"build:back": "parcel build src/background/index.js --out-dir dist/background", "build:back": "parcel build src/background/index.js --out-dir dist/background",
"build:front": "parcel build src/frontend/index.js --out-dir dist/frontend", "build:front": "parcel build src/frontend/index.js --out-dir dist/frontend",
"build": "npm run clean && npm run build:back & npm run build:front", "build": "npm run clean && npm run build:back && npm run build:front",
"package": "npm run build && zip -r dii-hackathon-extension.zip dist/ manifest.json" "package": "npm run build && zip -r dii-hackathon-extension.zip dist/ manifest.json"
}, },
"repository": { "repository": {
@ -23,6 +23,7 @@
}, },
"homepage": "https://github.com/dennisschoepf/dii-hackathon-extension#readme", "homepage": "https://github.com/dennisschoepf/dii-hackathon-extension#readme",
"devDependencies": { "devDependencies": {
"animejs": "^3.2.1",
"dotenv": "^8.2.0", "dotenv": "^8.2.0",
"mqtt": "4.1.0", "mqtt": "4.1.0",
"parcel": "^1.12.4" "parcel": "^1.12.4"

View file

@ -12,6 +12,8 @@ const mqttOptions = {
const client = mqtt.connect("mqtt://io.adafruit.com", mqttOptions); const client = mqtt.connect("mqtt://io.adafruit.com", mqttOptions);
export function subscribeToAllSensors({ onHeartbeat, onTilt }) { export function subscribeToAllSensors({ onHeartbeat, onTilt }) {
console.log(process.env.AIO_USERNAME);
client.on("connect", () => { client.on("connect", () => {
client.subscribe(mqttHeartbeatTopic); client.subscribe(mqttHeartbeatTopic);
client.subscribe(mqttTiltTopic); client.subscribe(mqttTiltTopic);

View file

@ -2,9 +2,9 @@ import { subscribeToAllSensors } from "./api";
import dotenv from "dotenv"; import dotenv from "dotenv";
import { storeValue } from "./storage"; import { storeValue } from "./storage";
dotenv.config();
chrome.runtime.onInstalled.addListener(() => { chrome.runtime.onInstalled.addListener(() => {
dotenv.config();
subscribeToAllSensors({ subscribeToAllSensors({
onHeartbeat: (heartbeatData) => storeValue("heartbeat", heartbeatData), onHeartbeat: (heartbeatData) => storeValue("heartbeat", heartbeatData),
onTilt: (tiltData) => storeValue("tilt", tiltData), onTilt: (tiltData) => storeValue("tilt", tiltData),

52
src/frontend/animation.js Normal file
View file

@ -0,0 +1,52 @@
import anime from "animejs";
export function setupStyles() {
const style = document.createElement("style");
style.innerHTML = `
.heartbeat-wrapper {
z-index: 9999;
position: fixed;
bottom: 120px;
width: 100vw;
display: flex;
justify-content: space-between;
}
.heartbeat-wrapper > .el {
height: 7px;
width: 7px;
border-radius: 50%;
background-color: #E02D25;
opacity: 0.75;
}
`;
document.head.appendChild(style);
}
export function setupHeartbeatElement() {
console.log("Start creation of heartbeat element");
const wrapper = document.createElement("div");
wrapper.classList.add("heartbeat-wrapper");
for (let i = 0; i < 120; i++) {
const el = document.createElement("div");
el.classList.add("el");
wrapper.appendChild(el);
}
document.body.appendChild(wrapper);
}
export function animate() {
return anime({
targets: ".heartbeat-wrapper .el",
keyframes: [{ translateY: 15 }, { translateY: -60 }, { translateY: 0 }],
duration: 1000,
easing: "cubicBezier(.1,1.42,.85,.09)",
delay: anime.stagger(100, {
start: 500,
}),
loop: 3,
});
}

View file

@ -1,23 +1,19 @@
const testEl = document.createElement("div"); import { setupHeartbeatElement, setupStyles, animate } from "./animation";
testEl.style["position"] = "fixed";
testEl.style["top"] = "0"; const started = false;
testEl.style["left"] = "0"; const timeElapsed = setupStyles();
testEl.style["z-index"] = "1000"; setupHeartbeatElement();
testEl.style["background-color"] = "red";
document.body.appendChild(testEl);
chrome.storage.onChanged.addListener((changes, namespace) => { chrome.storage.onChanged.addListener((changes, namespace) => {
for (let key in changes) { for (let key in changes) {
const newValue = changes[key]; const newValue = changes[key];
if (key === "heartbeat") { if (key === "heartbeat") {
console.log("heartbeat", newValue); const animation = animate();
testEl.innerHTML += `\nNew heartbeat: ${JSON.stringify(newValue)}`;
}
if (key === "tilt") { if (started) {
console.log("tilt", newValue); animation.restart();
testEl.innerHTML += `\nNew tilt: ${JSON.stringify(newValue)}`; }
} }
} }
}); });