@@ -27,6 +27,7 @@ var reGradients = [
27
27
/*
28
28
* TODO: Add IE10 vendor prefix (-ms) support
29
29
* TODO: Add W3C gradient (linear-gradient) support
30
+ * TODO: Add old Webkit -webkit-gradient(radial, ...) support
30
31
* TODO: Maybe some RegExp optimizations are possible ;o)
31
32
*/
32
33
_html2canvas . Generate . parseGradient = function ( css , bounds ) {
@@ -114,7 +115,7 @@ _html2canvas.Generate.parseGradient = function(css, bounds) {
114
115
case '-webkit-gradient' :
115
116
116
117
gradient = {
117
- type : m1 [ 2 ] ,
118
+ type : m1 [ 2 ] === 'radial' ? 'circle' : m1 [ 2 ] , // TODO: Add radial gradient support for older mozilla definitions
118
119
x0 : 0 ,
119
120
y0 : 0 ,
120
121
x1 : 0 ,
@@ -205,7 +206,7 @@ _html2canvas.Generate.parseGradient = function(css, bounds) {
205
206
case '-o-radial-gradient' :
206
207
207
208
gradient = {
208
- type : 'radial ' ,
209
+ type : 'circle ' ,
209
210
x0 : 0 ,
210
211
y0 : 0 ,
211
212
x1 : bounds . width ,
@@ -255,7 +256,7 @@ _html2canvas.Generate.parseGradient = function(css, bounds) {
255
256
) ;
256
257
} else { // ellipse
257
258
258
- h2clog ( 'No ellipse gradient supported by now, cause canvas can´t draw ellipse :(' ) ;
259
+ gradient . type = m2 [ 0 ] ;
259
260
260
261
gradient . rx = Math . max (
261
262
gradient . cx ,
@@ -278,7 +279,7 @@ _html2canvas.Generate.parseGradient = function(css, bounds) {
278
279
) ;
279
280
} else { // ellipse
280
281
281
- h2clog ( 'No ellipse gradient supported by now, cause canvas can´t draw ellipse :(' ) ;
282
+ gradient . type = m2 [ 0 ] ;
282
283
283
284
gradient . rx = Math . min (
284
285
gradient . cx ,
@@ -338,40 +339,78 @@ _html2canvas.Generate.Gradient = function(src, bounds) {
338
339
339
340
img = new Image ( ) ;
340
341
341
- if ( gradient && gradient . type === 'linear' ) {
342
- grad = ctx . createLinearGradient ( gradient . x0 , gradient . y0 , gradient . x1 , gradient . y1 ) ;
343
-
344
- for ( i = 0 , len = gradient . colorStops . length ; i < len ; i += 1 ) {
345
- try {
346
- grad . addColorStop ( gradient . colorStops [ i ] . stop , gradient . colorStops [ i ] . color ) ;
347
- }
348
- catch ( e ) {
349
- h2clog ( [ 'failed to add color stop: ' , e , '; tried to add: ' , gradient . colorStops [ i ] , '; stop: ' , i , '; in: ' , src ] ) ;
342
+ if ( gradient ) {
343
+ if ( gradient . type === 'linear' ) {
344
+ grad = ctx . createLinearGradient ( gradient . x0 , gradient . y0 , gradient . x1 , gradient . y1 ) ;
345
+
346
+ for ( i = 0 , len = gradient . colorStops . length ; i < len ; i += 1 ) {
347
+ try {
348
+ grad . addColorStop ( gradient . colorStops [ i ] . stop , gradient . colorStops [ i ] . color ) ;
349
+ }
350
+ catch ( e ) {
351
+ h2clog ( [ 'failed to add color stop: ' , e , '; tried to add: ' , gradient . colorStops [ i ] , '; stop: ' , i , '; in: ' , src ] ) ;
352
+ }
350
353
}
351
- }
354
+
355
+ ctx . fillStyle = grad ;
356
+ ctx . fillRect ( 0 , 0 , bounds . width , bounds . height ) ;
352
357
353
- ctx . fillStyle = grad ;
354
- ctx . fillRect ( 0 , 0 , bounds . width , bounds . height ) ;
355
-
356
- img . src = canvas . toDataURL ( ) ;
357
- } else if ( gradient && gradient . type === 'radial' ) {
358
-
359
- // TODO: Add support for "ellipsis" drawing
360
- grad = ctx . createRadialGradient ( gradient . cx , gradient . cy , 0 , gradient . cx , gradient . cy , gradient . rx ) ;
358
+ img . src = canvas . toDataURL ( ) ;
359
+ } else if ( gradient . type === 'circle' ) {
360
+
361
+ grad = ctx . createRadialGradient ( gradient . cx , gradient . cy , 0 , gradient . cx , gradient . cy , gradient . rx ) ;
362
+
363
+ for ( i = 0 , len = gradient . colorStops . length ; i < len ; i += 1 ) {
364
+ try {
365
+ grad . addColorStop ( gradient . colorStops [ i ] . stop , gradient . colorStops [ i ] . color ) ;
366
+ }
367
+ catch ( e ) {
368
+ h2clog ( [ 'failed to add color stop: ' , e , '; tried to add: ' , gradient . colorStops [ i ] , '; stop: ' , i , '; in: ' , src ] ) ;
369
+ }
370
+ }
371
+
372
+ ctx . fillStyle = grad ;
373
+ ctx . fillRect ( 0 , 0 , bounds . width , bounds . height ) ;
361
374
362
- for ( i = 0 , len = gradient . colorStops . length ; i < len ; i += 1 ) {
363
- try {
364
- grad . addColorStop ( gradient . colorStops [ i ] . stop , gradient . colorStops [ i ] . color ) ;
375
+ img . src = canvas . toDataURL ( ) ;
376
+ } else if ( gradient . type === 'ellipse' ) {
377
+
378
+ // draw circle
379
+ var canvasRadial = document . createElement ( 'canvas' ) ,
380
+ ctxRadial = canvasRadial . getContext ( '2d' ) ,
381
+ ri = Math . max ( gradient . rx , gradient . ry ) ,
382
+ di = ri * 2 , imgRadial ;
383
+
384
+ canvasRadial . width = canvasRadial . height = di ;
385
+
386
+ grad = ctxRadial . createRadialGradient ( gradient . rx , gradient . ry , 0 , gradient . rx , gradient . ry , ri ) ;
387
+
388
+ for ( i = 0 , len = gradient . colorStops . length ; i < len ; i += 1 ) {
389
+ try {
390
+ grad . addColorStop ( gradient . colorStops [ i ] . stop , gradient . colorStops [ i ] . color ) ;
391
+ }
392
+ catch ( e ) {
393
+ h2clog ( [ 'failed to add color stop: ' , e , '; tried to add: ' , gradient . colorStops [ i ] , '; stop: ' , i , '; in: ' , src ] ) ;
394
+ }
365
395
}
366
- catch ( e ) {
367
- h2clog ( [ 'failed to add color stop: ' , e , '; tried to add: ' , gradient . colorStops [ i ] , '; stop: ' , i , '; in: ' , src ] ) ;
396
+
397
+ ctxRadial . fillStyle = grad ;
398
+ ctxRadial . fillRect ( 0 , 0 , di , di ) ;
399
+
400
+ ctx . fillStyle = gradient . colorStops [ i - 1 ] . color ;
401
+ ctx . fillRect ( 0 , 0 , canvas . width , canvas . height ) ;
402
+
403
+ imgRadial = new Image ( ) ;
404
+ imgRadial . onload = function ( ) { // wait until the image is filled
405
+
406
+ // transform circle to ellipse
407
+ ctx . drawImage ( imgRadial , gradient . cx - gradient . rx , gradient . cy - gradient . ry , 2 * gradient . rx , 2 * gradient . ry ) ;
408
+
409
+ img . src = canvas . toDataURL ( ) ;
410
+
368
411
}
412
+ imgRadial . src = canvasRadial . toDataURL ( ) ;
369
413
}
370
-
371
- ctx . fillStyle = grad ;
372
- ctx . fillRect ( 0 , 0 , bounds . width , bounds . height ) ;
373
-
374
- img . src = canvas . toDataURL ( ) ;
375
414
}
376
415
377
416
return img ;
0 commit comments