栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何为svg路径的渐进绘图制作动画?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何为svg路径的渐进绘图制作动画?

此答案中列出了三种技术:


有一个全SVG解决方案,涉及逐步修改

stroke-dasharray
形状以绘制越来越长的“破折号”,然后是巨大的空白。

演示:[http](http://phrogz.net/svg/progressively-drawing-svg-path-via-

dasharray.html)://phrogz.net/svg/progressively-drawing-svg-path-via-
dasharray.html

相关代码:

var distancePerPoint = 1;var drawFPS          = 60;var orig = document.querySelector('path'), length, timer;orig.addEventListener('mouseover',startDrawingPath,false);orig.addEventListener('mouseout', stopDrawingPath, false);function startDrawingPath(){  length = 0;  orig.style.stroke = '#f60';  timer = setInterval(increaseLength,1000/drawFPS);}function increaseLength(){  var pathLength = orig.getTotalLength();  length += distancePerPoint;  orig.style.strokeDasharray = [length,pathLength].join(' ');  if (length >= pathLength) clearInterval(timer);}function stopDrawingPath(){  clearInterval(timer);  orig.style.stroke = '';  orig.style.strokeDasharray = '';}

另外,您仍然可以使用所有SVG并选择一次构建一个SVG路径命令:

演示:[http](http://phrogz.net/svg/progressively-cloning-svg-

path.html)://phrogz.net/svg/progressively-cloning-svg-
path.html

相关代码:

// Assumes 'orig' and dup' are SVG pathsfunction addNextPathSegment(){  var nextIndex   = dup.pathSegList.numberOfItems;  if (nextIndex<orig.pathSegList.numberOfItems){    var nextSegment = orig.pathSegList.getItem(nextIndex);    var segmentDup  = cloneSVGPathSeg( dup, nextSegment );    dup.pathSegList.appendItem( segmentDup );  }}function cloneSVGPathSeg( path, seg ){  switch(seg.pathSegTypeAsLetter){    case 'M': return path.createSVGPathSegMovetoAbs(seg.x,seg.y);         break;    case 'm': return path.createSVGPathSegMovetoRel(seg.x,seg.y);         break;    case 'L': return path.createSVGPathSegLinetoAbs(seg.x,seg.y);         break;    case 'l': return path.createSVGPathSegLinetoRel(seg.x,seg.y);         break;    case 'H': return path.createSVGPathSegLinetoHorizontalAbs(seg.x);     break;    case 'h': return path.createSVGPathSegLinetoHorizontalRel(seg.x);     break;    case 'V': return path.createSVGPathSegLinetoVerticalAbs(seg.y);       break;    case 'v': return path.createSVGPathSegLinetoVerticalRel(seg.y);       break;    case 'C': return path.createSVGPathSegCurvetoCubicAbs(seg.x,seg.y,seg.x1,seg.y1,seg.x2,seg.y2);        break;    case 'c': return path.createSVGPathSegCurvetoCubicRel(seg.x,seg.y,seg.x1,seg.y1,seg.x2,seg.y2);        break;    case 'S': return path.createSVGPathSegCurvetoCubicSmoothAbs(seg.x,seg.y,seg.x2,seg.y2);     break;    case 's': return path.createSVGPathSegCurvetoCubicSmoothRel(seg.x,seg.y,seg.x2,seg.y2);     break;    case 'Q': return path.createSVGPathSegCurvetoQuadraticAbs(seg.x,seg.y,seg.x1,seg.y1);       break;    case 'q': return path.createSVGPathSegCurvetoQuadraticRel(seg.x,seg.y,seg.x1,seg.y1);       break;    case 'T': return path.createSVGPathSegCurvetoQuadraticSmoothAbs(seg.x,seg.y);    break;    case 't': return path.createSVGPathSegCurvetoQuadraticSmoothRel(seg.x,seg.y);    break;    case 'A': return path.createSVGPathSegArcAbs(seg.x,seg.y,seg.r1,seg.r2,seg.angle,seg.largeArcFlag,seg.sweepFlag); break;    case 'a': return path.createSVGPathSegArcRel(seg.x,seg.y,seg.r1,seg.r2,seg.angle,seg.largeArcFlag,seg.sweepFlag); break;    case 'z':    case 'Z': return path.createSVGPathSegClosePath();         break;  }}

最后,您可以选择通过定期采样SVG路径并绘制到画布来绘制HTML5画布的路径。(请注意,不需要为此显示SVG路径;您可以完全在Javascript中构建SVG路径元素并对其进行采样):

演示:[http](http://phrogz.net/svg/progressively-drawing-svg-

path.html)://phrogz.net/svg/progressively-drawing-svg-
path.html

相关代码:

function startDrawingPath(){  points = [];  timer = setInterval(buildPath,1000/drawFPS);}// Assumes that 'orig' is an SVG pathfunction buildPath(){  var nextPoint = points.length * distancePerPoint;  var pathLength = orig.getTotalLength();  if (nextPoint <= pathLength){    points.push(orig.getPointAtLength(nextPoint));    redrawCanvas();  } else stopDrawingPath();}function redrawCanvas(){  clearCanvas();  ctx.beginPath();  ctx.moveTo(points[0].x,points[0].y);  for (var i=1;i<points.length;i++) ctx.lineTo(points[i].x,points[i].y);  ctx.stroke();}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/615236.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号