User:Monster Domosed/common.js: Difference between revisionsGive feedback
No edit summary |
No edit summary |
||
| (11 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
(function () { | (function () { | ||
'use strict'; | 'use strict'; | ||
const urlCache = new Map(); | const urlCache = new Map(); | ||
function getSize(element) { | function getSize(element) { | ||
const | const match = element.className.match(/model_size_(\d+)/); | ||
return match ? parseInt(match[1], 10) : 500; | |||
} | } | ||
function getFileUrl(fileName) { | function getFileUrl(fileName) { | ||
if (urlCache.has(fileName)) { | if (urlCache.has(fileName)) { | ||
return urlCache.get(fileName); | return urlCache.get(fileName); | ||
} | } | ||
| Line 76: | Line 21: | ||
'&iiprop=url' + | '&iiprop=url' + | ||
'&format=json'; | '&format=json'; | ||
const promise = fetch(apiUrl) | const promise = fetch(apiUrl) | ||
.then(function (response) { | .then(function (response) { | ||
if (!response.ok) { | if (!response.ok) { | ||
throw new Error( | throw new Error('API request failed'); | ||
} | } | ||
return response.json(); | return response.json(); | ||
}) | }) | ||
.then(function (data) { | .then(function (data) { | ||
const page = Object.values(data.query.pages)[0]; | |||
return page && page.imageinfo ? page.imageinfo[0].url : null; | |||
}) | |||
.catch(function () { | |||
return null; | |||
}); | |||
urlCache.set(fileName, promise); | |||
return promise; | |||
} | |||
function buildAnimationControls(wrap, container, viewer, defaultAnimation, autoplay) { | |||
const bar = document.createElement('div'); | |||
bar.className = 'model-viewer-controls'; | |||
const playButton = document.createElement('button'); | |||
playButton.className = 'model-anim-button'; | |||
playButton.type = 'button'; | |||
const select = document.createElement('select'); | |||
select.className = 'model-anim-select'; | |||
const slider = document.createElement('input'); | |||
slider.className = 'model-anim-slider'; | |||
slider.type = 'range'; | |||
slider.min = '0'; | |||
slider.max = '1000'; | |||
slider.value = '0'; | |||
slider.step = '1'; | |||
let paused = !autoplay; | |||
let rafId = null; | |||
let duration = 0; | |||
function updateSlider() { | |||
if (duration > 0) { | |||
slider.value = String(Math.round((viewer.currentTime / duration) * 1000)); | |||
} | |||
rafId = requestAnimationFrame(updateSlider); | |||
} | |||
function startLoop() { | |||
cancelAnimationFrame(rafId); | |||
rafId = requestAnimationFrame(updateSlider); | |||
} | |||
function setPlaying(playing) { | |||
paused = !playing; | |||
playButton.textContent = playing ? '❚❚' : '▶'; | |||
} | if (playing) { | ||
viewer.play(); | |||
} else { | |||
viewer.pause(); | |||
} | |||
} | |||
playButton.addEventListener('click', function () { | |||
setPlaying(paused); | |||
}); | |||
slider.addEventListener('input', function () { | |||
if (duration > 0) { | |||
viewer.currentTime = (parseFloat(slider.value) / 1000) * duration; | |||
} | |||
}); | |||
viewer.addEventListener('load', function () { | |||
const animations = viewer.availableAnimations || []; | |||
if (animations.length) { | |||
animations.forEach(function (animName) { | |||
const option = document.createElement('option'); | |||
} | option.value = animName; | ||
option.textContent = animName; | |||
select.appendChild(option); | |||
}); | |||
if ( | if (defaultAnimation && animations.indexOf(defaultAnimation) !== -1) { | ||
select.value = defaultAnimation; | |||
} | } | ||
viewer.animationName = select.value; | |||
duration = viewer.duration || 0; | |||
select.addEventListener('change', function () { | |||
viewer.animationName = select.value; | |||
duration = viewer.duration || 0; | |||
viewer.currentTime = 0; | |||
if (paused) { | |||
setPlaying(true); | |||
} | |||
}); | |||
if (paused) { | |||
setPlaying(false); | |||
} | |||
startLoop(); | |||
bar.classList.add('model-viewer-controls--visible'); | |||
} | |||
}); | |||
bar.appendChild(playButton); | |||
bar.appendChild(select); | |||
bar.appendChild(slider); | |||
container.appendChild(bar); | |||
} | } | ||
function loadModel(preview) { | function loadModel(preview) { | ||
if (preview.dataset.loaded) { | if (preview.dataset.loaded) { | ||
return; | return; | ||
} | } | ||
| Line 157: | Line 147: | ||
const fileName = preview.dataset.model; | const fileName = preview.dataset.model; | ||
const displayName = preview.dataset.name || fileName; | const displayName = preview.dataset.name || fileName; | ||
const defaultAnimation = preview.dataset.animation || ''; | |||
const autoplay = preview.dataset.autoplay === 'true'; | |||
const size = getSize(preview); | const size = getSize(preview); | ||
getFileUrl(fileName + '.glb') | getFileUrl(fileName + '.glb') | ||
.then(function (url) { | .then(function (url) { | ||
if (!url) { | if (!url) { | ||
return; | return; | ||
} | } | ||
const wrap = document.createElement('div'); | const wrap = document.createElement('div'); | ||
| Line 212: | Line 167: | ||
const viewer = document.createElement('model-viewer'); | const viewer = document.createElement('model-viewer'); | ||
viewer.src = url; | viewer.src = url; | ||
viewer.setAttribute('camera-controls', ''); | viewer.setAttribute('camera-controls', ''); | ||
viewer.setAttribute('touch-action', 'pan-y'); | viewer.setAttribute('touch-action', 'pan-y'); | ||
if (preview.dataset.autoplay === 'true') { | |||
viewer.setAttribute('autoplay', ''); | |||
} | |||
} | |||
container.appendChild(viewer); | container.appendChild(viewer); | ||
wrap.appendChild(container); | wrap.appendChild(container); | ||
buildAnimationControls(wrap, container, viewer, defaultAnimation, autoplay); | |||
const name = document.createElement('div'); | const name = document.createElement('div'); | ||
| Line 240: | Line 184: | ||
wrap.appendChild(name); | wrap.appendChild(name); | ||
preview.replaceWith(wrap); | preview.replaceWith(wrap); | ||
}); | }); | ||
} | } | ||
function initPreview(preview) { | function initPreview(preview) { | ||
if (preview.dataset.initialized) { | if (preview.dataset.initialized) { | ||
return; | return; | ||
} | } | ||
preview.dataset.initialized = '1'; | preview.dataset.initialized = '1'; | ||
const button = document.createElement('button'); | const button = document.createElement('button'); | ||
button.className = 'model-load-button'; | button.className = 'model-load-button'; | ||
button.type = 'button'; | button.type = 'button'; | ||
button.addEventListener('click', function () { | button.addEventListener('click', function () { | ||
loadModel(preview); | loadModel(preview); | ||
}); | }); | ||
preview.appendChild(button); | preview.appendChild(button); | ||
} | } | ||
function scan( | function scan() { | ||
document | |||
.querySelectorAll('.model-placeholder') | |||
.forEach(initPreview); | |||
} | } | ||
let scanTimer = null; | |||
function debouncedScan() { | |||
clearTimeout(scanTimer); | |||
scanTimer = setTimeout(scan, 100); | |||
} | } | ||
scan(); | |||
const observer = new MutationObserver(debouncedScan); | |||
observer.observe(document.body, { | |||
childList: true, | |||
subtree: true | |||
}); | |||
} | |||
})(); | })(); | ||