Skip to content

Commit

Permalink
Merge pull request #140 from nickhsine/tsp
Browse files Browse the repository at this point in the history
refactor(tsp): play audio if in the viewport
  • Loading branch information
nickhsine committed Mar 16, 2023
2 parents 5608031 + de27d86 commit 3b71792
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 29 deletions.
1 change: 1 addition & 0 deletions packages/three-story-points/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"dependencies": {
"gsap": "^3.11.4",
"lodash": "^4.17.21",
"react-intersection-observer": "^9.3.5",
"three": "^0.150.1",
"three-story-controls": "^1.0.6"
},
Expand Down
7 changes: 4 additions & 3 deletions packages/three-story-points/src/react-components/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,18 @@ function useMuted(initialValue = true) {

/**
* @param {Object} opts
* @param {boolean} opts.play
* @param {string[]} opts.audioUrls
* @param {string} [opts.preload='auto'] - 'auto', 'none' or 'metadata'. `preload` attribute of `audio` tag.
*/
export default function Audio({ audioUrls, preload = 'auto' }) {
export default function Audio({ play, audioUrls, preload = 'auto' }) {
const audioRef = useRef(null)
const muted = useMuted(true)

useEffect(() => {
const audio = audioRef.current
// Do not need to play audio if `muted` is true.
if (!audio || muted) {
if (!audio || muted || !play) {
return
}

Expand Down Expand Up @@ -170,7 +171,7 @@ export default function Audio({ audioUrls, preload = 'auto' }) {
audio.pause()
}
}
}, [muted])
}, [muted, play])

return (
<audio ref={audioRef} preload={preload}>
Expand Down
59 changes: 33 additions & 26 deletions packages/three-story-points/src/react-components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from 'three'
import { loadGltfModel } from '../loader.js'
import { useState, useEffect, useRef, useMemo } from 'react'
import { useInView } from 'react-intersection-observer'

const _ = {
throttle,
Expand Down Expand Up @@ -243,9 +244,13 @@ export default function ThreeStoryPoints({
pois: plainPois = [],
debugMode = false,
}) {
const containerRef = useRef(null)
const [leftOffset, setLeftOffset] = useState(0)
const [models, setModels] = useState([])
const [poiIndex, setPoiIndex] = useState(0)
const [inViewRef, inView] = useInView({
threshold: [0.6],
})

/** @type POI[] */
const pois = useMemo(() => {
Expand All @@ -262,7 +267,6 @@ export default function ThreeStoryPoints({
})
}, [plainPois])

const containerRef = useRef(null)
const canvasRef = useRef(null)
const threeObj = useMemo(() => createThreeObj(models, pois, canvasRef), [
models,
Expand Down Expand Up @@ -376,30 +380,33 @@ export default function ThreeStoryPoints({
}, [threeObj])

return (
<Block leftOffset={leftOffset} ref={containerRef}>
<canvas ref={canvasRef}></canvas>
{poiIndex > 0 ? (
<PrevNav
onClick={() => {
threeObj.controls.prevPOI()
}}
/>
) : null}
{poiIndex < pois.length - 1 ? (
<NextNav
onClick={() => {
threeObj.controls.nextPOI()
}}
/>
) : null}
<Caption>{captions[poiIndex]}</Caption>
{audios?.[poiIndex]?.urls && (
<Audio
key={poiIndex}
audioUrls={audios?.[poiIndex]?.urls}
preload={audios?.[poiIndex]?.preload}
/>
)}
<div ref={inViewRef}>
<Block leftOffset={leftOffset} ref={containerRef}>
<canvas ref={canvasRef}></canvas>
{poiIndex > 0 ? (
<PrevNav
onClick={() => {
threeObj.controls.prevPOI()
}}
/>
) : null}
{poiIndex < pois.length - 1 ? (
<NextNav
onClick={() => {
threeObj.controls.nextPOI()
}}
/>
) : null}
<Caption>{captions[poiIndex]}</Caption>
{audios?.[poiIndex]?.urls && (
<Audio
key={poiIndex}
play={inView}
audioUrls={audios?.[poiIndex]?.urls}
preload={audios?.[poiIndex]?.preload}
/>
)}
</Block>
{debugMode && (
<AudioPlayButton
onClick={() => {
Expand All @@ -411,6 +418,6 @@ export default function ThreeStoryPoints({
播放聲音
</AudioPlayButton>
)}
</Block>
</div>
)
}

0 comments on commit 3b71792

Please sign in to comment.