Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add video component #19

Merged
merged 4 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nasty-monkeys-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'threlte-uikit': minor
---

Add `<Video>` Component
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ The component internals may be accessed via the `ref` property.
- [Text](https://docs.pmnd.rs/uikit/getting-started/components-and-properties#text)
- [SVG](https://docs.pmnd.rs/uikit/getting-started/components-and-properties#svg)
- [Content](https://docs.pmnd.rs/uikit/getting-started/components-and-properties#content)
- [Video](https://docs.pmnd.rs/uikit/getting-started/components-and-properties#video)

## Context providers

Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"!dist/**/*.spec.*"
],
"dependencies": {
"@pmndrs/uikit": "^0.3.8"
"@pmndrs/uikit": "^0.3.10"
},
"peerDependencies": {
"@threlte/core": ">=7",
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/Image.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
const innerRef = currentWritable(new Group())
const { style, properties, defaults } = usePropertySignals<ImageProperties>()
$: props = { ...$$restProps }
$: properties.value = $$restProps
$: properties.value = props

const internals = createImage(parent, style, properties, defaults, outerRef, innerRef)
$: internals.interactionPanel.name = name ?? ''
Expand Down
79 changes: 79 additions & 0 deletions src/lib/components/Video.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<script lang="ts">
import Image from './Image.svelte'
import { SRGBColorSpace, VideoTexture } from 'three'
import { useThrelte } from '@threlte/core'
import type { VideoProperties } from '@pmndrs/uikit'
import { setupVideoElementInvalidation, updateVideoElement } from '@pmndrs/uikit/internals'
import type { EventHandlers } from '$lib/Events'
import type { ImageRef } from '$lib/useInternals'
import { onDestroy } from 'svelte'

type $$Props = VideoProperties & {
ref?: ImageRef
src: string | HTMLVideoElement
element?: HTMLVideoElement
} & EventHandlers

export let src: $$Props['src']
export let ref: $$Props['ref'] = undefined
export let autoplay: $$Props['autoplay'] = undefined
export let loop: $$Props['loop'] = undefined
export let muted: $$Props['muted'] = undefined
export let playbackRate: $$Props['playbackRate'] = undefined
export let preservesPitch: $$Props['preservesPitch'] = undefined
export let volume: $$Props['volume'] = undefined

const { invalidate } = useThrelte()

let aspectRatio = 1

$: providedHtmlElement = src instanceof HTMLVideoElement ? src : undefined

export let element: HTMLVideoElement | undefined = undefined
$: element = providedHtmlElement ?? document.createElement('video')
$: setupVideoElementInvalidation(element, invalidate)
$: updateVideoElement(element, {
src,
autoplay,
loop,
muted,
playbackRate,
preservesPitch,
volume,
})

const updateAspectRatio = () => {
aspectRatio = element.videoWidth / element.videoHeight
}

$: {
element
updateAspectRatio()
}

$: {
element.removeEventListener('resize', updateAspectRatio)
element.addEventListener('resize', updateAspectRatio)
}

onDestroy(() => element.removeEventListener('resize', updateAspectRatio))

let texture: VideoTexture | undefined

$: {
if (texture) texture.dispose()
texture = new VideoTexture(element)
texture.colorSpace = SRGBColorSpace
}
</script>

{#if texture}
<Image
bind:ref
{aspectRatio}
{...$$restProps}
src={texture}
>
<slot />
</Image>
{/if}
1 change: 1 addition & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export { default as Image } from './components/Image.svelte'
export { default as Root } from './components/Root.svelte'
export { default as SVG } from './components/Svg.svelte'
export { default as Text } from './components/Text.svelte'
export { default as Video } from './components/Video.svelte'

// Refs
export type { ContainerRef, ContentRef, ImageRef, RootRef, SvgRef, TextRef } from './useInternals'
27 changes: 24 additions & 3 deletions src/routes/Scene.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import { Root, Container, Text, Image, Content, SVG } from '$lib'
import { Root, Container, Text, Image, Content, SVG, Video } from '$lib'
import { T, useTask } from '@threlte/core'
import { PerfMonitor } from '@threlte/extras'
import { PerfMonitor, TransformControls } from '@threlte/extras'
import { OrbitControls, interactivity } from '@threlte/extras'
import Fullscreen from './Fullscreen.svelte'
// import { Inspector } from 'three-inspect'
Expand All @@ -15,15 +15,18 @@
elapsed += delta
val = Math.sin(elapsed * 5) * 20
})

let clicked = false
</script>

<svelte:window on:click={() => (clicked = true)} />
<PerfMonitor />

<Fullscreen />

<T.PerspectiveCamera
makeDefault
position={[2, 2, 5]}
position={[5, 5, 10]}
on:create={({ ref }) => ref.lookAt(0, 0, 0)}
>
<OrbitControls />
Expand All @@ -41,6 +44,7 @@
padding={20}
width={330 + val}
height={300}
backgroundColor="#fff"
hover={{
backgroundColor: '#ccc',
}}
Expand Down Expand Up @@ -91,4 +95,21 @@
</Root>
</T.Group>

{#if clicked}
<TransformControls position.y={3}>
<T.Group>
<Root
height={200}
width={400}
>
<Video
autoplay
borderRadius={10}
src="/BigBuckBunny_320x180.mp4"
/>
</Root>
</T.Group>
</TransformControls>
{/if}

<T.DirectionalLight />
Binary file added static/BigBuckBunny_320x180.mp4
Binary file not shown.