svg.js

来自「用来在地图上做操作GIS,在地图上做标记」· JavaScript 代码 · 共 554 行 · 第 1/2 页

JS
554
字号
     * {Boolean} Whether or not the specified node is of the specified type     */    nodeTypeCompare: function(node, type) {        return (type == node.nodeName);    },       /**     * Method: createRenderRoot     *      * Returns:     * {DOMElement} The specific render engine's root element     */    createRenderRoot: function() {        var id = this.container.id + "_svgRoot";        var rendererRoot = this.nodeFactory(id, "svg");        return rendererRoot;                            },    /**     * Method: createRoot     *      * Returns:     * {DOMElement} The main root element to which we'll add vectors     */    createRoot: function() {        var id = this.container.id + "_root";        var root = this.nodeFactory(id, "g");        // flip the SVG display Y axis upside down so it         // matches the display Y axis of the map        root.setAttributeNS(null, "transform", "scale(1, -1)");        return root;    },    /**************************************     *                                    *     *     GEOMETRY DRAWING FUNCTIONS     *     *                                    *     **************************************/    /**     * Method: drawPoint     * This method is only called by the renderer itself.     *      * Parameters:      * node - {DOMElement}     * geometry - {<OpenLayers.Geometry>}     */     drawPoint: function(node, geometry) {        this.drawCircle(node, geometry, 1);    },    /**     * Method: drawCircle     * This method is only called by the renderer itself.     *      * Parameters:      * node - {DOMElement}     * geometry - {<OpenLayers.Geometry>}     * radius - {Float}     */    drawCircle: function(node, geometry, radius) {        var resolution = this.getResolution();        var x = (geometry.x / resolution + this.left);        var y = (geometry.y / resolution - this.top);        var draw = true;        if (x < -this.maxPixel || x > this.maxPixel) { draw = false; }        if (y < -this.maxPixel || y > this.maxPixel) { draw = false; }        if (draw) {             node.setAttributeNS(null, "cx", x);            node.setAttributeNS(null, "cy", y);            node.setAttributeNS(null, "r", radius);        } else {            this.root.removeChild(node);        }                    },        /**     * Method: drawLineString     * This method is only called by the renderer itself.     *      * Parameters:      * node - {DOMElement}     * geometry - {<OpenLayers.Geometry>}     */     drawLineString: function(node, geometry) {        node.setAttributeNS(null, "points", this.getComponentsString(geometry.components));      },        /**v     * Method: drawLinearRing     * This method is only called by the renderer itself.     *      * Parameters:      * node - {DOMElement}     * geometry - {<OpenLayers.Geometry>}     */     drawLinearRing: function(node, geometry) {        node.setAttributeNS(null, "points", this.getComponentsString(geometry.components));    },        /**     * Method: drawPolygon     * This method is only called by the renderer itself.     *      * Parameters:      * node - {DOMElement}     * geometry - {<OpenLayers.Geometry>}     */     drawPolygon: function(node, geometry) {        var d = "";        var draw = true;        for (var j = 0; j < geometry.components.length; j++) {            var linearRing = geometry.components[j];            d += " M";            for (var i = 0; i < linearRing.components.length; i++) {                var component = this.getShortString(linearRing.components[i])                if (component) {                    d += " " + component;                } else {                    draw = false;                }                }        }        d += " z";        if (draw) {            node.setAttributeNS(null, "d", d);            node.setAttributeNS(null, "fill-rule", "evenodd");        } else {            node.setAttributeNS(null, "d", "");        }        },        /**     * Method: drawRectangle     * This method is only called by the renderer itself.     *      * Parameters:      * node - {DOMElement}     * geometry - {<OpenLayers.Geometry>}     */     drawRectangle: function(node, geometry) {        // This needs to be reworked         var x = (geometry.x / resolution + this.left);        var y = (geometry.y / resolution - this.top);        var draw = true;        if (x < -this.maxPixel || x > this.maxPixel) { draw = false; }        if (y < -this.maxPixel || y > this.maxPixel) { draw = false; }        if (draw) {            node.setAttributeNS(null, "x", x);            node.setAttributeNS(null, "y", y);            node.setAttributeNS(null, "width", geometry.width);            node.setAttributeNS(null, "height", geometry.height);        } else {            node.setAttributeNS(null, "x", "");            node.setAttributeNS(null, "y", "");            node.setAttributeNS(null, "width", 0);            node.setAttributeNS(null, "height", 0);        }        },            /**     * Method: drawCurve     * This method is only called by the renderer itself.     *      * Parameters:      * node - {DOMElement}     * geometry - {<OpenLayers.Geometry>}     */     drawCurve: function(node, geometry) {        var d = null;        var draw = true;        for (var i = 0; i < geometry.components.length; i++) {            if ((i%3) == 0 && (i/3) == 0) {                var component = this.getShortString(geometry.components[i]);                if (!component) { draw = false; }                d = "M " + component;            } else if ((i%3) == 1) {                var component = this.getShortString(geometry.components[i]);                if (!component) { draw = false; }                d += " C " + component;            } else {                var component = this.getShortString(geometry.components[i]);                if (!component) { draw = false; }                d += " " + component;            }        }        if (draw) {            node.setAttributeNS(null, "d", d);        } else {            node.setAttributeNS(null, "d", "");        }        },        /**     * Method: drawSurface     * This method is only called by the renderer itself.     *      * Parameters:      * node - {DOMElement}     * geometry - {<OpenLayers.Geometry>}     */     drawSurface: function(node, geometry) {        // create the svg path string representation        var d = null;        var draw = true;        for (var i = 0; i < geometry.components.length; i++) {            if ((i%3) == 0 && (i/3) == 0) {                var component = this.getShortString(geometry.components[i]);                if (!component) { draw = false; }                d = "M " + component;            } else if ((i%3) == 1) {                var component = this.getShortString(geometry.components[i]);                if (!component) { draw = false; }                d += " C " + component;            } else {                var component = this.getShortString(geometry.components[i]);                if (!component) { draw = false; }                d += " " + component;            }        }        d += " Z";        if (draw) {            node.setAttributeNS(null, "d", d);        } else {            node.setAttributeNS(null, "d", "");        }        },    /**      * Method: getComponentString     *      * Parameters:     * components - {Array(<OpenLayers.Geometry.Point)} Array of points     */    getComponentsString: function(components) {        var strings = [];        for(var i = 0; i < components.length; i++) {            var component = this.getShortString(components[i]);            if (component) {                strings.push(component);            }        }        return strings.join(",");    },        /**      * Method: getShortString     *      * Parameters:     * point - {<OpenLayers.Geometry.Point>}     *      * Returns:     * {String}     */    getShortString: function(point) {        var resolution = this.getResolution();        var x = (point.x / resolution + this.left);        var y = (point.y / resolution - this.top);        if (x < -this.maxPixel || x > this.maxPixel) { return false; }        if (y < -this.maxPixel || y > this.maxPixel) { return false; }        var string =  x + "," + y;          return string;            },    CLASS_NAME: "OpenLayers.Renderer.SVG"});

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?