|
|
import {Renderer} from './Renderer'; |
|
|
import * as DomUtil from '../../dom/DomUtil'; |
|
|
import * as DomEvent from '../../dom/DomEvent'; |
|
|
import Browser from '../../core/Browser'; |
|
|
import {stamp} from '../../core/Util'; |
|
|
import {svgCreate, pointsToPath} from './SVG.Util'; |
|
|
export {pointsToPath}; |
|
|
import {vmlMixin, vmlCreate} from './SVG.VML'; |
|
|
|
|
|
export var create = Browser.vml ? vmlCreate : svgCreate; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export var SVG = Renderer.extend({ |
|
|
|
|
|
_initContainer: function () { |
|
|
this._container = create('svg'); |
|
|
|
|
|
|
|
|
this._container.setAttribute('pointer-events', 'none'); |
|
|
|
|
|
this._rootGroup = create('g'); |
|
|
this._container.appendChild(this._rootGroup); |
|
|
}, |
|
|
|
|
|
_destroyContainer: function () { |
|
|
DomUtil.remove(this._container); |
|
|
DomEvent.off(this._container); |
|
|
delete this._container; |
|
|
delete this._rootGroup; |
|
|
delete this._svgSize; |
|
|
}, |
|
|
|
|
|
_update: function () { |
|
|
if (this._map._animatingZoom && this._bounds) { return; } |
|
|
|
|
|
Renderer.prototype._update.call(this); |
|
|
|
|
|
var b = this._bounds, |
|
|
size = b.getSize(), |
|
|
container = this._container; |
|
|
|
|
|
|
|
|
if (!this._svgSize || !this._svgSize.equals(size)) { |
|
|
this._svgSize = size; |
|
|
container.setAttribute('width', size.x); |
|
|
container.setAttribute('height', size.y); |
|
|
} |
|
|
|
|
|
|
|
|
DomUtil.setPosition(container, b.min); |
|
|
container.setAttribute('viewBox', [b.min.x, b.min.y, size.x, size.y].join(' ')); |
|
|
|
|
|
this.fire('update'); |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
_initPath: function (layer) { |
|
|
var path = layer._path = create('path'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (layer.options.className) { |
|
|
DomUtil.addClass(path, layer.options.className); |
|
|
} |
|
|
|
|
|
if (layer.options.interactive) { |
|
|
DomUtil.addClass(path, 'leaflet-interactive'); |
|
|
} |
|
|
|
|
|
this._updateStyle(layer); |
|
|
this._layers[stamp(layer)] = layer; |
|
|
}, |
|
|
|
|
|
_addPath: function (layer) { |
|
|
if (!this._rootGroup) { this._initContainer(); } |
|
|
this._rootGroup.appendChild(layer._path); |
|
|
layer.addInteractiveTarget(layer._path); |
|
|
}, |
|
|
|
|
|
_removePath: function (layer) { |
|
|
DomUtil.remove(layer._path); |
|
|
layer.removeInteractiveTarget(layer._path); |
|
|
delete this._layers[stamp(layer)]; |
|
|
}, |
|
|
|
|
|
_updatePath: function (layer) { |
|
|
layer._project(); |
|
|
layer._update(); |
|
|
}, |
|
|
|
|
|
_updateStyle: function (layer) { |
|
|
var path = layer._path, |
|
|
options = layer.options; |
|
|
|
|
|
if (!path) { return; } |
|
|
|
|
|
if (options.stroke) { |
|
|
path.setAttribute('stroke', options.color); |
|
|
path.setAttribute('stroke-opacity', options.opacity); |
|
|
path.setAttribute('stroke-width', options.weight); |
|
|
path.setAttribute('stroke-linecap', options.lineCap); |
|
|
path.setAttribute('stroke-linejoin', options.lineJoin); |
|
|
|
|
|
if (options.dashArray) { |
|
|
path.setAttribute('stroke-dasharray', options.dashArray); |
|
|
} else { |
|
|
path.removeAttribute('stroke-dasharray'); |
|
|
} |
|
|
|
|
|
if (options.dashOffset) { |
|
|
path.setAttribute('stroke-dashoffset', options.dashOffset); |
|
|
} else { |
|
|
path.removeAttribute('stroke-dashoffset'); |
|
|
} |
|
|
} else { |
|
|
path.setAttribute('stroke', 'none'); |
|
|
} |
|
|
|
|
|
if (options.fill) { |
|
|
path.setAttribute('fill', options.fillColor || options.color); |
|
|
path.setAttribute('fill-opacity', options.fillOpacity); |
|
|
path.setAttribute('fill-rule', options.fillRule || 'evenodd'); |
|
|
} else { |
|
|
path.setAttribute('fill', 'none'); |
|
|
} |
|
|
}, |
|
|
|
|
|
_updatePoly: function (layer, closed) { |
|
|
this._setPath(layer, pointsToPath(layer._parts, closed)); |
|
|
}, |
|
|
|
|
|
_updateCircle: function (layer) { |
|
|
var p = layer._point, |
|
|
r = Math.max(Math.round(layer._radius), 1), |
|
|
r2 = Math.max(Math.round(layer._radiusY), 1) || r, |
|
|
arc = 'a' + r + ',' + r2 + ' 0 1,0 '; |
|
|
|
|
|
|
|
|
var d = layer._empty() ? 'M0 0' : |
|
|
'M' + (p.x - r) + ',' + p.y + |
|
|
arc + (r * 2) + ',0 ' + |
|
|
arc + (-r * 2) + ',0 '; |
|
|
|
|
|
this._setPath(layer, d); |
|
|
}, |
|
|
|
|
|
_setPath: function (layer, path) { |
|
|
layer._path.setAttribute('d', path); |
|
|
}, |
|
|
|
|
|
|
|
|
_bringToFront: function (layer) { |
|
|
DomUtil.toFront(layer._path); |
|
|
}, |
|
|
|
|
|
_bringToBack: function (layer) { |
|
|
DomUtil.toBack(layer._path); |
|
|
} |
|
|
}); |
|
|
|
|
|
if (Browser.vml) { |
|
|
SVG.include(vmlMixin); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function svg(options) { |
|
|
return Browser.svg || Browser.vml ? new SVG(options) : null; |
|
|
} |
|
|
|