-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMMM-PhotoStack.js
More file actions
146 lines (128 loc) · 4.55 KB
/
MMM-PhotoStack.js
File metadata and controls
146 lines (128 loc) · 4.55 KB
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
Module.register("MMM-PhotoStack", {
defaults: {
imagePaths: [],
speed: 8000,
stackSize: 4,
maxRotation: 8,
maxOffset: 30,
frameColor: "white",
backgroundColor: "black",
frameWidth: 16,
photoWidth: 400,
flyInDuration: 1200,
flyOutDuration: 800,
recursiveSubDirectories: true,
randomizeImageOrder: true,
imageExtensions: ["jpg", "jpeg", "png", "gif", "webp"],
rescanInterval: 0
},
start() {
this.urls = [];
this.cursor = 0;
this.cards = [];
this.timer = null;
this.registered = false;
this.sendSocketNotification("PHOTOSTACK_REGISTER", {
identifier: this.identifier,
paths: this.config.imagePaths,
recursive: this.config.recursiveSubDirectories,
extensions: this.config.imageExtensions,
randomize: this.config.randomizeImageOrder,
rescanInterval: this.config.rescanInterval
});
},
getStyles() {
return ["MMM-PhotoStack.css"];
},
getDom() {
if (!this.container) {
this.container = document.createElement("div");
this.container.className = "photostack-container";
this.container.style.setProperty("--photo-width", this.config.photoWidth + "px");
this.container.style.setProperty("--frame-width", this.config.frameWidth + "px");
this.container.style.setProperty("--frame-color", this.config.frameColor);
this.container.style.setProperty("--background-color", this.config.backgroundColor);
this.container.style.setProperty("--fly-in-duration", this.config.flyInDuration + "ms");
this.container.style.setProperty("--fly-out-duration", this.config.flyOutDuration + "ms");
}
return this.container;
},
socketNotificationReceived(notification, payload) {
if (notification !== "PHOTOSTACK_IMAGES") return;
if (!payload || payload.identifier !== this.identifier) return;
const hadUrls = this.urls.length > 0;
this.urls = payload.urls || [];
if (this.urls.length === 0) return;
if (!this.container) this.updateDom();
if (!hadUrls) {
this.cursor = 0;
this.scheduleNextCard(true);
}
},
scheduleNextCard(immediate) {
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
}
if (!this.urls || this.urls.length === 0) return;
const delay = immediate ? 0 : this.config.speed;
this.timer = setTimeout(() => {
this.timer = null;
if (!this.container) {
this.scheduleNextCard(false);
return;
}
this.addCard();
this.scheduleNextCard(false);
}, delay);
},
addCard() {
if (!this.container || this.urls.length === 0) return;
const url = this.urls[this.cursor % this.urls.length];
this.cursor = (this.cursor + 1) % this.urls.length;
const entries = [
{ x: "120vw", y: "-60vh" },
{ x: "-120vw", y: "-60vh" },
{ x: "0vw", y: "-150vh" },
{ x: "120vw", y: "0vh" }
];
const entry = entries[Math.floor(Math.random() * entries.length)];
const restX = this.randomBetween(-this.config.maxOffset, this.config.maxOffset);
const restY = this.randomBetween(-this.config.maxOffset, this.config.maxOffset);
const restRotate = this.randomBetween(-this.config.maxRotation, this.config.maxRotation);
const card = document.createElement("div");
card.className = "photostack-card photostack-fly-in";
card.style.setProperty("--rest-x", restX.toFixed(2) + "px");
card.style.setProperty("--rest-y", restY.toFixed(2) + "px");
card.style.setProperty("--rest-rotate", restRotate.toFixed(2) + "deg");
card.style.setProperty("--in-x", entry.x);
card.style.setProperty("--in-y", entry.y);
const img = document.createElement("img");
img.className = "photostack-image";
img.src = url;
img.alt = "";
card.appendChild(img);
for (const existing of this.cards) {
const z = parseInt(existing.element.style.zIndex, 10) || 0;
existing.element.style.zIndex = String(z - 1);
}
const newZ = this.config.stackSize;
card.style.zIndex = String(newZ);
this.container.appendChild(card);
this.cards.push({ element: card });
setTimeout(() => {
card.classList.remove("photostack-fly-in");
}, this.config.flyInDuration);
while (this.cards.length > this.config.stackSize) {
const oldest = this.cards.shift();
const el = oldest.element;
el.classList.add("photostack-fly-out");
setTimeout(() => {
if (el.parentNode) el.parentNode.removeChild(el);
}, this.config.flyOutDuration);
}
},
randomBetween(min, max) {
return Math.random() * (max - min) + min;
}
});