Commits

BeezyT committed f2ddd94

canvas manager improvements and ie compatibility

  • Participants
  • Parent commits d951cea

Comments (0)

Files changed (4)

src/jqplot.canvasGridRenderer.js

           this._elem = null;
         }
       
-        // don't use the canvas manager with excanvas.
-        if ($.jqplot.use_excanvas) {
-          elem = document.createElement('canvas');
-        }
-        else {
-          elem = plot.canvasManager.getCanvas();
-        }
+        elem = plot.canvasManager.getCanvas();
 
         var w = this._plotDimensions.width;
         var h = this._plotDimensions.height;
         this._elem = $(elem);
         this._elem.addClass('jqplot-grid-canvas');
         this._elem.css({ position: 'absolute', left: 0, top: 0 });
-        //if ($.jqplot.use_excanvas) {
-        //    window.G_vmlCanvasManager.init_(document);
-        //}
-        if ($.jqplot.use_excanvas) {
-            elem = window.G_vmlCanvasManager.initElement(elem);
-        }
+        
+		elem = plot.canvasManager.initCanvas(elem);
+		
         this._top = this._offsets.top;
         this._bottom = h - this._offsets.bottom;
         this._left = this._offsets.left;

src/jqplot.core.js

 			$.jqplot.CanvasManager.free = [];
 		}
 		
-		var canvases = $.jqplot.CanvasManager.canvases;
-		var free = $.jqplot.CanvasManager.free;
+		var myCanvases = [];
         
         this.getCanvas = function() {
-            var canvas;
+			var canvas;
             var makeNew = true;
 
-            for (var i=0, l=canvases.length; i<l; i++) {
-                if (free[i] === true) {
+           for (var i=0, l=$.jqplot.CanvasManager.canvases.length; i<l; i++) {
+                if ($.jqplot.CanvasManager.free[i] === true) {
                     makeNew = false;
-                    canvas = canvases[i];
-                    free[i] = false;
+                    canvas = $.jqplot.CanvasManager.canvases[i];
+                    $.jqplot.CanvasManager.free[i] = false;
+					myCanvases.push(i);
                     break;
                 }
-            }   
+            }
 
             if (makeNew) {
                 canvas = document.createElement('canvas');
-                canvases.push(canvas);
-                free.push(false);
+				myCanvases.push($.jqplot.CanvasManager.canvases.length);
+				$.jqplot.CanvasManager.canvases.push(canvas);
+                $.jqplot.CanvasManager.free.push(false);
             }   
             
             return canvas;
         };
+		
+		// this method has to be used after settings the dimesions
+		// on the element returned by getCanvas()
+		this.initCanvas = function(canvas) {
+			if ($.jqplot.use_excanvas) {
+				return window.G_vmlCanvasManager.initElement(canvas);
+			}
+			return canvas;
+		}
 
         this.freeAllCanvases = function() {
-            for (var i = 0; i < canvases.length; i++) {
-                this.freeCanvas(i);
+			for (var i = 0; i < myCanvases.length; i++) {
+				this.freeCanvas(myCanvases[i]);
             }
-            canvas = null;
+			myCanvases = [];
         };
 
         this.freeCanvas = function(idx) {
-            var canvas = canvases[idx];
-            canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height);
-            $(canvas).removeClass().removeAttr('style');
-            free[idx] = true;
+			if ($.jqplot.use_excanvas) {
+				// excanvas can't be reused, but properly unset
+				window.G_vmlCanvasManager.uninitElement($.jqplot.CanvasManager.canvases[idx]);
+				$.jqplot.CanvasManager.canvases[idx] = null;
+			} else {
+				var canvas = $.jqplot.CanvasManager.canvases[idx];
+				canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height);
+				$(canvas).unbind().removeAttr('class').removeAttr('style')
+						.removeAttr('width').removeAttr('height');
+				$.jqplot.CanvasManager.free[idx] = true;
+			}
         };
 		
     };
     // http://www.modernizr.com
     
     $.jqplot.support_canvas = function() {
-        return !!document.createElement('canvas').getContext;
+		if (typeof $.jqplot.support_canvas.result == 'undefined') {
+			$.jqplot.support_canvas.result = !!document.createElement('canvas').getContext; 
+		}
+        return $.jqplot.support_canvas.result;
     };
             
     $.jqplot.support_canvas_text = function() {
-        return !!(document.createElement('canvas').getContext && typeof document.createElement('canvas').getContext('2d').fillText == 'function');
+		if (typeof $.jqplot.support_canvas_text.result == 'undefined') {
+			$.jqplot.support_canvas_text.result = !!(document.createElement('canvas').getContext && typeof document.createElement('canvas').getContext('2d').fillText == 'function'); 
+		}
+        return $.jqplot.support_canvas_text.result;
     };
     
     $.jqplot.use_excanvas = ($.browser.msie && !$.jqplot.support_canvas()) ? true : false;
 
         // }
 
-        // don't use the canvas manager with excanvas.
-        if ($.jqplot.use_excanvas) {
-            window.G_vmlCanvasManager.uninitElement(elem);
-            elem = document.createElement('canvas');
-        }
-        else {
-            elem = plot.canvasManager.getCanvas();
-        }
-
+        elem = plot.canvasManager.getCanvas();
+		
         // if new plotDimensions supplied, use them.
         if (plotDimensions != null) {
             this._plotDimensions = plotDimensions;
         this._elem.css({ position: 'absolute', left: this._offsets.left, top: this._offsets.top });
         
         this._elem.addClass(klass);
-        if ($.jqplot.use_excanvas) {
-            // useless ?? window.G_vmlCanvasManager.init_(document);
-            elem = window.G_vmlCanvasManager.initElement(elem);
-        }
-        // avoid memory leak
+        
+		elem = plot.canvasManager.initCanvas(elem);
+		
         elem = null;
         return this._elem;
     };
 		// Releases all resources occupied by the plot
 		this.destroy = function() {
 			this.canvasManager.freeAllCanvases();
-			this.target.empty();
+			this.target[0].innerHTML = '';
 		};
         
         // method: replot

src/plugins/jqplot.canvasAxisLabelRenderer.js

             ropts.pt2px = this.pt2px;
         }
         
-        if (this.enableFontSupport) {
-            
-            function support_canvas_text() {
-                return !!(document.createElement('canvas').getContext && typeof document.createElement('canvas').getContext('2d').fillText == 'function');
-            }
-            
-            if (support_canvas_text()) {
-                this._textRenderer = new $.jqplot.CanvasFontRenderer(ropts);
-            }
-            
-            else {
-                this._textRenderer = new $.jqplot.CanvasTextRenderer(ropts); 
-            }
-        }
-        else {
-            this._textRenderer = new $.jqplot.CanvasTextRenderer(ropts); 
-        }
+        this._textRenderer = new $.jqplot.CanvasTextRenderer(ropts);  
     };
     
     $.jqplot.CanvasAxisLabelRenderer.prototype.init = function(options) {
 
         // create a canvas here, but can't draw on it untill it is appended
         // to dom for IE compatability.
-
-        var elem;
-
-        // don't use the canvas manager with excanvas.
-        if ($.jqplot.use_excanvas) {
-            window.G_vmlCanvasManager.uninitElement(elem);
-            elem = document.createElement('canvas');
-        }
-        else {
-            elem = plot.canvasManager.getCanvas();
-        }
+        var elem = plot.canvasManager.getCanvas();
 
         this._textRenderer.setText(this.label, ctx);
         var w = this.getWidth(ctx);
         elem.height = h;
         elem.style.width = w;
         elem.style.height = h;
-        // // domelem.style.textAlign = 'center';
-        // elem.style.position = 'absolute';
+        
+		elem = plot.canvasManager.initCanvas(elem);
+		
         this._elem = $(elem);
         this._elem.css({ position: 'absolute'});
         this._elem.addClass('jqplot-'+this.axis+'-label');
-
-        if ($.jqplot.use_excanvas) {
-            // useless ?? window.G_vmlCanvasManager.init_(document);
-            elem = window.G_vmlCanvasManager.initElement(elem);
-        }
         
         elem = null;
         return this._elem;
     };
     
     $.jqplot.CanvasAxisLabelRenderer.prototype.pack = function() {
-        if ($.jqplot.use_excanvas) {
-            //window.G_vmlCanvasManager.init_(document);
-            this._elem.get(0) = window.G_vmlCanvasManager.initElement(this._elem.get(0));
-        }
         this._textRenderer.draw(this._elem.get(0).getContext("2d"), this.label);
     };
     

src/plugins/jqplot.canvasAxisTickRenderer.js

             ropts.pt2px = this.pt2px;
         }
         
-        if (this.enableFontSupport) {
-            
-            function support_canvas_text() {
-                return !!(document.createElement('canvas').getContext && typeof document.createElement('canvas').getContext('2d').fillText == 'function');
-            }
-            
-            if (support_canvas_text()) {
-                this._textRenderer = new $.jqplot.CanvasFontRenderer(ropts);
-            }
-            
-            else {
-                this._textRenderer = new $.jqplot.CanvasTextRenderer(ropts); 
-            }
-        }
-        else {
-            this._textRenderer = new $.jqplot.CanvasTextRenderer(ropts); 
-        }
+        this._textRenderer = new $.jqplot.CanvasTextRenderer(ropts);
     };
     
     $.jqplot.CanvasAxisTickRenderer.prototype.init = function(options) {
         // create a canvas here, but can't draw on it untill it is appended
         // to dom for IE compatability.
 
-        var elem;
-
-        // don't use the canvas manager with excanvas.
-        if ($.jqplot.use_excanvas) {
-            window.G_vmlCanvasManager.uninitElement(elem);
-            elem = document.createElement('canvas');
-        }
-        else {
-            elem = plot.canvasManager.getCanvas();
-        }
+        var elem = plot.canvasManager.getCanvas();
 
         this._textRenderer.setText(this.label, ctx);
         var w = this.getWidth(ctx);
         elem.style.height = h;
         elem.style.textAlign = 'left';
         elem.style.position = 'absolute';
+		
+		elem = plot.canvasManager.initCanvas(elem);
+		
         this._elem = $(elem);
         this._elem.css(this._styles);
         this._elem.addClass('jqplot-'+this.axis+'-tick');
-
-        if ($.jqplot.use_excanvas) {
-            // useless ?? window.G_vmlCanvasManager.init_(document);
-            elem = window.G_vmlCanvasManager.initElement(elem);
-        }
-        
+		
         elem = null;
         return this._elem;
     };
     
     $.jqplot.CanvasAxisTickRenderer.prototype.pack = function() {
-        if ($.jqplot.use_excanvas) {
-            //window.G_vmlCanvasManager.init_(document);
-            this._elem.get(0) = window.G_vmlCanvasManager.initElement(this._elem.get(0));
-        }
         this._textRenderer.draw(this._elem.get(0).getContext("2d"), this.label);
     };