mxgraph添加节点闪烁和SVG动画

1,416 阅读2分钟

节点闪烁效果

要实现mxGraph中节点或节点边框的闪烁效果,可以通过以下步骤来实现:

  1. 创建一个带有闪烁效果的CSS类:
@keyframes blink {
  from {
    opacity: 1.0;
  }
  to {
    opacity: 0.0;
  }
}

.blink {
  animation: blink 1s linear infinite alternate;
}

这个CSS类定义了一个名为"blink"的类,使用动画关键帧来实现节点的闪烁效果。

  1. 在mxStylesheet中定义该类的样式:
var style = graph.getStylesheet().getDefaultVertexStyle();
style[mxConstants.STYLE_STROKECOLOR]='#ff0000'; // 设置节点边框颜色
style[mxConstants.STYLE_STROKEWIDTH]=3; // 设置节点边框宽度
style['blink'] = '1'; // 设置闪烁标记

这里设置了节点的边框颜色和宽度,并在样式中添加了名为"blink"的属性,用于标记需要闪烁的节点。

  1. 在mxGraph中添加节点并设置该节点的样式为"blink",从而实现闪烁效果:
var parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
try {
  var v1 = graph.insertVertex(parent, null, 'Node1', 20, 20, 80, 30, 'blink'); // 设置节点样式为"blink"
} finally {
  graph.getModel().endUpdate();
}

以上是在mxGraph中实现节点闪烁效果的方法。如果需要实现节点边框闪烁效果,只需要将上述样式中的STROKECOLOR、STROKEWIDTH等常量修改为STROKECOLOR2、STROKEWIDTH2等常量即可。

SVG动画的示例:

  1. 填充颜色渐变动画

使用SVG元素的animateTransform属性可以实现填充颜色渐变动画,可以通过设置时间间隔和颜色变化范围等参数来定制动画效果。

var doc = mxUtils.createXmlDocument();
var svg = document.createElementNS(mxConstants.NS_SVG, 'svg');
var animateTransform = doc.createElementNS(mxConstants.NS_SVG, 'animateTransform');
animateTransform.setAttribute('attributeName', 'fill');
animateTransform.setAttribute('type', 'rotate');
animateTransform.setAttribute('from', '#ff0000');
animateTransform.setAttribute('to', '#0000ff');
animateTransform.setAttribute('dur', '3s');
animateTransform.setAttribute('repeatCount', 'indefinite');
svg.appendChild(animateTransform);
  1. 路径动画

使用SVG元素的animateMotion属性可以实现路径动画,可以通过设置路径和时间间隔等参数来定制动画效果。

var doc = mxUtils.createXmlDocument();
var path = doc.createElementNS(mxConstants.NS_SVG, 'path');
path.setAttribute('d', 'M 10 80 Q 95 10 180 80');
var animateMotion = doc.createElementNS(mxConstants.NS_SVG, 'animateMotion');
animateMotion.setAttribute('dur', '5s');
animateMotion.setAttribute('repeatCount', 'indefinite');
var mpath = doc.createElementNS(mxConstants.NS_SVG, 'mpath');
mpath.setAttributeNS(null, "xlink:href", "#path1");
animateMotion.appendChild(mpath);
path.appendChild(animateMotion);
  1. 形状变换动画

使用SVG元素的animateTransform属性可以实现形状变换动画,可以通过设置变换类型和时间间隔等参数来定制动画效果。

var doc = mxUtils.createXmlDocument();
var rect = doc.createElementNS(mxConstants.NS_SVG, 'rect');
rect.setAttribute('width', '100');
rect.setAttribute('height', '200');
var animateTransform = doc.createElementNS(mxConstants.NS_SVG, 'animateTransform');
animateTransform.setAttribute('attributeName', 'transform');
animateTransform.setAttribute('type', 'rotate');
animateTransform.setAttribute('from', '0 50 50');
animateTransform.setAttribute('to', '360 50 50');
animateTransform.setAttribute('dur', '10s');
animateTransform.setAttribute('repeatCount', 'indefinite');
rect.appendChild(animateTransform);

以上是几个在mxGraph中使用SVG动画的示例,可以根据实际需求进行修改和扩展。