Spaces:
Runtime error
Runtime error
File size: 5,137 Bytes
5fd3fc6 | 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 172 | class Slideshow {
constructor(num_frames, fps, video_id) {
this.num_frames = num_frames;
this.fps = fps;
this.video_id = video_id;
this._slides = document.getElementById('slideshow');
this.value = document.getElementById('frames_display');
this.is_paused = true;
this._current_frame = 0;
this.playbutton = document.getElementById('play_button');
this._frames = [];
this._masks = [];
// Add options to datalist, which adds tick marks below the input slider
this._slider = document.getElementById('slider');
// Remove tick marks if there are too many
let num_ticks = (this.num_frames > 200) ? 0 : this.num_frames;
let markers = document.getElementById("markers");
for (let i = 0; i < num_ticks; i++) {
let tick = document.createElement('div');
tick.classList.add('tick');
tick.setAttribute('value', i);
markers.appendChild(tick);
}
// Called when slider is being moved
this._slider.addEventListener("change", (event) => {
this._current_frame = parseInt(event.target.value);
this.update_slideshow();
this.reset_interaction();
});
this.playbutton.addEventListener('click', this.play_or_pause.bind(this));
setInterval(this.play_slideshow.bind(this), 1 / this.fps * 1000);
}
load_masks() {
let promise = Promise.resolve(); // Start with a resolved promise
for (let i = 0; i < num_frames; i++) {
promise = promise
.then(() => fetch('/mask/' + String(this.video_id) + '/' + String(i)))
.then(response => response.blob())
.then(blob => {
const blob_url = URL.createObjectURL(blob);
this._masks[i] = blob_url;
});
}
// Return the final promise that resolves when all fetches are complete
return promise;
}
set_current_mask() {
let promise = Promise.resolve(); // Start with a resolved promise
promise = promise
.then(() => fetch('/mask/' + String(this.video_id) + '/' + String(this._current_frame)))
.then(response => response.blob())
.then(blob => {
const blob_url = URL.createObjectURL(blob);
this._masks[this._current_frame] = blob_url;
});
// Return the final promise that resolves when all fetches are complete
return promise;
}
preload_frames() {
let promise = Promise.resolve(); // Start with a resolved promise
for (let i = 0; i < num_frames; i++) {
promise = promise
.then(() => fetch('/frame/' + String(this.video_id) + '/' + String(i)))
.then(response => response.blob())
.then(blob => {
const blob_url = URL.createObjectURL(blob);
this._frames.push(blob_url);
});
}
// Return the final promise that resolves when all fetches are complete
return promise;
}
async initialize() {
await this.preload_frames();
await this.load_masks();
// Set first frame
this.update_slideshow();
}
reset_interaction() {
// Reset interactions in manager
fetch('/reset_interaction', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({video_id: this.video_id})
}).catch(error => {
console.error('Error:', error);
});
}
set_seekbar_value(n) {
this._slider.value = n;
}
play_or_pause() {
this.is_paused = !this.is_paused;
}
/*
Simulate video playing by changing the image
*/
update_slideshow() {
throw new Error('Not implemented');
}
play_slideshow() {
if (!this.is_paused) {
this._current_frame++;
if (this._current_frame > this.num_frames - 1) {
this._current_frame = 0;
}
this.set_seekbar_value(this._current_frame);
this.update_slideshow();
}
}
highlight_tick() {
let tick = document.querySelector(`.tick[value='${this.current_frame}']`);
tick.classList.add('highlight_tick');
}
remove_tick_highlight() {
let tick = document.querySelector(`.tick[value='${this.current_frame}']`);
tick.classList.remove('highlight_tick');
}
remove_all_highlights() {
let highlighted_ticks = document.querySelectorAll('.highlight_tick');
highlighted_ticks.forEach(t => t.classList.remove('highlight_tick'));
}
get current_frame() {
return this._current_frame;
}
get slides() {
return this._slides;
}
get slider() {
return this._slider;
}
get frames() {
return this._frames;
}
get masks() {
return this._masks;
}
}
|