Skip to content

Instantly share code, notes, and snippets.

@games
Last active July 4, 2021 19:20
Show Gist options
  • Select an option

  • Save games/855e22cb71d5f31b65bb to your computer and use it in GitHub Desktop.

Select an option

Save games/855e22cb71d5f31b65bb to your computer and use it in GitHub Desktop.
VideoSprite
namespace trio {
export var defaultVideoShader: VideoShader;
export class VideoSprite extends PIXI.Sprite {
constructor(texture: PIXI.Texture, withAlpha: boolean = true) {
super(texture);
if (withAlpha) {
if (!defaultVideoShader) {
defaultVideoShader = new VideoShader(trio.director.renderer.shaderManager);
}
this.shader = defaultVideoShader;
}
}
static fromUrl(url: string, withAlpha: boolean = true) : VideoSprite {
return new VideoSprite(new PIXI.Texture(PIXI.VideoBaseTexture.fromUrl(url)), withAlpha);
}
static fromVideo(video: HTMLVideoElement, withAlpha: boolean = true) : VideoSprite {
return new VideoSprite(new PIXI.Texture(PIXI.VideoBaseTexture.fromVideo(video)), withAlpha);
}
static fromResource(resource: PIXI.loaders.Resource, withAlpha: boolean = true) : VideoSprite {
if (resource.loadType != PIXI.loaders.Resource.LOAD_TYPE.VIDEO) {
throw new TypeError("the resource can't be other type except video");
}
var v: HTMLVideoElement = resource.data as HTMLVideoElement;
v.loop = true;
v.currentTime = 0;
v.load();
v.play();
return VideoSprite.fromVideo(v, withAlpha);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment