@@ -206,7 +206,7 @@ _html2canvas.Generate.parseGradient = function(css, bounds) {
206206 case '-o-radial-gradient' :
207207
208208 gradient = {
209- type : 'radial ' ,
209+ type : 'circle ' ,
210210 x0 : 0 ,
211211 y0 : 0 ,
212212 x1 : bounds . width ,
@@ -256,7 +256,7 @@ _html2canvas.Generate.parseGradient = function(css, bounds) {
256256 ) ;
257257 } else { // ellipse
258258
259- h2clog ( 'No ellipse gradient supported by now, cause canvas can´t draw ellipse :(' ) ;
259+ gradient . type = m2 [ 0 ] ;
260260
261261 gradient . rx = Math . max (
262262 gradient . cx ,
@@ -279,7 +279,7 @@ _html2canvas.Generate.parseGradient = function(css, bounds) {
279279 ) ;
280280 } else { // ellipse
281281
282- h2clog ( 'No ellipse gradient supported by now, cause canvas can´t draw ellipse :(' ) ;
282+ gradient . type = m2 [ 0 ] ;
283283
284284 gradient . rx = Math . min (
285285 gradient . cx ,
@@ -339,40 +339,75 @@ _html2canvas.Generate.Gradient = function(src, bounds) {
339339
340340 img = new Image ( ) ;
341341
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+ }
351353 }
352- }
354+
355+ ctx . fillStyle = grad ;
356+ ctx . fillRect ( 0 , 0 , bounds . width , bounds . height ) ;
353357
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+ }
366370 }
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+ }
369395 }
370- }
396+
397+ ctxRadial . fillStyle = grad ;
398+ ctxRadial . fillRect ( 0 , 0 , di , di ) ;
371399
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+ }
376411 }
377412
378413 return img ;
0 commit comments