React Leaflet.Arc - Animate

时间:2017-03-14 12:20:47

标签: leaflet automatic-ref-counting react-leaflet

我正在尝试使用动画创建ReactLeafletArc插件,这是代码

if (!L) {
    throw "Leaflet.js not included";
} else if (!arc || !arc.GreatCircle) {
    throw "arc.js not included";
} else {
    L.Polyline.Arc = function (from, to, options) {
        from = L.latLng(from);
        to = L.latLng(to);

        var vertices = 10;
        var arcOptions = {};
        if (options) {
            if (options.offset) {
                arcOptions.offset = options.offset;
                delete options.offset;
            }
            if (options.vertices) {
                vertices = options.vertices;
                delete options.vertices;
            }
        }

        var generator = new arc.GreatCircle({ x: from.lng, y: from.lat }, { x: to.lng, y: to.lat });

        var line = generator.Arc(vertices, arcOptions);
        var latLngs = [];

        var newLine = L.polyline(line.geometries[0].coords.map(function (c) {
            return c.reverse();
        }), options);

        var totalLength = newLine._path.getTotalLength() * 4;
        newLine._path.classList.add('path-start');
        newLine._path.style.strokeDashoffset = totalLength;
        newLine._path.style.strokeDasharray = totalLength;
        setTimeout((function (path) {
            return function () {
                path.style.strokeDashoffset = 0;
            };
        })(newLine._path), 200);

        return newLine;
    };
}

我收到错误

  

未捕获的TypeError:无法读取属性' getTotalLength'未定义的

使用console.log(newLine)我可以看到没有_path。

NewClass
_bounds:L.LatLngBounds
_initHooksCalled:true
_latlngs:Array[100]
options:Object
__proto__:NewClass

但如果我评论该部分

var totalLength = newLine._path.getTotalLength() * 4;
    newLine._path.classList.add('path-start');
    newLine._path.style.strokeDashoffset = totalLength;
    newLine._path.style.strokeDasharray = totalLength;
    setTimeout((function (path) {
        return function () {
            path.style.strokeDashoffset = 0;
        };
    })(newLine._path), 200);

创建的行没有动画,使用console.log(newLine)我得到了这个

NewClass
_bounds:L.LatLngBounds
_initHooksCalled:true
_latlngs:Array[100]
_leaflet_id:122
_map:NewClass
_mapToAdd:NewClass
_parts:Array[1]
_path:path
_pxBounds:L.Bounds
_renderer:NewClass
_rings:Array[1]
_zoomAnimated:true
options:Object
__proto__:NewClass

有什么建议吗?

0 个答案:

没有答案