【问题标题】:Animate SVG path动画 SVG 路径
【发布时间】:2016-02-14 22:52:03
【问题描述】:

我可以通过动画的原点旋转 SVG 路径吗?我尝试在 svg 组上使用 CSS 关键帧,但这会使路径消失。理想情况下,我不想使用 javascript,但如果没有其他方法我会。

路径位于 css 样式的视差内,这可能会与 SVG 视图框混淆,因为坐标似乎不正确。

【问题讨论】:

标签: javascript html css web svg


【解决方案1】:

试试这个,我这里用的是 ES6 类。

var NS="http://www.w3.org/2000/svg";  
var SVG=function(el){
    return document.createElementNS(NS,el);
}
 
 svg = SVG('svg');
    svg.width='100%';
    svg.height='100%';
document.body.appendChild(svg);


class myPath {
     constructor() {
     this.path=SVG('path');
     var d="M 10,90 Q 100,15 200,70 ";
     this.path.setAttribute("d", d);
     this.path.setAttribute("fill", "#F7931E");
     this.path.addEventListener("click",this,false);
   }
   get draw(){
       return this.path;
   }
 }
 
 myPath.prototype.rotate = function(x) {
   return svg.createSVGTransformFromMatrix(svg.createSVGMatrix().rotate(x));
}

myPath.prototype.animate = function() {
       self = this.path;
       self.transform.baseVal.appendItem(this.rotate(5));
};

 myPath.prototype.handleEvent= function(evt){
  self = evt.target;  
  console.log(self.getAttribute('d'));
     
 self.move = setInterval(()=>this.animate(),100);
       
}

svg.appendChild(new myPath().draw);

【讨论】:

    猜你喜欢
    • 2016-01-06
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 2021-04-18
    • 2020-03-14
    • 2015-04-10
    • 2017-03-30
    相关资源
    最近更新 更多