简体   繁体   English

动画SVG路径

[英]Animate SVG path

Can I rotate an SVG path via the point of it's origin with an animation? 我可以通过动画通过其原点旋转SVG路径吗? I've tried using CSS keyframes on svg groups but that makes the path disappear. 我试过在svg组上使用CSS关键帧,但这会使路径消失。 Ideally I don't want to be using javascript but if there's no other way I will. 理想情况下,我不想使用javascript,但如果没有其他方法,我会使用。

The path is inside a css styled parallax which might be messing with the SVG viewbox as the coordinates don't seem right. 该路径位于css样式的视差内,这可能会与SVG视图框混淆,因为坐标似乎不正确。

Try this, I'm using here ES6 class. 试试这个,我在这里使用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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM