vidhimudaliar commited on
Commit
74eec79
·
verified ·
1 Parent(s): 8ea5a48

Update assets/js/video_segment.js

Browse files
Files changed (1) hide show
  1. assets/js/video_segment.js +41 -1
assets/js/video_segment.js CHANGED
@@ -162,7 +162,47 @@ function attachLabeledLoop(videoEl, { label, startFrame, endFrame, fps = DEFAULT
162
  // No snapping during seek; we only enforce the window when time reaches endSec.
163
  }
164
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  window.DEFAULT_FPS = DEFAULT_FPS;
166
  window.attachLabeledLoop = attachLabeledLoop;
167
  window.computeWindowFrames = computeWindowFrames;
168
-
 
162
  // No snapping during seek; we only enforce the window when time reaches endSec.
163
  }
164
 
165
+ /**
166
+ * Lazily loads a video source when the element is near viewport.
167
+ * Expects markup like:
168
+ * <video ...><source data-src="..." type="video/mp4"></video>
169
+ */
170
+ function setupLazyVideo(videoEl, { rootMargin = "300px 0px", threshold = 0.01 } = {}) {
171
+ if (!videoEl) return;
172
+
173
+ const sourceEl = videoEl.querySelector("source[data-src]");
174
+ if (!sourceEl) return;
175
+
176
+ let loaded = false;
177
+
178
+ function loadNow() {
179
+ if (loaded) return;
180
+ const src = sourceEl.dataset.src;
181
+ if (!src) return;
182
+ sourceEl.src = src;
183
+ sourceEl.removeAttribute("data-src");
184
+ videoEl.load();
185
+ loaded = true;
186
+ }
187
+
188
+ if (!("IntersectionObserver" in window)) {
189
+ loadNow();
190
+ return;
191
+ }
192
+
193
+ const observer = new IntersectionObserver((entries) => {
194
+ for (const entry of entries) {
195
+ if (!entry.isIntersecting) continue;
196
+ loadNow();
197
+ observer.unobserve(videoEl);
198
+ break;
199
+ }
200
+ }, { root: null, rootMargin, threshold });
201
+
202
+ observer.observe(videoEl);
203
+ }
204
+
205
  window.DEFAULT_FPS = DEFAULT_FPS;
206
  window.attachLabeledLoop = attachLabeledLoop;
207
  window.computeWindowFrames = computeWindowFrames;
208
+ window.setupLazyVideo = setupLazyVideo;