简体   繁体   English

使用Snap.svg缩放多边形

[英]Using Snap.svg to scale a polygon

I have a polygon created below: 我在下面创建了一个多边形:

var s = Snap('#test');

var polygon = s.polyline(200, 286, 250, 200, 350, 200, 400, 286, 350, 373, 250, 373);

polygon.scale(2, 2);

How do I get the polygon to scale? 如何缩放多边形? I want it to grow to twice its size from the center. 我希望它从中心长到其大小的两倍。

You would apply a transform to it. 您将对其应用转换。

var polygon = s.polyline(200, 286, 250, 200, 350, 200, 400, 286, 350, 373, 250, 373);
polygon.transform('s2')

's' in this case is for scale (also t and r for translate and rotate). 在这种情况下,“ s”代表比例尺(t和r代表平移和旋转)。 You can also give it an x,y amount of scale, as well as a center point to scale from. 您还可以给它一个x,y比例尺,以及一个从其缩放的中心点。 So you could do... 所以你可以做...

polygon.transform('s2,3,100,100') 

To scale x by 2, y by 3 based on a center of 100,100 to scale from. 将x缩放为2,将y缩放为3,以100,100为中心。

You can read this SO answer for further details on transform strings if desired. 如果需要,您可以阅读此SO答案以获取有关转换字符串的更多详细信息。

You could change Scale value by change polygonScale value. 您可以通过更改polygonScale值来更改Scale值。

Don't remove viewBox attribute 不要删除viewBox属性

https://jsfiddle.net/nsrrexdd/ https://jsfiddle.net/nsrrexdd/

 var s = Snap('#test'), tl = new TimelineMax(), polygonScale = 2; // control var polygon = s.polyline(200, 286, 250, 200, 350, 200, 400, 286, 350, 373, 250, 373); polygon.attr({fill: 'red', class: 'myPolygon'}); // polygon.scale(2, 2); tl.set(".myPolygon", { transformOrigin: 'center', scale: polygonScale }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.19.0/TweenMax.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/snap.svg/0.4.1/snap.svg-min.js"></script> <svg id="test" viewBox="0 0 500 500"></svg> 

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

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