@@ -206,7 +206,7 @@ _html2canvas.Generate.parseGradient = function(css, bounds) {
206
206
case '-o-radial-gradient' :
207
207
208
208
gradient = {
209
- type : 'radial ' ,
209
+ type : 'circle ' ,
210
210
x0 : 0 ,
211
211
y0 : 0 ,
212
212
x1 : bounds . width ,
@@ -256,7 +256,7 @@ _html2canvas.Generate.parseGradient = function(css, bounds) {
256
256
) ;
257
257
} else { // ellipse
258
258
259
- h2clog ( 'No ellipse gradient supported by now, cause canvas can´t draw ellipse :(' ) ;
259
+ gradient . type = m2 [ 0 ] ;
260
260
261
261
gradient . rx = Math . max (
262
262
gradient . cx ,
@@ -279,7 +279,7 @@ _html2canvas.Generate.parseGradient = function(css, bounds) {
279
279
) ;
280
280
} else { // ellipse
281
281
282
- h2clog ( 'No ellipse gradient supported by now, cause canvas can´t draw ellipse :(' ) ;
282
+ gradient . type = m2 [ 0 ] ;
283
283
284
284
gradient . rx = Math . min (
285
285
gradient . cx ,
@@ -339,40 +339,75 @@ _html2canvas.Generate.Gradient = function(src, bounds) {
339
339
340
340
img = new Image ( ) ;
341
341
342
- if ( gradient && gradient . type === 'linear' ) {
343
- grad = ctx . createLinearGradient ( gradient . x0 , gradient . y0 , gradient . x1 , gradient . y1 ) ;
344
-
345
- for ( i = 0 , len = gradient . colorStops . length ; i < len ; i += 1 ) {
346
- try {
347
- grad . addColorStop ( gradient . colorStops [ i ] . stop , gradient . colorStops [ i ] . color ) ;
348
- }
349
- catch ( e ) {
350
- 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
+ }
351
353
}
352
- }
354
+
355
+ ctx . fillStyle = grad ;
356
+ ctx . fillRect ( 0 , 0 , bounds . width , bounds . height ) ;
353
357
354
- ctx . fillStyle = grad ;
355
- ctx . fillRect ( 0 , 0 , bounds . width , bounds . height ) ;
356
-
357
- img . src = canvas . toDataURL ( ) ;
358
- } else if ( gradient && gradient . type === 'radial' ) {
359
-
360
- // TODO: Add support for "ellipsis" drawing
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 ) ;
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
+ }
366
370
}
367
- catch ( e ) {
368
- h2clog ( [ 'failed to add color stop: ' , e , '; tried to add: ' , gradient . colorStops [ i ] , '; stop: ' , i , '; in: ' , src ] ) ;
371
+
372
+ ctx . fillStyle = grad ;
373
+ ctx . fillRect ( 0 , 0 , bounds . width , bounds . height ) ;
374
+
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
+ }
369
395
}
370
- }
396
+
397
+ ctxRadial . fillStyle = grad ;
398
+ ctxRadial . fillRect ( 0 , 0 , di , di ) ;
371
399
372
- ctx . fillStyle = grad ;
373
- ctx . fillRect ( 0 , 0 , bounds . width , bounds . height ) ;
374
-
375
- img . src = canvas . toDataURL ( ) ;
400
+ imgRadial = new Image ( ) ;
401
+ imgRadial . src = canvasRadial . toDataURL ( ) ;
402
+
403
+ // transform circle to ellipse
404
+ ctx . fillStyle = gradient . colorStops [ i - 1 ] . color ;
405
+ ctx . fillRect ( 0 , 0 , canvas . width , canvas . height ) ;
406
+
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
+ }
376
411
}
377
412
378
413
return img ;
0 commit comments