简体   繁体   English

svg.js缩放和移动/中心的奇怪交互

[英]svg.js strange interaction of scale and move/center

I'm not very familiar working with svgs in js but here is something that is definitely strange. 我对在js中使用svgs不太熟悉,但这绝对是一件很奇怪的事情。 I'm having an arrow and then a path that is supposed to fill that arrow to a certain extend. 我有一个箭头,然后是一条应该将箭头填充到一定程度的路径。 looks like this: 看起来像这样: 图片1 Now my aim is to be able to scale the white part but it should still stay inside that arrow 现在我的目标是能够缩放白色部分,但它仍应留在该箭头内 图片2 . Now the weird thing is that I cannot figure out how move the white part back into the right place. 现在奇怪的是,我无法弄清楚如何将白色部分移回正确的位置。 I've tried different attempts. 我尝试了不同的尝试。 here is my current code (it works for scaleFactor 1 but not for any other): 这是我当前的代码(它适用于scaleFactor 1,但不适用于其他任何代码):

var draw = SVG('arrow').size(500, 500);
    var arrowPath=draw.polyline('10,243.495 202.918,15.482 397.199,245.107').fill('none').stroke({ width: 20 });

    var arrow=draw.group();
    arrow.add(arrowPath.clone());

    var scaleFactor=0.5;

    var fillArrow=draw.path('M357.669,198.387c-41.747,35.357-95.759,56.678-154.751,56.678c-58.991,0-113.003-21.32-154.75-56.676l154.75-182.907  L357.669,198.387z');
    fillArrow.fill('#ffffee');
    var moveX=(arrowPath.width()/2-fillArrow.width()/2)/scaleFactor+9.5;
    console.log(moveX);
    fillArrow.center(arrowPath.cx(), arrowPath.cy()).scale(scaleFactor);
//another attempt was
fillArrow.move(moveX,0);

When you are scaling, rotating and translating in SVG you are doing coordinate transforms. 在SVG中缩放,旋转和平移时,您正在执行坐标变换。 That is, you are actually not changing the object you are drawing but the coordinate system that you are drawing the object in. Think of it as pixel has a certain size on your screen, and if your do svgObject.scale(0.5) the pixel becomes half the size. 也就是说,实际上您不是在更改要绘制的对象,而是要在其中绘制对象的坐标系。可以将其视为像素在屏幕上具有一定的大小,如果您执行svgObject.scale(0.5)该像素变成一半的大小。

So if you draw a square by path('M10,10 L20,10 L20,20 L10,20 z') and then apply scale(0.5) it will look like you have drawn a path that looks like path('M5,5 L10,5 L10,10 L5,10 Z') 因此,如果您按path('M10,10 L20,10 L20,20 L10,20 z')绘制正方形path('M10,10 L20,10 L20,20 L10,20 z')然后应用scale(0.5) ,则看起来就像您绘制的路径看起来像path('M5,5 L10,5 L10,10 L5,10 Z')

This might sound strange at first but, but alot of geometrical calculations becomes much simpler when you can do this. 一开始听起来可能很奇怪,但是当您执行此操作时,许多几何计算会变得更加简单。

You want to scale around the tip of the arrow (make sure that does not move). 您想要围绕箭头的尖端缩放(确保不会移动)。 Then you should place that point in the origo (0,0) and draw the object around that point. 然后,应将该点放置在origo (0,0)并围绕该点绘制对象。 Do that in a group. 分组进行。 Because then you can translate the group coordinate system to the correct position. 因为这样您才能将组坐标系平移到正确的位置。

var draw = SVG('arrow').size(600, 600);

// create a group
var arrow = draw.group();

// Draw the arrow path in the group
// I have placed the "tip" of the arrow in (0,0)
var arrowPath = arrow.polyline('-250,250 0,0 250,250').fill('none').stroke({
    width: 20
});

// Draw the fill arrow in the group. Again, the point 
// I which to scale around is placed at (0,0)
var fillArrow = arrow.path('M0,0L150,150,-150,150z').fill('#ffffee');

// Move the group to the position we like to display it in the SVG
arrow.move(260, 20);

var scaleFactor = 0.5
fillArrow.scale(scaleFactor);

And here is a working example where you can test and change the scale factor. 这是一个工作示例,您可以在其中测试和更改比例因子。 http://jsfiddle.net/ZmGQk/1/ http://jsfiddle.net/ZmGQk/1/

And here is a good explanation on how the translate, rotate and scale works. 这是有关平移,旋转和缩放如何工作的很好的解释。 http://commons.oreilly.com/wiki/index.php/SVG_Essentials/Transforming_the_Coordinate_System http://commons.oreilly.com/wiki/index.php/SVG_Essentials/Transforming_the_Coordinate_System

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

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