Visualize Your Ideas With Code
import {
BBox,
DependencyContext,
PlaybackState,
SerializedVector2,
SignalValue,
SimpleSignal,
clamp,
isReactive,
useLogger,
useThread,
} from '@motion-canvas/core';
import {computed, initial, nodeName, signal} from '../decorators';
import {DesiredLength} from '../partials';
import {drawImage} from '../utils';
import {Rect, RectProps} from './Rect';
import reactivePlaybackRate from './__logs__/reactive-playback-rate.md';
export interface VideoProps extends RectProps {
/**
* {@inheritDoc Video.src}
*/
src?: SignalValue<string>;
/**
* {@inheritDoc Video.alpha}
*/
alpha?: SignalValue<number>;
/**
* {@inheritDoc Video.smoothing}
*/
smoothing?: SignalValue<boolean>;
/**
* {@inheritDoc Video.loop}
*/
loop?: SignalValue<boolean>;
/**
* {@inheritDoc Video.playbackRate}
*/
playbackRate?: number;
/**
* The starting time for this video in seconds.
*/
time?: SignalValue<number>;
play?: boolean;
}
@nodeName('Video')
export class Video extends Rect {
private static readonly pool: Record<string, HTMLVideoElement> = {};
/**
* The source of this video.
*
* @example
* Using a local video:
* ```tsx
* import video from './example.mp4';
* // ...
* view.add(<Video src={video} />)
* ```
* Loading an image from the internet:
* ```tsx
* view.add(<Video src="https://example.com/video.mp4" />)
* ```
*/
@signal()
public declare readonly src: SimpleSignal<string, this>;
/**
* The alpha value of this video.
*
* @remarks
* Unlike opacity, the alpha value affects only the video itself, leaving the
* fill, stroke, and children intact.
*/
@initial(1)
@signal()
public declare readonly alpha: SimpleSignal<number, this>;
/**
* Whether the video should be smoothed.
*
* @remarks
* When disabled, the video will be scaled using the nearest neighbor
* interpolation with no smoothing. The resulting video will appear pixelated.
*
* @defaultValue true
*/
@initial(true)
@signal()
public declare readonly smoothing: SimpleSignal<boolean, this>;
/**
* Whether this video should loop upon reaching the end.
*/
@initial(false)
@signal()
public declare readonly loop: SimpleSignal<boolean, this>;
/**
* The rate at which the video plays, as multiples of the normal speed.
*
* @defaultValue 1
*/
@initial(1)
@signal()
public declare readonly playbackRate: SimpleSignal<number, this>;
@initial(0)
@signal()
protected declare readonly time: SimpleSignal<number, this>;
@initial(false)
@signal()
protected declare readonly playing: SimpleSignal<boolean, this>;
private lastTime = -1;
public constructor({play, ...props}: VideoProps) {
super(props);
if (play) {
this.play();
}
}
/**
* {@inheritDoc Curve.completion}
*/
public curveCompletion(): number {
return super.completion();
}
public isPlaying(): boolean {
return this.playing();
}
public getCurrentTime(): number {
return this.clampTime(this.time());
}
public getDuration(): number {
return this.video().duration;
}
protected override desiredSize(): SerializedVector2<DesiredLength> {
const custom = super.desiredSize();
if (custom.x === null && custom.y === null) {
const image = this.video();
return {
x: image.videoWidth,
y: image.videoHeight,
};
}
return custom;
}
/**
* The completion of this video in the range from 0 to 1.
*
* @remarks
* To get the percentage of the stroke that's currently visible, use
* {@link Video.curveCompletion} instead.
*/
@computed()
public override completion(): number {
return this.clampTime(this.time()) / this.video().duration;
}
@computed()
protected video(): HTMLVideoElement {
const src = this.src();
const key = `${this.key}/${src}`;
let video = Video.pool[key];
if (!video) {
video = document.createElement('video');
video.src = src;
Video.pool[key] = video;
}
if (video.readyState < 2) {
DependencyContext.collectPromise(
new Promise<void>(resolve => {
const listener = () => {
resolve();
video.removeEventListener('canplay', listener);
};
video.addEventListener('canplay', listener);
}),
);
}
return video;
}
@computed()
protected seekedVideo(): HTMLVideoElement {
const video = this.video();
const time = this.clampTime(this.time());
video.playbackRate = this.playbackRate();
if (!video.paused) {
video.pause();
}
if (this.lastTime === time) {
return video;
}
this.setCurrentTime(time);
return video;
}
@computed()
protected fastSeekedVideo(): HTMLVideoElement {
const video = this.video();
const time = this.clampTime(this.time());
video.playbackRate = this.playbackRate();
if (this.lastTime === time) {
return video;
}
const playing =
this.playing() && time < video.duration && video.playbackRate > 0;
if (playing) {
if (video.paused) {
DependencyContext.collectPromise(video.play());
}
} else {
if (!video.paused) {
video.pause();
}
}
if (Math.abs(video.currentTime - time) > 0.2) {
this.setCurrentTime(time);
} else if (!playing) {
video.currentTime = time;
}
return video;
}
protected override draw(context: CanvasRenderingContext2D) {
this.drawShape(context);
const alpha = this.alpha();
if (alpha > 0) {
const playbackState = this.view().playbackState();
const video =
playbackState === PlaybackState.Playing ||
playbackState === PlaybackState.Presenting
? this.fastSeekedVideo()
: this.seekedVideo();
const box = BBox.fromSizeCentered(this.computedSize());
context.save();
context.clip(this.getPath());
if (alpha < 1) {
context.globalAlpha *= alpha;
}
context.imageSmoothingEnabled = this.smoothing();
drawImage(context, video, box);
context.restore();
}
if (this.clip()) {
context.clip(this.getPath());
}
this.drawChildren(context);
}
protected override applyFlex() {
super.applyFlex();
const video = this.video();
this.element.style.aspectRatio = (
this.ratio() ?? video.videoWidth / video.videoHeight
).toString();
}
protected setCurrentTime(value: number) {
const video = this.video();
if (video.readyState < 2) return;
video.currentTime = value;
this.lastTime = value;
if (video.seeking) {
DependencyContext.collectPromise(
new Promise<void>(resolve => {
const listener = () => {
resolve();
video.removeEventListener('seeked', listener);
};
video.addEventListener('seeked', listener);
}),
);
}
}
protected setPlaybackRate(playbackRate: number) {
let value: number;
if (isReactive(playbackRate)) {
value = playbackRate();
useLogger().warn({
message: 'Invalid value set as the playback rate',
remarks: reactivePlaybackRate,
inspect: this.key,
stack: new Error().stack,
});
} else {
value = playbackRate;
}
this.playbackRate.context.setter(value);
if (this.playing()) {
if (value === 0) {
this.pause();
} else {
const time = useThread().time;
const start = time();
const offset = this.time();
this.time(() => this.clampTime(offset + (time() - start) * value));
}
}
}
public play() {
const time = useThread().time;
const start = time();
const offset = this.time();
const playbackRate = this.playbackRate();
this.playing(true);
this.time(() => this.clampTime(offset + (time() - start) * playbackRate));
}
public pause() {
this.playing(false);
this.time.save();
this.video().pause();
}
public seek(time: number) {
const playing = this.playing();
this.time(this.clampTime(time));
if (playing) {
this.play();
} else {
this.pause();
}
}
public clampTime(time: number): number {
const duration = this.video().duration;
if (this.loop()) {
time %= duration;
}
return clamp(0, duration, time);
}
protected override collectAsyncResources() {
super.collectAsyncResources();
this.seekedVideo();
}
}