From 7ed62ba6396a7c44322320b3513cffdd7dec936a Mon Sep 17 00:00:00 2001 From: crschmidt Date: Fri, 25 Aug 2006 18:20:20 +0000 Subject: [PATCH] Commit canvas work to trunk. Note that the Canvas layer is *not production ready* -- it is a memory hog, and slow as heck when dragging. However, including it doesn't harm OpenLayers. git-svn-id: http://svn.openlayers.org/trunk/openlayers@1371 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- examples/canvas.html | 613 +++++++++++++++++++++++++++++++++ lib/OpenLayers.js | 1 + lib/OpenLayers/Layer/Canvas.js | 117 +++++++ 3 files changed, 731 insertions(+) create mode 100644 examples/canvas.html create mode 100644 lib/OpenLayers/Layer/Canvas.js diff --git a/examples/canvas.html b/examples/canvas.html new file mode 100644 index 0000000000..f683d4faa1 --- /dev/null +++ b/examples/canvas.html @@ -0,0 +1,613 @@ + + + + + + + + +

OpenLayers Example

+
+
+ Start Lon: + Start Lat:
+ End Lon: + End Lat:
+ +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + +
+ +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + +
+ +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + +
+ +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + +
+ +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + +
+ +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + +
+ +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + + + +    + +
+
+
+ + diff --git a/lib/OpenLayers.js b/lib/OpenLayers.js index a037a64c87..e9628475ac 100644 --- a/lib/OpenLayers.js +++ b/lib/OpenLayers.js @@ -82,6 +82,7 @@ if (typeof(_OPENLAYERS_SFL_) == "undefined") { "OpenLayers/Layer/WMS/Untiled.js", "OpenLayers/Layer/GeoRSS.js", "OpenLayers/Layer/Boxes.js", + "OpenLayers/Layer/Canvas.js", "OpenLayers/Popup/Anchored.js", "OpenLayers/Popup/AnchoredBubble.js", "OpenLayers/Control.js", diff --git a/lib/OpenLayers/Layer/Canvas.js b/lib/OpenLayers/Layer/Canvas.js new file mode 100644 index 0000000000..6c48a323ae --- /dev/null +++ b/lib/OpenLayers/Layer/Canvas.js @@ -0,0 +1,117 @@ +/* Copyright (c) 2006 MetaCarta, Inc., published under the BSD license. + * See http://svn.openlayers.org/trunk/openlayers/license.txt for the full + * text of the license. */ + +/** + * @class + * + * @requires OpenLayers/Layer.js + */ +OpenLayers.Layer.Canvas = Class.create(); +OpenLayers.Layer.Canvas.prototype = + Object.extend( new OpenLayers.Layer(), { + + /** Canvas layer is never a base layer. + * + * @type Boolean + */ + isBaseLayer: false, + isFixed: true, + /** internal marker list + * @type Array(OpenLayers.Marker) */ + canvas: null, + + lines: new Array(), + + /** + * @constructor + * + * @param {String} name + * @param {Object} options Hashtable of extra options to tag onto the layer + */ + initialize: function(name, options) { + OpenLayers.Layer.prototype.initialize.apply(this, arguments); + }, + + /** + * + */ + destroy: function() { + // xxx actually destroy the canvas to scavenge ram? + canvas = null; + OpenLayers.Layer.prototype.destroy.apply(this, arguments); + }, + + + /** + * @param {OpenLayers.Bounds} bounds + * @param {Boolean} zoomChanged + * @param {Boolean} minor + */ + moveTo:function(bounds, zoomChanged, minor) { + this.redraw(); + }, + + setStrokeColor: function(color) { + var ctx = this.canvas.getContext("2d"); + ctx.strokeStyle = color; + }, + setStrokeWidth: function(width) { + var ctx = this.canvas.getContext("2d"); + ctx.lineWidth = width; + }, + setAlpha: function(alpha) { + var ctx = this.canvas.getContext("2d"); + ctx.globalAlpha = alpha; + }, + /** + * + */ + clearCanvas: function() { + if(this.canvas != null) { + this.canvas.getContext("2d").clearRect(0,0,this.map.getSize().w, this.map.getSize().h); + // xxx use real width and height + } + }, + + drawLine: function(start, end) { + var ctx = this.canvas.getContext("2d"); + this.addLine(start, end); + this.lines.push(new Array(start,end, ctx.strokeStyle, ctx.lineWidth, ctx.globalAlpha)); + }, + addLine: function(start, end) { + var ctx = this.canvas.getContext("2d"); + var startpx = this.map.getPixelFromLonLat(start); + var endpx = this.map.getPixelFromLonLat(end); + ctx.beginPath(); + ctx.moveTo(startpx.x, startpx.y); + ctx.lineTo(endpx.x, endpx.y); + ctx.closePath(); + ctx.stroke(); + }, + + /** clear all the marker div's from the layer and then redraw all of them. + * Use the map to recalculate new placement of markers. + */ + redraw: function() { + // xxx rebuild the canvas if smaller than the view + // xxx may wish to overside the canvas with overflow=hidden by default + if(!this.canvas) { + this.canvas = document.createElement("CANVAS"); + this.canvas.setAttribute("width",this.map.getSize().w); + this.canvas.setAttribute("height",this.map.getSize().h); + this.div.appendChild(this.canvas); + } else { + this.clearCanvas(); + } + for(var i=0; i < this.lines.length; i++) { + this.setStrokeColor(this.lines[i][2]); + this.setStrokeWidth(this.lines[i][3]); + this.setAlpha(this.lines[i][4]); + this.addLine(this.lines[i][0], this.lines[i][1]); + } + }, + + /** @final @type String */ + CLASS_NAME: "OpenLayers.Layer.Canvas" +});