[OpenLayers-Commits] r11817 - sandbox/tschaub/canvas/tests/Renderer

commits-20090109 at openlayers.org commits-20090109 at openlayers.org
Wed Mar 30 19:35:43 EDT 2011


Author: tschaub
Date: 2011-03-30 16:35:42 -0700 (Wed, 30 Mar 2011)
New Revision: 11817

Modified:
   sandbox/tschaub/canvas/tests/Renderer/Canvas.html
Log:
Adding hit detection tests.

Modified: sandbox/tschaub/canvas/tests/Renderer/Canvas.html
===================================================================
--- sandbox/tschaub/canvas/tests/Renderer/Canvas.html	2011-03-30 23:34:45 UTC (rev 11816)
+++ sandbox/tschaub/canvas/tests/Renderer/Canvas.html	2011-03-30 23:35:42 UTC (rev 11817)
@@ -146,7 +146,86 @@
         t.eq(r.resolution, null, "resolution nullified");
         t.eq(r.map, null, "map nullified");
     }
+    
+    function test_hitDetection(t) {
+        if (!supported) {
+            t.plan(0); 
+            return; 
+        }
+        
+        var layer = new OpenLayers.Layer.Vector(null, {
+            isBaseLayer: true,
+            resolutions: [1],
+            styleMap: new OpenLayers.StyleMap({
+                pointRadius: 5,
+                strokeWidth: 3,
+                fillColor: "red",
+                fillOpacity: 0.5,
+                strokeColor: "blue",
+                strokeOpacity: 0.75
+            }),
+            renderers: ["Canvas"]
+        });
+        
+        var map = new OpenLayers.Map({
+            div: "map",
+            controls: [],
+            layers: [layer],
+            center: new OpenLayers.LonLat(0, 0),
+            zoom: 0
+        });
+        
+        layer.addFeatures([
+            new OpenLayers.Feature.Vector(
+                new OpenLayers.Geometry.Point(-100, 0)
+            ),
+            new OpenLayers.Feature.Vector(
+                OpenLayers.Geometry.fromWKT("LINESTRING(-50 0, 50 0)")
+            ),
+            new OpenLayers.Feature.Vector(
+                OpenLayers.Geometry.fromWKT("POLYGON((100 -25, 150 -25, 150 25, 100 25, 100 -25), (120 -5, 130 -5, 130 5, 120 5, 120 -5))")
+            )
+        ]);
+        
+        var cases = [{
+            msg: "center of point", x: -100, y: 0, id: layer.features[0].id
+        }, {
+            msg: "edge of point", x: -103, y: 3, id: layer.features[0].id
+        }, {
+            msg: "outside point", x: -110, y: 3, id: null
+        }, {
+            msg: "center of line", x: 0, y: 0, id: layer.features[1].id
+        }, {
+            msg: "edge of line", x: 0, y: 1, id: layer.features[1].id
+        }, {
+            msg: "outside line", x: 0, y: 5, id: null
+        }, {
+            msg: "inside polygon", x: 110, y: 0, id: layer.features[2].id
+        }, {
+            msg: "edge of polygon", x: 99, y: 0, id: layer.features[2].id
+        }, {
+            msg: "inside polygon hole", x: 125, y: 0, id: null
+        }, {
+            msg: "outside polygon", x: 155, y: 0, id: null
+        }];
 
+        function px(x, y) {
+            return map.getPixelFromLonLat(
+                new OpenLayers.LonLat(x, y)
+            );
+        }        
+        
+        var num = cases.length;
+        t.plan(num);
+        var c, feature;
+        for (var i=0; i<num; ++i) {
+            c = cases[i];
+            feature = layer.renderer.getFeatureIdFromEvent({xy: px(c.x, c.y)});
+            t.eq(feature && feature.id, c.id, c.msg);
+        }
+        
+    }
+
   </script>
 </head>
 <body>



More information about the Commits mailing list