11//-----------------------------------------------------------------------------
2-
3- var waitForLoading = false ;
4- var register = false ;
5-
6- function handleiOSTouch ( ev ) {
7- if ( Graphics . _video . paused && Graphics . isVideoPlaying ( ) ) Graphics . _video . play ( ) ;
8- }
9-
102/**
113 * The static class that carries out graphics processing.
124 *
@@ -16,6 +8,10 @@ function Graphics() {
168 throw new Error ( 'This is a static class' ) ;
179}
1810
11+ Graphics . _cssFontLoading = document . fonts && document . fonts . ready ;
12+ Graphics . _fontLoaded = null ;
13+
14+
1915/**
2016 * Initializes the graphics system.
2117 *
@@ -39,6 +35,8 @@ Graphics.initialize = function(width, height, type) {
3935 this . _errorPrinter = null ;
4036 this . _canvas = null ;
4137 this . _video = null ;
38+ this . _videoUnlocked = ! Utils . isMobileDevice ( ) ;
39+ this . _videoLoading = false ;
4240 this . _upperCanvas = null ;
4341 this . _renderer = null ;
4442 this . _fpsMeter = null ;
@@ -62,6 +60,22 @@ Graphics.initialize = function(width, height, type) {
6260 this . _disableTextSelection ( ) ;
6361 this . _disableContextMenu ( ) ;
6462 this . _setupEventHandlers ( ) ;
63+ this . _setupCssFontLoading ( ) ;
64+ } ;
65+
66+
67+ Graphics . _setupCssFontLoading = function ( ) {
68+ if ( Graphics . _cssFontLoading ) {
69+ document . fonts . ready . then ( function ( fonts ) {
70+ Graphics . _fontLoaded = fonts ;
71+ } ) . catch ( function ( error ) {
72+ SceneManager . onError ( error ) ;
73+ } ) ;
74+ }
75+ } ;
76+
77+ Graphics . canUseCssFontLoading = function ( ) {
78+ return ! ! this . _cssFontLoading ;
6579} ;
6680
6781/**
@@ -323,17 +337,25 @@ Graphics.loadFont = function(name, url) {
323337 * @return {Boolean } True if the font file is loaded
324338 */
325339Graphics . isFontLoaded = function ( name ) {
326- if ( ! this . _hiddenCanvas ) {
327- this . _hiddenCanvas = document . createElement ( 'canvas' ) ;
340+ if ( Graphics . _cssFontLoading ) {
341+ if ( Graphics . _fontLoaded ) {
342+ return Graphics . _fontLoaded . check ( '10px "' + name + '"' ) ;
343+ }
344+
345+ return false ;
346+ } else {
347+ if ( ! this . _hiddenCanvas ) {
348+ this . _hiddenCanvas = document . createElement ( 'canvas' ) ;
349+ }
350+ var context = this . _hiddenCanvas . getContext ( '2d' ) ;
351+ var text = 'abcdefghijklmnopqrstuvwxyz' ;
352+ var width1 , width2 ;
353+ context . font = '40px ' + name + ', sans-serif' ;
354+ width1 = context . measureText ( text ) . width ;
355+ context . font = '40px sans-serif' ;
356+ width2 = context . measureText ( text ) . width ;
357+ return width1 !== width2 ;
328358 }
329- var context = this . _hiddenCanvas . getContext ( '2d' ) ;
330- var text = 'abcdefghijklmnopqrstuvwxyz' ;
331- var width1 , width2 ;
332- context . font = '40px ' + name + ', sans-serif' ;
333- width1 = context . measureText ( text ) . width ;
334- context . font = '40px sans-serif' ;
335- width2 = context . measureText ( text ) . width ;
336- return width1 !== width2 ;
337359} ;
338360
339361/**
@@ -349,14 +371,7 @@ Graphics.playVideo = function(src) {
349371 this . _video . onerror = this . _onVideoError . bind ( this ) ;
350372 this . _video . onended = this . _onVideoEnd . bind ( this ) ;
351373 this . _video . load ( ) ;
352-
353- if ( Utils . isMobileSafari ( ) ) {
354- waitForLoading = true ;
355- if ( ! register ) {
356- register = true ;
357- document . addEventListener ( 'touchstart' , handleiOSTouch ) ;
358- }
359- }
374+ this . _videoLoading = true ;
360375} ;
361376
362377/**
@@ -367,8 +382,7 @@ Graphics.playVideo = function(src) {
367382 * @return {Boolean } True if the video is playing
368383 */
369384Graphics . isVideoPlaying = function ( ) {
370- if ( Utils . isMobileSafari ( ) ) return waitForLoading || ( this . _video && this . _isVideoVisible ( ) ) ;
371- return this . _video && this . _isVideoVisible ( ) ;
385+ return this . _videoLoading || this . _isVideoVisible ( ) ;
372386} ;
373387
374388/**
@@ -707,7 +721,9 @@ Graphics._createVideo = function() {
707721 this . _video = document . createElement ( 'video' ) ;
708722 this . _video . id = 'GameVideo' ;
709723 this . _video . style . opacity = 0 ;
724+ this . _video . setAttribute ( 'playsinline' , '' ) ;
710725 this . _updateVideo ( ) ;
726+ makeVideoPlayableInline ( this . _video ) ;
711727 document . body . appendChild ( this . _video ) ;
712728} ;
713729
@@ -962,9 +978,7 @@ Graphics._applyCanvasFilter = function() {
962978Graphics . _onVideoLoad = function ( ) {
963979 this . _video . play ( ) ;
964980 this . _updateVisibility ( true ) ;
965- if ( Utils . isMobileSafari ( ) ) {
966- waitForLoading = false ;
967- }
981+ this . _videoLoading = false ;
968982} ;
969983
970984/**
@@ -974,6 +988,7 @@ Graphics._onVideoLoad = function() {
974988 */
975989Graphics . _onVideoError = function ( ) {
976990 this . _updateVisibility ( false ) ;
991+ this . _videoLoading = false ;
977992} ;
978993
979994/**
@@ -983,13 +998,6 @@ Graphics._onVideoError = function() {
983998 */
984999Graphics . _onVideoEnd = function ( ) {
9851000 this . _updateVisibility ( false ) ;
986-
987- if ( Utils . isMobileSafari ( ) ) {
988- if ( register ) {
989- document . removeEventListener ( 'touchstart' , handleiOSTouch ) ;
990- register = false ;
991- }
992- }
9931001} ;
9941002
9951003/**
@@ -1021,6 +1029,7 @@ Graphics._isVideoVisible = function() {
10211029Graphics . _setupEventHandlers = function ( ) {
10221030 window . addEventListener ( 'resize' , this . _onWindowResize . bind ( this ) ) ;
10231031 document . addEventListener ( 'keydown' , this . _onKeyDown . bind ( this ) ) ;
1032+ document . addEventListener ( 'touchend' , this . _onTouchEnd . bind ( this ) ) ;
10241033} ;
10251034
10261035/**
@@ -1057,6 +1066,22 @@ Graphics._onKeyDown = function(event) {
10571066 }
10581067} ;
10591068
1069+ /**
1070+ * @static
1071+ * @method _onTouchEnd
1072+ * @param {TouchEvent } event
1073+ * @private
1074+ */
1075+ Graphics . _onTouchEnd = function ( event ) {
1076+ if ( ! this . _videoUnlocked ) {
1077+ this . _video . play ( ) ;
1078+ this . _videoUnlocked = true ;
1079+ }
1080+ if ( this . _isVideoVisible ( ) && this . _video . paused ) {
1081+ this . _video . play ( ) ;
1082+ }
1083+ } ;
1084+
10601085/**
10611086 * @static
10621087 * @method _switchFPSMeter
0 commit comments