g6/demos/animate-node-hover.html

116 lines
2.7 KiB
HTML
Raw Normal View History

<!DOCTYPE html>
<html lang="en">
<head>
2020-02-14 10:10:54 +08:00
<meta charset="UTF-8" />
<title>自定义节点选中样式</title>
</head>
<body>
<div id="mountNode"></div>
<script src="../build/g6.js"></script>
<script>
/**
* 本案例演示如何通过交互设置元素状态当鼠标hover到圆形节点上时圆形节点执行动画效果
* by 镜曦。
2020-02-14 10:10:54 +08:00
*/
G6.registerNode(
'animate-circle',
{
setState: function setState(name, value, item) {
const shape = item.get('keyShape');
const cfg = item.get('model');
if (name === 'running') {
if (value) {
shape.animate(
ratio => {
const diff = ratio <= 0.5 ? ratio * 10 : (1 - ratio) * 10;
return {
r: cfg.size / 2 + diff,
};
},
{
repeat: true,
duration: 1000,
easing: 'easeCubic',
},
);
} else {
shape.stopAnimate();
shape.attr('lineDash', null);
}
}
},
},
2020-02-14 10:10:54 +08:00
'circle',
);
const data = {
nodes: [
{
x: 100,
y: 100,
shape: 'animate-circle',
label: 'animate-circle',
id: 'node1',
size: 30,
labelCfg: {
position: 'bottom',
},
},
2020-02-14 10:10:54 +08:00
{
x: 400,
y: 100,
shape: 'rect',
label: 'rect2',
id: 'node2',
labelCfg: {
position: 'bottom',
},
},
2020-02-14 10:10:54 +08:00
],
edges: [
{
source: 'node1',
target: 'node2',
label: 'line',
labelCfg: {
refY: 10,
},
style: {
endArrow: true,
},
},
],
};
const graph = new G6.Graph({
container: 'mountNode',
width: 1000,
height: 600,
defaultNode: {
color: '#87e8de',
style: {
2020-02-14 10:10:54 +08:00
fill: '#87e8de',
},
},
defaultEdge: {
2020-02-14 10:10:54 +08:00
color: '#bae7ff',
},
modes: {
2020-02-14 10:10:54 +08:00
default: ['drag-node'],
},
});
graph.data(data);
graph.render();
graph.on('node:mouseenter', ev => {
const node = ev.item;
graph.setItemState(node, 'running', true);
});
graph.on('node:mouseleave', ev => {
const node = ev.item;
graph.setItemState(node, 'running', false);
});
</script>
</body>
2020-02-14 10:10:54 +08:00
</html>