@@ -76,84 +76,86 @@ p5.prototype.loadImage = function(path, successCallback, failureCallback) {
7676 mode : 'cors'
7777 } ) ;
7878
79- fetch ( path , req ) . then ( response => {
80- // GIF section
81- const contentType = response . headers . get ( 'content-type' ) ;
82- if ( contentType === null ) {
83- console . warn (
84- 'The image you loaded does not have a Content-Type header. If you are using the online editor consider reuploading the asset.'
85- ) ;
86- }
87- if ( contentType && contentType . includes ( 'image/gif' ) ) {
88- response . arrayBuffer ( ) . then (
89- arrayBuffer => {
90- if ( arrayBuffer ) {
91- const byteArray = new Uint8Array ( arrayBuffer ) ;
92- _createGif (
93- byteArray ,
94- pImg ,
95- successCallback ,
96- failureCallback ,
97- ( pImg => {
98- self . _decrementPreload ( ) ;
99- } ) . bind ( self )
100- ) ;
79+ fetch ( path , req )
80+ . then ( response => {
81+ // GIF section
82+ const contentType = response . headers . get ( 'content-type' ) ;
83+ if ( contentType === null ) {
84+ console . warn (
85+ 'The image you loaded does not have a Content-Type header. If you are using the online editor consider reuploading the asset.'
86+ ) ;
87+ }
88+ if ( contentType && contentType . includes ( 'image/gif' ) ) {
89+ response . arrayBuffer ( ) . then (
90+ arrayBuffer => {
91+ if ( arrayBuffer ) {
92+ const byteArray = new Uint8Array ( arrayBuffer ) ;
93+ _createGif (
94+ byteArray ,
95+ pImg ,
96+ successCallback ,
97+ failureCallback ,
98+ ( pImg => {
99+ self . _decrementPreload ( ) ;
100+ } ) . bind ( self )
101+ ) ;
102+ }
103+ } ,
104+ e => {
105+ if ( typeof failureCallback === 'function' ) {
106+ failureCallback ( e ) ;
107+ } else {
108+ console . error ( e ) ;
109+ }
101110 }
102- } ,
103- e => {
111+ ) ;
112+ } else {
113+ // Non-GIF Section
114+ const img = new Image ( ) ;
115+
116+ img . onload = ( ) => {
117+ pImg . width = pImg . canvas . width = img . width ;
118+ pImg . height = pImg . canvas . height = img . height ;
119+
120+ // Draw the image into the backing canvas of the p5.Image
121+ pImg . drawingContext . drawImage ( img , 0 , 0 ) ;
122+ pImg . modified = true ;
123+ if ( typeof successCallback === 'function' ) {
124+ successCallback ( pImg ) ;
125+ }
126+ self . _decrementPreload ( ) ;
127+ } ;
128+
129+ img . onerror = e => {
130+ p5 . _friendlyFileLoadError ( 0 , img . src ) ;
104131 if ( typeof failureCallback === 'function' ) {
105132 failureCallback ( e ) ;
106133 } else {
107134 console . error ( e ) ;
108135 }
136+ } ;
137+
138+ // Set crossOrigin in case image is served with CORS headers.
139+ // This will let us draw to the canvas without tainting it.
140+ // See https://developer.mozilla.org/en-US/docs/HTML/CORS_Enabled_Image
141+ // When using data-uris the file will be loaded locally
142+ // so we don't need to worry about crossOrigin with base64 file types.
143+ if ( path . indexOf ( 'data:image/' ) !== 0 ) {
144+ img . crossOrigin = 'Anonymous' ;
109145 }
110- ) ;
111- } else {
112- // Non-GIF Section
113- const img = new Image ( ) ;
114-
115- img . onload = ( ) => {
116- pImg . width = pImg . canvas . width = img . width ;
117- pImg . height = pImg . canvas . height = img . height ;
118-
119- // Draw the image into the backing canvas of the p5.Image
120- pImg . drawingContext . drawImage ( img , 0 , 0 ) ;
121- pImg . modified = true ;
122- if ( typeof successCallback === 'function' ) {
123- successCallback ( pImg ) ;
124- }
125- self . _decrementPreload ( ) ;
126- } ;
127-
128- img . onerror = e => {
129- p5 . _friendlyFileLoadError ( 0 , img . src ) ;
130- if ( typeof failureCallback === 'function' ) {
131- failureCallback ( e ) ;
132- } else {
133- console . error ( e ) ;
134- }
135- } ;
136-
137- // Set crossOrigin in case image is served with CORS headers.
138- // This will let us draw to the canvas without tainting it.
139- // See https://developer.mozilla.org/en-US/docs/HTML/CORS_Enabled_Image
140- // When using data-uris the file will be loaded locally
141- // so we don't need to worry about crossOrigin with base64 file types.
142- if ( path . indexOf ( 'data:image/' ) !== 0 ) {
143- img . crossOrigin = 'Anonymous' ;
146+ // start loading the image
147+ img . src = path ;
144148 }
145- // start loading the image
146- img . src = path ;
147- }
148- pImg . modified = true ;
149- } ) . catch ( e => {
150- p5 . _friendlyFileLoadError ( 0 , path ) ;
151- if ( typeof failureCallback === 'function' ) {
152- failureCallback ( e ) ;
153- } else {
154- console . error ( e ) ;
155- }
156- } )
149+ pImg . modified = true ;
150+ } )
151+ . catch ( e => {
152+ p5 . _friendlyFileLoadError ( 0 , path ) ;
153+ if ( typeof failureCallback === 'function' ) {
154+ failureCallback ( e ) ;
155+ } else {
156+ console . error ( e ) ;
157+ }
158+ } ) ;
157159 return pImg ;
158160} ;
159161
0 commit comments