diff --git a/packages/cax-svg/cax/svg.js b/packages/cax-svg/cax/svg.js index 6d9e11eed..28c962aec 100644 --- a/packages/cax-svg/cax/svg.js +++ b/packages/cax-svg/cax/svg.js @@ -5,10 +5,18 @@ function renderSVG(vdom, canvas, scope) { const svg = new SVG(vdom) stage.add(svg) stage.update() - + triggerAddedStage(svg) return svg.children[0] } +function triggerAddedStage(svg) { + svg.addedStage && svg.addedStage() + + svg.children && svg.children.forEach(child => { + triggerAddedStage(child) + }) +} + export { renderSVG, html diff --git a/packages/cax-svg/cax/svg/animate.js b/packages/cax-svg/cax/svg/animate.js new file mode 100644 index 000000000..26c0ef7c7 --- /dev/null +++ b/packages/cax-svg/cax/svg/animate.js @@ -0,0 +1,70 @@ +import pathTransition from '../pasition/index' + +export function animate(obj, option) { + const valueList = option.values.split(';').filter(item => item.trim() !== '') + let index = 0, + count = 0, + stage + const len = valueList.length + const duration = parseFloat(option.dur) / len * 1000 + function _animate() { + const nextIndex = index + 1 === len ? 0 : index + 1 + pathTransition.animate({ + from: valueList[index], + to: valueList[nextIndex], + duration: duration, + easing: function (v) { return v }, + begin: function () { + stage = obj.stage + }, + progress: function (shapes, percent) { + obj.d = toSVGString(shapes) + stage.update() + }, + end: function (shapes) { + count++ + index = nextIndex + if (option.repeatCount === 'indefinite') { + _animate() + } else { + if (count < Number(option.repeatCount)) { + _animate() + } + } + + } + }) + } + + if (obj.stage) { + _animate() + } else { + obj.addedStage = () => { + _animate() + } + } + +} + + +function toSVGString(shapes) { + return shapes.map(function (shape) { + shape.forEach(function (point, idx) { + if (!idx) { + /* + * 若是第一个点数组,那么对该点数组的处理是前面加M,然后前两个点后面加C + * */ + point.splice(2, 0, "C"); + point.unshift("M"); + } else { + /* + * 除了第一个点数据外,所有的点数组的前两个点删除掉 + * */ + point.splice(0, 2, "C"); + } + }); + return shape.map(function (point) { + return point.join(" "); + }).join(""); + }).join("") +}; diff --git a/packages/cax-svg/cax/svg/index.js b/packages/cax-svg/cax/svg/index.js index 325e5872f..322d2ac5b 100644 --- a/packages/cax-svg/cax/svg/index.js +++ b/packages/cax-svg/cax/svg/index.js @@ -8,6 +8,7 @@ import { polygon } from './polygon' import { path } from './path' import { pasition } from './pasition' import { group } from './group' +import { animate } from './animate' class SVG extends Group { constructor(vdom) { @@ -58,7 +59,11 @@ class SVG extends Group { break case 'path': - parent.add(path(vdomChild.props)) + const obj = path(vdomChild.props) + parent.add(obj) + if(vdomChild.children && vdomChild.children[0] && vdomChild.children[0].type === 'animate'){ + animate(obj, vdomChild.children[0].props) + } break case 'pasition': diff --git a/packages/cax-svg/pages/animate/index.js b/packages/cax-svg/pages/animate/index.js index 723b9acf6..106c33940 100644 --- a/packages/cax-svg/pages/animate/index.js +++ b/packages/cax-svg/pages/animate/index.js @@ -7,7 +7,7 @@ Page({ const svg = renderSVG(html` - +