@@ -235,9 +235,8 @@ function parseBackgroundSizePosition(value, element, attribute, index) {
235
235
value = ( value || '' ) . split ( ',' ) ;
236
236
value = value [ index || 0 ] || value [ 0 ] || 'auto' ;
237
237
value = _html2canvas . Util . trimText ( value ) . split ( ' ' ) ;
238
-
239
- if ( attribute === 'backgroundSize' && ( ! value [ 0 ] || value [ 0 ] . match ( / c o v e r | c o n t a i n | a u t o / ) ) ) {
240
- //these values will be handled in the parent function
238
+ if ( attribute === 'backgroundSize' && ( value [ 0 ] && value [ 0 ] . match ( / ^ ( c o v e r | c o n t a i n | a u t o ) $ / ) ) ) {
239
+ return value ;
241
240
} else {
242
241
value [ 0 ] = ( value [ 0 ] . indexOf ( "%" ) === - 1 ) ? toPX ( element , attribute + "X" , value [ 0 ] ) : value [ 0 ] ;
243
242
if ( value [ 1 ] === undefined ) {
@@ -296,71 +295,71 @@ _html2canvas.Util.resizeBounds = function( current_width, current_height, target
296
295
} ;
297
296
} ;
298
297
299
- function backgroundBoundsFactory ( prop , el , bounds , image , imageIndex , backgroundSize ) {
300
- var bgposition = _html2canvas . Util . getCSS ( el , prop , imageIndex ) ,
301
- topPos ,
302
- left ,
303
- percentage ,
304
- val ;
298
+ _html2canvas . Util . BackgroundPosition = function ( element , bounds , image , imageIndex , backgroundSize ) {
299
+ var backgroundPosition = _html2canvas . Util . getCSS ( element , 'backgroundPosition' , imageIndex ) ,
300
+ leftPosition ,
301
+ topPosition ;
302
+ if ( backgroundPosition . length === 1 ) {
303
+ backgroundPosition = [ backgroundPosition [ 0 ] , backgroundPosition [ 0 ] ] ;
304
+ }
305
305
306
- if ( bgposition . length === 1 ) {
307
- val = bgposition [ 0 ] ;
306
+ if ( backgroundPosition [ 0 ] . toString ( ) . indexOf ( "%" ) !== - 1 ) {
307
+ leftPosition = ( bounds . width - ( backgroundSize || image ) . width ) * ( parseFloat ( backgroundPosition [ 0 ] ) / 100 ) ;
308
+ } else {
309
+ leftPosition = parseInt ( backgroundPosition [ 0 ] , 10 ) ;
310
+ }
308
311
309
- bgposition = [ ] ;
312
+ if ( backgroundPosition [ 1 ] === 'auto' ) {
313
+ topPosition = leftPosition / image . width * image . height ;
314
+ } else if ( backgroundPosition [ 1 ] . toString ( ) . indexOf ( "%" ) !== - 1 ) {
315
+ topPosition = ( bounds . height - ( backgroundSize || image ) . height ) * parseFloat ( backgroundPosition [ 1 ] ) / 100 ;
316
+ } else {
317
+ topPosition = parseInt ( backgroundPosition [ 1 ] , 10 ) ;
318
+ }
310
319
311
- bgposition [ 0 ] = val ;
312
- bgposition [ 1 ] = val ;
320
+ if ( backgroundPosition [ 0 ] === 'auto' ) {
321
+ leftPosition = topPosition / image . height * image . width ;
313
322
}
314
323
315
- if ( bgposition [ 0 ] . toString ( ) . indexOf ( "%" ) !== - 1 ) {
316
- percentage = ( parseFloat ( bgposition [ 0 ] ) / 100 ) ;
317
- left = bounds . width * percentage ;
318
- if ( prop !== 'backgroundSize' ) {
319
- left -= ( backgroundSize || image ) . width * percentage ;
320
- }
324
+ return { left : leftPosition , top : topPosition } ;
325
+ } ;
326
+
327
+ _html2canvas . Util . BackgroundSize = function ( element , bounds , image , imageIndex ) {
328
+ var backgroundSize = _html2canvas . Util . getCSS ( element , 'backgroundSize' , imageIndex ) ,
329
+ width ,
330
+ height ;
331
+
332
+ if ( backgroundSize . length === 1 ) {
333
+ backgroundSize = [ backgroundSize [ 0 ] , backgroundSize [ 0 ] ] ;
334
+ }
335
+
336
+ if ( backgroundSize [ 0 ] . toString ( ) . indexOf ( "%" ) !== - 1 ) {
337
+ width = bounds . width * parseFloat ( backgroundSize [ 0 ] ) / 100 ;
338
+ } else if ( backgroundSize [ 0 ] === 'auto' ) {
339
+ width = image . width ;
321
340
} else {
322
- if ( prop === ' backgroundSize' ) {
323
- if ( bgposition [ 0 ] === 'auto' ) {
324
- left = image . width ;
341
+ if ( / c o n t a i n | c o v e r / . test ( backgroundSize [ 0 ] ) ) {
342
+ var resized = _html2canvas . Util . resizeBounds ( image . width , image . height , bounds . width , bounds . height , backgroundSize [ 0 ] ) ;
343
+ return { width : resized . width , height : resized . height } ;
325
344
} else {
326
- if ( / c o n t a i n | c o v e r / . test ( bgposition [ 0 ] ) ) {
327
- var resized = _html2canvas . Util . resizeBounds ( image . width , image . height , bounds . width , bounds . height , bgposition [ 0 ] ) ;
328
- left = resized . width ;
329
- topPos = resized . height ;
330
- } else {
331
- left = parseInt ( bgposition [ 0 ] , 10 ) ;
332
- }
345
+ width = parseInt ( backgroundSize [ 0 ] , 10 ) ;
333
346
}
334
- } else {
335
- left = parseInt ( bgposition [ 0 ] , 10 ) ;
336
- }
337
347
}
338
348
339
-
340
- if ( bgposition [ 1 ] === 'auto' ) {
341
- topPos = left / image . width * image . height ;
342
- } else if ( bgposition [ 1 ] . toString ( ) . indexOf ( "%" ) !== - 1 ) {
343
- percentage = ( parseFloat ( bgposition [ 1 ] ) / 100 ) ;
344
- topPos = bounds . height * percentage ;
345
- if ( prop !== 'backgroundSize' ) {
346
- topPos -= ( backgroundSize || image ) . height * percentage ;
347
- }
348
-
349
+ if ( backgroundSize [ 1 ] === 'auto' ) {
350
+ height = width / image . width * image . height ;
351
+ } else if ( backgroundSize [ 1 ] . toString ( ) . indexOf ( "%" ) !== - 1 ) {
352
+ height = bounds . height * parseFloat ( backgroundSize [ 1 ] ) / 100 ;
349
353
} else {
350
- topPos = parseInt ( bgposition [ 1 ] , 10 ) ;
354
+ height = parseInt ( backgroundSize [ 1 ] , 10 ) ;
351
355
}
352
356
353
- return [ left , topPos ] ;
354
- }
355
357
356
- _html2canvas . Util . BackgroundPosition = function ( el , bounds , image , imageIndex , backgroundSize ) {
357
- var result = backgroundBoundsFactory ( 'backgroundPosition' , el , bounds , image , imageIndex , backgroundSize ) ;
358
- return { left : result [ 0 ] , top : result [ 1 ] } ;
359
- } ;
358
+ if ( backgroundSize [ 0 ] === 'auto' ) {
359
+ width = height / image . height * image . width ;
360
+ }
360
361
361
- _html2canvas . Util . BackgroundSize = function ( el , bounds , image , imageIndex ) {
362
- var result = backgroundBoundsFactory ( 'backgroundSize' , el , bounds , image , imageIndex ) ;
363
- return { width : result [ 0 ] , height : result [ 1 ] } ;
362
+ return { width : width , height : height } ;
364
363
} ;
365
364
366
365
_html2canvas . Util . Extend = function ( options , defaults ) {
@@ -415,7 +414,7 @@ _html2canvas.Util.Children = function( elem ) {
415
414
} ;
416
415
417
416
_html2canvas . Util . isTransparent = function ( backgroundColor ) {
418
- return ( backgroundColor === "transparent" || backgroundColor === "rgba(0, 0, 0, 0)" ) ;
417
+ return ( ! backgroundColor || backgroundColor === "transparent" || backgroundColor === "rgba(0, 0, 0, 0)" ) ;
419
418
} ;
420
419
_html2canvas . Util . Font = ( function ( ) {
421
420
@@ -2945,7 +2944,7 @@ _html2canvas.Renderer.Canvas = function(options) {
2945
2944
canvas . height = canvas . style . height = options . height || zStack . ctx . height ;
2946
2945
2947
2946
fstyle = ctx . fillStyle ;
2948
- ctx . fillStyle = ( Util . isTransparent ( zStack . backgroundColor ) && options . background !== undefined ) ? options . background : parsedData . backgroundColor ;
2947
+ ctx . fillStyle = ( Util . isTransparent ( parsedData . backgroundColor ) && options . background !== undefined ) ? options . background : parsedData . backgroundColor ;
2949
2948
ctx . fillRect ( 0 , 0 , canvas . width , canvas . height ) ;
2950
2949
ctx . fillStyle = fstyle ;
2951
2950
0 commit comments