# Code coverage report for src/layer/vector/canvas/Polygon.Canvas.js

## Statements: 8.33% (1 / 12) Branches: 12.5% (1 / 8) Functions: 0% (0 / 1) Lines: 8.33% (1 / 12)

All files » src/layer/vector/canvas/ » Polygon.Canvas.js

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38

    1

     /*
     * Extends L.Polygon to be able to manually detect clicks on Canvas-rendered polygons.
     */

    L.Polygon.include(!L.Path.CANVAS ? {} : {
    	_containsPoint: function (p) {
    		var inside = false,
    		    part, p1, p2,
    		    i, j, k,
    		    len, len2;

    		// TODO optimization: check if within bounds first

    		if (L.Polyline.prototype._containsPoint.call(this, p, true)) {
    			// click on polygon border
    			return true;
    		}

    		// ray casting algorithm for detecting if point is in polygon

    		for (i = 0, len = this._parts.length; i < len; i++) {
    			part = this._parts[i];

    			for (j = 0, len2 = part.length, k = len2 - 1; j < len2; k = j++) {
    				p1 = part[j];
    				p2 = part[k];

    				if (((p1.y > p.y) !== (p2.y > p.y)) &&
    						(p.x < (p2.x - p1.x) * (p.y - p1.y) / (p2.y - p1.y) + p1.x)) {
    					inside = !inside;
    				}
    			}
    		}

    		return inside;
    	}
    });