@@ -704,33 +704,47 @@ define([
704704 _animationComplete : function ( ) {
705705 logger . debug ( this . id + "._animationComplete" ) ;
706706 if ( this . base64Attr ) {
707- var ctx = this . canvasNode . getContext ( '2d' ) ;
708- var imgData = ctx . getImageData ( 0 , 0 , this . canvasNode . width , this . canvasNode . height ) ;
709- var data = imgData . data ;
710- // if the charttype is a donut, we have to fix up some translucency issues.
711- if ( this . _chartType === "doughnut" ) {
712- for ( var i = 0 ; i < data . length ; i += 4 ) {
713- if ( data [ i + 3 ] < 255 ) {
714- data [ i ] = 255 ;
715- data [ i + 1 ] = 255 ;
716- data [ i + 2 ] = 255 ;
717- data [ i + 3 ] = 255 ;
718- }
719- }
720- ctx . putImageData ( imgData , 0 , 0 ) ;
721- }
722- this . _mxObj . set ( this . base64Attr , this . _getBase64StringFromCanvasNode ( ) ) ;
707+ this . _mxObj . set ( this . base64Attr , this . _getBase64StringFromCanvasWithBackground ( "white" ) ) ;
723708
724709 }
725710 } ,
726711
727712 /**
728- * Get Base64 String From Canvas Node
729- * --
730- * @returns {String } - the base64 string of whatever's in the canvasnode
713+ * Get Base64 String From Canvas Node with Background
714+ * ---
715+ * @author Conner Charlebois
716+ * @since 10 Nov, 2017
717+ * @param {String } backgroundColor - CSS color for the background fill
718+ * @returns {String } - the base64 String with the background fill applied.
719+ * @see https://stackoverflow.com/a/44174406/1513051
731720 */
732- _getBase64StringFromCanvasNode : function ( ) {
733- return this . canvasNode . toDataURL ( this . _chartType === "doughnut" ? "image/jpeg" : "image/png" ) ;
721+ _getBase64StringFromCanvasWithBackground : function ( backgroundColor ) {
722+
723+ var context = this . canvasNode . getContext ( '2d' ) ;
724+ var canvas = context . canvas ;
725+ //cache height and width
726+ var w = canvas . width ;
727+ var h = canvas . height ;
728+ //get the current ImageData for the canvas.
729+ var data = context . getImageData ( 0 , 0 , w , h ) ;
730+ //store the current globalCompositeOperation
731+ var compositeOperation = context . globalCompositeOperation ;
732+ //set to draw behind current content
733+ context . globalCompositeOperation = "destination-over" ;
734+ //set background color
735+ context . fillStyle = backgroundColor ;
736+ //draw background / rect on entire canvas
737+ context . fillRect ( 0 , 0 , w , h ) ;
738+ //get the image data from the canvas
739+ var imageData = canvas . toDataURL ( "image/jpeg" ) ;
740+ //clear the canvas
741+ context . clearRect ( 0 , 0 , w , h ) ;
742+ //restore it with original / cached ImageData
743+ context . putImageData ( data , 0 , 0 ) ;
744+ //reset the globalCompositeOperation to what it was
745+ context . globalCompositeOperation = compositeOperation ;
746+
747+ return imageData ;
734748 }
735749
736750 } ) ;
0 commit comments