-
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from pmndrs/feature/video
Feature/video
- Loading branch information
Showing
7 changed files
with
168 additions
and
2 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { | ||
ComponentPropsWithoutRef, | ||
ReactNode, | ||
RefAttributes, | ||
createContext, | ||
forwardRef, | ||
useContext, | ||
useEffect, | ||
useImperativeHandle, | ||
useMemo, | ||
} from 'react' | ||
import { Image } from './image.js' | ||
import { Texture, VideoTexture } from 'three' | ||
import { signal } from '@preact/signals-core' | ||
|
||
const VideoContainerContext = createContext<HTMLVideoElement | undefined>(undefined) | ||
|
||
export function useVideoElement(): HTMLVideoElement { | ||
const element = useContext(VideoContainerContext) | ||
if (element == null) { | ||
throw new Error(`useVideoElement can only be executed inside a VideoContainer`) | ||
} | ||
return element | ||
} | ||
|
||
export const VideoContainer: ( | ||
props: Omit<ComponentPropsWithoutRef<typeof Image>, 'src'> & { | ||
src: string | MediaStream | ||
volume?: number | ||
preservesPitch?: boolean | ||
playbackRate?: number | ||
muted?: boolean | ||
loop?: boolean | ||
autoplay?: boolean | ||
} & RefAttributes<HTMLVideoElement>, | ||
) => ReactNode = forwardRef( | ||
( | ||
props: Omit<ComponentPropsWithoutRef<typeof Image>, 'src'> & { | ||
src: string | MediaStream | ||
volume?: number | ||
preservesPitch?: boolean | ||
playbackRate?: number | ||
muted?: boolean | ||
loop?: boolean | ||
autoplay?: boolean | ||
}, | ||
ref, | ||
) => { | ||
const texture = useMemo(() => signal<Texture | undefined>(undefined), []) | ||
const aspectRatio = useMemo(() => signal<number>(1), []) | ||
const video = useMemo(() => document.createElement('video'), []) | ||
useEffect(() => { | ||
if (!props.autoplay) { | ||
return | ||
} | ||
video.style.position = 'absolute' | ||
video.style.width = '1px' | ||
video.style.zIndex = '-1000' | ||
video.style.top = '0px' | ||
video.style.left = '0px' | ||
document.body.append(video) | ||
return () => video.remove() | ||
}, [props.autoplay, video]) | ||
video.playsInline = true | ||
video.volume = props.volume ?? 1 | ||
video.preservesPitch = props.preservesPitch ?? true | ||
video.playbackRate = props.playbackRate ?? 1 | ||
video.muted = props.muted ?? false | ||
video.loop = props.loop ?? false | ||
video.autoplay = props.autoplay ?? false | ||
useEffect(() => { | ||
if (typeof props.src === 'string') { | ||
video.src = props.src | ||
} else { | ||
video.srcObject = props.src | ||
} | ||
const updateAspectRatio = () => (aspectRatio.value = video.videoWidth / video.videoHeight) | ||
updateAspectRatio() | ||
video.addEventListener('resize', updateAspectRatio) | ||
return () => video.removeEventListener('resize', updateAspectRatio) | ||
}, [aspectRatio, props.src, video]) | ||
useEffect(() => { | ||
const videoTexture = new VideoTexture(video) | ||
texture.value = videoTexture | ||
return () => videoTexture.dispose() | ||
}, [texture, video]) | ||
|
||
useImperativeHandle(ref, () => video, [video]) | ||
return <Image aspectRatio={aspectRatio} {...props} src={texture} /> | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { Image } from './image.js' | ||
import { VideoTexture } from 'three' | ||
import { ImageProperties } from '../index.js' | ||
import { Signal, signal } from '@preact/signals-core' | ||
|
||
export class VideoContainer extends Image { | ||
public readonly element: HTMLVideoElement | ||
private readonly texture: VideoTexture | ||
private readonly aspectRatio: Signal<number> | ||
private readonly updateAspectRatio: () => void | ||
|
||
constructor( | ||
src: string | MediaStream, | ||
volume?: number, | ||
preservesPitch?: boolean, | ||
playbackRate?: number, | ||
muted?: boolean, | ||
loop?: boolean, | ||
autoplay?: boolean, | ||
properties?: ImageProperties, | ||
defaultProperties?: ImageProperties, | ||
) { | ||
const element = document.createElement('video') | ||
const texture = new VideoTexture(element) | ||
const aspectRatio = signal<number>(1) | ||
super(texture, { aspectRatio, ...properties }, defaultProperties) | ||
this.element = element | ||
this.texture = texture | ||
this.aspectRatio = aspectRatio | ||
if (autoplay) { | ||
this.element.style.position = 'absolute' | ||
this.element.style.width = '1px' | ||
this.element.style.zIndex = '-1000' | ||
this.element.style.top = '0px' | ||
this.element.style.left = '0px' | ||
document.body.append(this.element) | ||
} | ||
this.element.playsInline = true | ||
this.element.volume = volume ?? 1 | ||
this.element.preservesPitch = preservesPitch ?? true | ||
this.element.playbackRate = playbackRate ?? 1 | ||
this.element.muted = muted ?? false | ||
this.element.loop = loop ?? false | ||
this.element.autoplay = autoplay ?? false | ||
if (typeof src === 'string') { | ||
this.element.src = src | ||
} else { | ||
this.element.srcObject = src | ||
} | ||
this.updateAspectRatio = () => (aspectRatio.value = this.element.videoWidth / this.element.videoHeight) | ||
this.updateAspectRatio() | ||
this.element.addEventListener('resize', this.updateAspectRatio) | ||
} | ||
|
||
setProperties(properties?: ImageProperties): void { | ||
super.setProperties({ | ||
aspectRatio: this.aspectRatio, | ||
...properties, | ||
}) | ||
} | ||
|
||
destroy(): void { | ||
super.destroy() | ||
this.texture.dispose() | ||
this.element.remove() | ||
this.element.removeEventListener('resize', this.updateAspectRatio) | ||
} | ||
} |