PulseCanvas / website · pulsecanvas.ai / film-player.js
| 1 | import { filmCaptionsUrl, filmUrl } from './media.config.js'; |
| 2 | |
| 3 | const playIcon = '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M8 5.5v13l11-6.5z" fill="currentColor" /></svg>'; |
| 4 | |
| 5 | // Shows a film's poster with a pill-shaped play button; the R2 video loads only on click. |
| 6 | export function mountFilm(container, film, { buttonText = 'Play' } = {}) { |
| 7 | if (!container) return; |
| 8 | container.replaceChildren(); |
| 9 | const poster = document.createElement('img'); |
| 10 | Object.assign(poster, { src: film.poster, alt: '', loading: 'lazy', decoding: 'async' }); |
| 11 | poster.className = 'yt-poster'; |
| 12 | const button = document.createElement('button'); |
| 13 | button.type = 'button'; |
| 14 | button.className = 'yt-play'; |
| 15 | button.setAttribute('aria-label', `${buttonText}: ${film.label}`); |
| 16 | button.innerHTML = `<span class="yt-play-icon">${playIcon}</span><span></span><span class="yt-play-time"></span>`; |
| 17 | button.children[1].textContent = buttonText; |
| 18 | button.children[2].textContent = film.duration; |
| 19 | button.addEventListener('click', () => { |
| 20 | const small = window.matchMedia('(max-width: 820px)').matches || navigator.connection?.saveData; |
| 21 | const video = document.createElement('video'); |
| 22 | if (film.captions) video.crossOrigin = 'anonymous'; |
| 23 | Object.assign(video, { src: filmUrl(film, small ? '720p' : '1080p'), poster: film.poster, controls: true, autoplay: true, playsInline: true, title: film.label }); |
| 24 | video.setAttribute('aria-label', film.label); |
| 25 | if (film.captions) { |
| 26 | const track = document.createElement('track'); |
| 27 | Object.assign(track, { kind: 'captions', src: filmCaptionsUrl(film), srclang: 'en', label: 'English' }); |
| 28 | video.append(track); |
| 29 | } |
| 30 | container.replaceChildren(video); |
| 31 | container.classList.add('is-playing'); |
| 32 | video.play().catch(() => {}); |
| 33 | window.gtag?.('event', 'video_start', { video_title: film.label, video_provider: 'r2', video_url: video.src }); |
| 34 | }); |
| 35 | container.append(poster, button); |
| 36 | container.classList.add('is-ready', 'yt-film'); |
| 37 | } |