简体   繁体   English

如何通过动画更改Snap.svg中多边形的点?

[英]How to change the points of a polygon in Snap.svg through animation?

I'm trying to implement a button with a certain polygon on it, which when pressed changes the polygon to something else. 我正在尝试在其上实现一个带有某个多边形的按钮,按下该按钮会将多边形更改为其他内容。 For example the play icon on the button changing to stop icon. 例如,按钮上的播放图标更改为停止图标。 Ideally the icon should be a polygon with three points depicting the play symbol. 理想情况下,图标应该是一个多边形,其中三个点描绘了游戏符号。 After animation it turns into a polygon of four points(A square) depicting the stop symbol. 在动画之后,它变成一个描绘停止符号的四个点(一个正方形)的多边形。

I tried doing it this way: 我试过这样做:

var paper = Snap('svg');
var tpts = [100,100,100,130,120,115];
var sqpts = [100,100,100,130,130,130,130,100];
var tri = paper.polygon(sqpts);
tri.attr({
    id:"tri",
    fill:"#555555"
});

sqrFunc = function(){
    tri.animate({"points":sqpts},1000,mina.linear);
}
triFunc = function(){
    tri.animate({"points":tpts},1000,mina.linear);
}

On clicking the button once I call sqrFunc and on clicking it again I call triFunc. 单击按钮一次我调用sqrFunc并再次单击它我调用triFunc。 I tried assigning different point arrays to "points" because when I query tri.attr("points") I get the assigned points as return value. 我尝试将不同的点数组分配给“点”,因为当我查询tri.attr(“points”)时,我将指定的点作为返回值。 However, trying to assign the arrays like this ends up in errors. 但是,尝试分配这样的数组会导致错误。 Any help regarding this problem is greatly appreciated. 非常感谢有关此问题的任何帮助。

I don't get any error with your code, however the tranformation does not happen as wished because the triangle has only 3 points. 我的代码没有任何错误,但转换不会发生,因为三角形只有3个点。

I fixed it with 我修好了

var tpts = [100,100,100,100,100,130,120,115];

try this 尝试这个

var paper = Snap('svg');
var tpts = [100,100,100,100,100,130,120,115];
var sqpts = [100,100,100,130,130,130,130,100];

var tri = paper.polygon(tpts);
tri.attr({
    id:"tri",
    fill:"#555555"
});

sqrFunc = function(){
    tri.animate({"points":sqpts},1000,mina.linear);
}
triFunc = function(){
    tri.animate({"points":tpts},1000,mina.linear);
}

setTimeout(sqrFunc, 200);
setTimeout(triFunc, 1200);

another method is to use paths 另一种方法是使用路径

var paper = Snap('svg');
var tri = paper.path("M 10 10, L 10 50, L 50 25, Z");
tri.attr({
    id:"tri",
    fill:"#555555"
});

sqrFunc = function(){
    tri.animate({d:"M 10 10, L 10 50, L 50 50, L50 10,Z"},1000,mina.linear);
}
triFunc = function(){
    tri.animate({d:"M 10 10, L 10 50, L 50 25, Z"},1000,mina.linear);
}
setTimeout(sqrFunc, 200);
setTimeout(triFunc, 1200);

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

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