Skip to content

Commit 97bc582

Browse files
committed
Added VideoTexture support back in and fixed various load related issues.
1 parent a2eacca commit 97bc582

1 file changed

Lines changed: 47 additions & 37 deletions

File tree

src/pixi/textures/VideoTexture.js

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,47 +9,54 @@
99
* @param source {HTMLVideoElement}
1010
* @param scaleMode {Number} See {{#crossLink "PIXI/scaleModes:property"}}PIXI.scaleModes{{/crossLink}} for possible values
1111
*/
12-
PIXI.VideoTexture = function( source, scaleMode )
12+
PIXI.VideoTexture = function(source, scaleMode)
1313
{
14-
if( !source ){
14+
if (!source)
15+
{
1516
throw new Error( 'No video source element specified.' );
1617
}
1718

1819
// hook in here to check if video is already available.
1920
// PIXI.BaseTexture looks for a source.complete boolean, plus width & height.
2021

21-
if( (source.readyState === source.HAVE_ENOUGH_DATA || source.readyState === source.HAVE_FUTURE_DATA ) && source.width && source.height )
22+
if ((source.readyState === source.HAVE_ENOUGH_DATA || source.readyState === source.HAVE_FUTURE_DATA ) && source.width && source.height)
2223
{
2324
source.complete = true;
2425
}
2526

26-
PIXI.BaseTexture.call( this, source, scaleMode );
27+
this.ended = false;
28+
29+
PIXI.BaseTexture.call(this, source, scaleMode);
2730

2831
this.autoUpdate = false;
2932
this.updateBound = this._onUpdate.bind(this);
3033

31-
if( !source.complete )
34+
if (!source.complete)
3235
{
3336
this._onCanPlay = this.onCanPlay.bind(this);
3437

35-
source.addEventListener( 'canplay', this._onCanPlay );
36-
source.addEventListener( 'canplaythrough', this._onCanPlay );
38+
source.addEventListener('canplay', this._onCanPlay);
39+
source.addEventListener('canplaythrough', this._onCanPlay);
40+
source.addEventListener('ended', this._onEnded.bind(this));
3741

3842
// started playing..
39-
source.addEventListener( 'play', this.onPlayStart.bind(this) );
40-
source.addEventListener( 'pause', this.onPlayStop.bind(this) );
43+
source.addEventListener('play', this.onPlayStart.bind(this));
44+
source.addEventListener('pause', this.onPlayStop.bind(this));
4145
}
4246

4347
};
4448

45-
PIXI.VideoTexture.prototype = Object.create( PIXI.BaseTexture.prototype );
46-
49+
PIXI.VideoTexture.prototype = Object.create(PIXI.BaseTexture.prototype);
4750
PIXI.VideoTexture.constructor = PIXI.VideoTexture;
4851

49-
PIXI.VideoTexture.prototype._onUpdate = function()
52+
PIXI.VideoTexture.prototype._onEnded = function()
5053
{
54+
this.ended = true;
55+
};
5156

52-
if(this.autoUpdate)
57+
PIXI.VideoTexture.prototype._onUpdate = function()
58+
{
59+
if (this.autoUpdate)
5360
{
5461
window.requestAnimationFrame(this.updateBound);
5562
this.dirty();
@@ -58,7 +65,7 @@ PIXI.VideoTexture.prototype._onUpdate = function()
5865

5966
PIXI.VideoTexture.prototype.onPlayStart = function()
6067
{
61-
if(!this.autoUpdate)
68+
if (!this.autoUpdate)
6269
{
6370
window.requestAnimationFrame(this.updateBound);
6471
this.autoUpdate = true;
@@ -72,34 +79,34 @@ PIXI.VideoTexture.prototype.onPlayStop = function()
7279

7380
PIXI.VideoTexture.prototype.onCanPlay = function()
7481
{
75-
if( event.type === 'canplaythrough' )
82+
if (event.type === 'canplaythrough')
7683
{
77-
this.hasLoaded = true;
78-
84+
this.hasLoaded = true;
7985

80-
if( this.source )
86+
if (this.source)
8187
{
82-
this.source.removeEventListener( 'canplay', this._onCanPlay );
83-
this.source.removeEventListener( 'canplaythrough', this._onCanPlay );
88+
this.source.removeEventListener('canplay', this._onCanPlay);
89+
this.source.removeEventListener('canplaythrough', this._onCanPlay);
8490

85-
this.width = this.source.videoWidth;
86-
this.height = this.source.videoHeight;
91+
this.width = this.source.videoWidth;
92+
this.height = this.source.videoHeight;
8793

8894
// prevent multiple loaded dispatches..
89-
if( !this.__loaded ){
95+
if (!this.__loaded )
96+
{
9097
this.__loaded = true;
91-
// this.dispatchEvent( { type: 'loaded', content: this } );
98+
this.dispatchEvent( { type: 'loaded', content: this } );
9299
}
93100
}
94101
}
95102
};
96103

97104
PIXI.VideoTexture.prototype.destroy = function()
98105
{
99-
if( this.source && this.source._pixiId )
106+
if (this.source && this.source._pixiId)
100107
{
101108
PIXI.BaseTextureCache[ this.source._pixiId ] = null;
102-
delete PIXI.BaseTextureCache[ this.source._pixiId ];
109+
delete PIXI.BaseTextureCache[this.source._pixiId];
103110

104111
this.source._pixiId = null;
105112
delete this.source._pixiId;
@@ -117,19 +124,19 @@ PIXI.VideoTexture.prototype.destroy = function()
117124
* @param scaleMode {Number} See {{#crossLink "PIXI/scaleModes:property"}}PIXI.scaleModes{{/crossLink}} for possible values
118125
* @return {VideoTexture}
119126
*/
120-
PIXI.VideoTexture.baseTextureFromVideo = function( video, scaleMode )
127+
PIXI.VideoTexture.baseTextureFromVideo = function(video, scaleMode)
121128
{
122-
if( !video._pixiId )
129+
if (!video._pixiId)
123130
{
124131
video._pixiId = 'video_' + PIXI.TextureCacheIdGenerator++;
125132
}
126133

127-
var baseTexture = PIXI.BaseTextureCache[ video._pixiId ];
134+
var baseTexture = PIXI.BaseTextureCache[video._pixiId];
128135

129-
if( !baseTexture )
136+
if (!baseTexture)
130137
{
131-
baseTexture = new PIXI.VideoTexture( video, scaleMode );
132-
PIXI.BaseTextureCache[ video._pixiId ] = baseTexture;
138+
baseTexture = new PIXI.VideoTexture(video, scaleMode);
139+
PIXI.BaseTextureCache[video._pixiId] = baseTexture;
133140
}
134141

135142
return baseTexture;
@@ -144,10 +151,11 @@ PIXI.VideoTexture.baseTextureFromVideo = function( video, scaleMode )
144151
* @param scaleMode {Number} See {{#crossLink "PIXI/scaleModes:property"}}PIXI.scaleModes{{/crossLink}} for possible values
145152
* @return {Texture} A Texture, but not a VideoTexture.
146153
*/
147-
PIXI.VideoTexture.textureFromVideo = function( video, scaleMode )
154+
PIXI.VideoTexture.textureFromVideo = function(video, scaleMode)
148155
{
149-
var baseTexture = PIXI.VideoTexture.baseTextureFromVideo( video, scaleMode );
150-
return new PIXI.Texture( baseTexture );
156+
var baseTexture = PIXI.VideoTexture.baseTextureFromVideo(video, scaleMode);
157+
158+
return new PIXI.Texture(baseTexture);
151159
};
152160

153161
/**
@@ -159,11 +167,13 @@ PIXI.VideoTexture.textureFromVideo = function( video, scaleMode )
159167
* @param scaleMode {Number} See {{#crossLink "PIXI/scaleModes:property"}}PIXI.scaleModes{{/crossLink}} for possible values
160168
* @return {VideoTexture}
161169
*/
162-
PIXI.VideoTexture.fromUrl = function( videoSrc, scaleMode )
170+
PIXI.VideoTexture.fromUrl = function(videoSrc, scaleMode, autoPlay)
163171
{
164172
var video = document.createElement('video');
173+
165174
video.src = videoSrc;
166175
video.autoPlay = true;
167176
video.play();
168-
return PIXI.VideoTexture.textureFromVideo( video, scaleMode);
177+
178+
return PIXI.VideoTexture.textureFromVideo(video, scaleMode);
169179
};

0 commit comments

Comments
 (0)