mxgraph示例解析(二) animation动画实现

animation运动效果图

官方示例

<style type="text/css">
    .flow {
        stroke-dasharray: 8;
        animation: dash 0.5s linear;
        animation-iteration-count: infinite;
    }
    @keyframes dash {
        to {
        stroke-dashoffset: -16;
        }
    }
</style>

<!-- Example code -->
<script type="text/javascript">
    function main(container)
    {
        var graph = new mxGraph(container);
        graph.setEnabled(false);
        var parent = graph.getDefaultParent();

        var vertexStyle = 'shape=cylinder;strokeWidth=2;fillColor=#ffffff;strokeColor=black;' +
            'gradientColor=#a0a0a0;fontColor=black;fontStyle=1;spacingTop=14;';
        
        graph.getModel().beginUpdate();
        try
        {
            var v1 = graph.insertVertex(parent, null, 'Pump', 20, 20, 60, 60,vertexStyle);
            var v2 = graph.insertVertex(parent, null, 'Tank', 200, 150, 60, 60,vertexStyle);
            var e1 = graph.insertEdge(parent, null, '', v1, v2,
                'strokeWidth=3;endArrow=block;endSize=2;endFill=1;strokeColor=black;rounded=1;');
            e1.geometry.points = [new mxPoint(230, 50)];
            graph.orderCells(true, [e1]);
        }
        finally
        {
            // Updates the display
            graph.getModel().endUpdate();
        }

        // Adds animation to edge shape and makes "pipe" visible
        var state = graph.view.getState(e1);
        state.shape.node.getElementsByTagName('path')[0].removeAttribute('visibility');
        state.shape.node.getElementsByTagName('path')[0].setAttribute('stroke-width', '6');
        state.shape.node.getElementsByTagName('path')[0].setAttribute('stroke', 'lightGray');
        state.shape.node.getElementsByTagName('path')[1].setAttribute('class', 'flow');
    };
</script>

实现原理

  • 获取连线
var state = graph.view.getState(e1);
  • 添加动画
state.shape.node.getElementsByTagName('path')[1].setAttribute('class', 'flow');
  • 动画实现
.flow {
    stroke-dasharray: 8;
    animation: dash 0.5s linear;
    animation-iteration-count: infinite;
}
@keyframes dash {
    to {
    stroke-dashoffset: -16;
    }
}

简化示例

  • css
<style type="text/css">
    .flow {
        stroke-dasharray: 8;
        animation: dash 0.5s linear;
        animation-iteration-count: infinite;
    }
    
    @keyframes dash {
        to {
            stroke-dashoffset: -16;
        }
    }
</style>
  • js
var graph = new mxGraph(container);
graph.setEnabled(false); //设置不可编辑
var parent = graph.getDefaultParent();

graph.getModel().beginUpdate();
try {
    var v1 = graph.insertVertex(parent, null, "Pump", 20, 20, 60, 60);
    var v2 = graph.insertVertex(parent, null, "Tank", 200, 150, 60, 60);
    var e1 = graph.insertEdge(parent, null, "", v1, v2);
    e1.geometry.points = [new mxPoint(230, 50)];
} finally {
    graph.getModel().endUpdate();
}

var state = graph.view.getState(e1);
state.shape.node.getElementsByTagName("path")[1].setAttribute("class", "flow"); //设置动画css
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,802评论 1 32
  • Just In Time即时编译器。当JVM发现某个方法或代码块运行频繁时,就会把这段代码认定为热点代码,通过JI...
    蒸汽飞船阅读 3,421评论 0 0
  • 昨晚赶到学校时晚会已是快结束,只因在同事聚餐上贪吃了几口。 要换作五年...
    穿越尘埃1阅读 1,560评论 0 0
  • 童年,敏感,纤细幼小又有些小受伤的心灵,有些受伤总会被释然,心灵重新抚平,又注入新的愁绪,伴随着幸福,旧的愁绪远去...
    元初2023阅读 1,638评论 0 0
  • 第594天锻炼,今天练车。 ——每天一荐:“新东方家庭教育”
    河南师范大学文学院王程颢阅读 1,118评论 0 0