9
9
10
10
'use strict' ;
11
11
12
+ var isNumeric = require ( 'fast-isnumeric' ) ;
13
+
12
14
var glPlot3d = require ( 'gl-plot3d' ) ;
13
15
var createCamera = glPlot3d . createCamera ;
14
16
var createPlot = glPlot3d . createScene ;
@@ -30,7 +32,48 @@ var createAxesOptions = require('./layout/convert');
30
32
var createSpikeOptions = require ( './layout/spikes' ) ;
31
33
var computeTickMarks = require ( './layout/tick_marks' ) ;
32
34
33
- var isMobile = require ( 'is-mobile' ) ( { tablet : true , featureDetect : true } ) ;
35
+ var isMobileOrTablet = require ( 'is-mobile' ) ( { tablet : true , featureDetect : true } ) ;
36
+ var preserveDrawingBuffer = handleSafari14 ( isMobileOrTablet ) ;
37
+
38
+ function handleSafari14 ( hasDrawingBuffer ) {
39
+ if ( ! hasDrawingBuffer ) {
40
+ var ua = getUserAgent ( ) ;
41
+ if ( typeof ua !== 'string' ) return false ;
42
+
43
+ var allParts = ua . split ( '/' ) ;
44
+ for ( var i = 0 ; i < allParts . length ; i ++ ) {
45
+ var part = allParts [ i ] ;
46
+ if ( part . indexOf ( 'Safari' ) !== - 1 ) {
47
+ // find Safari version
48
+ var v = part . split ( '.' ) [ 0 ] ;
49
+ if ( isNumeric ( v ) ) v = + v ;
50
+
51
+ // to fix https://github.com/plotly/plotly.js/issues/5158
52
+ if ( v >= 14 ) return true ;
53
+ }
54
+ }
55
+ }
56
+
57
+ return hasDrawingBuffer ;
58
+ }
59
+
60
+ function getUserAgent ( ) {
61
+ // similar to https://github.com/juliangruber/is-mobile/blob/91ca39ccdd4cfc5edfb5391e2515b923a730fbea/index.js#L14-L17
62
+ var ua ;
63
+ if ( typeof navigator !== 'undefined' ) {
64
+ ua = navigator . userAgent ;
65
+ }
66
+
67
+ if (
68
+ ua &&
69
+ ua . headers &&
70
+ typeof ua . headers [ 'user-agent' ] === 'string'
71
+ ) {
72
+ ua = ua . headers [ 'user-agent' ] ;
73
+ }
74
+
75
+ return ua ;
76
+ }
34
77
35
78
36
79
var STATIC_CANVAS , STATIC_CONTEXT ;
@@ -98,7 +141,7 @@ proto.prepareOptions = function() {
98
141
canvas : scene . canvas ,
99
142
gl : scene . gl ,
100
143
glOptions : {
101
- preserveDrawingBuffer : isMobile ,
144
+ preserveDrawingBuffer : preserveDrawingBuffer ,
102
145
premultipliedAlpha : true ,
103
146
antialias : true
104
147
} ,
@@ -155,19 +198,19 @@ proto.tryCreatePlot = function() {
155
198
// invert preserveDrawingBuffer setup which could be resulted from is-mobile not detecting the right device
156
199
Lib . warn ( [
157
200
'webgl setup failed possibly due to' ,
158
- isMobile ? 'disabling' : 'enabling' ,
201
+ preserveDrawingBuffer ? 'disabling' : 'enabling' ,
159
202
'preserveDrawingBuffer config.' ,
160
203
'The device may not be supported by is-mobile module!' ,
161
204
'Inverting preserveDrawingBuffer option in second attempt to create webgl scene.'
162
205
] . join ( ' ' ) ) ;
163
206
164
- // invert is-mobile
165
- isMobile = opts . glOptions . preserveDrawingBuffer = ! opts . glOptions . preserveDrawingBuffer ;
207
+ // invert preserveDrawingBuffer
208
+ preserveDrawingBuffer = opts . glOptions . preserveDrawingBuffer = ! opts . glOptions . preserveDrawingBuffer ;
166
209
167
210
scene . glplot = createPlot ( opts ) ;
168
211
} catch ( e ) {
169
- // revert changes to is-mobile
170
- isMobile = opts . glOptions . preserveDrawingBuffer = ! opts . glOptions . preserveDrawingBuffer ;
212
+ // revert changes to preserveDrawingBuffer
213
+ preserveDrawingBuffer = opts . glOptions . preserveDrawingBuffer = ! opts . glOptions . preserveDrawingBuffer ;
171
214
172
215
success = false ;
173
216
}
0 commit comments