【问题标题】:How to reset SVG transformations (i.e. return an SVG object to its original state)?如何重置 SVG 转换(即将 SVG 对象恢复到其原始状态)?
【发布时间】:2012-10-28 08:56:31
【问题描述】:

我正在构建一个应用程序,用户可以在其中无限次操作 svg 对象(例如图像),即旋转和缩放它们。我使用 Raphael.js 来处理 SVG。

如何在应用新转换之前将对象“重置”到其初始状态,以使新转换不会与之前的转换重叠?

Bellow 是一个测试(这是它的一个工作小提琴:jsfiddle.net/5TsW6/3/),在应用与第一个相同的转换之前,我尝试在第二个视口中重置对象,但我的尝试没有效果,因为两个视口中的结果不同,而应该是相同的:

<div id="wrapper1" style="width:250px; height:250px; outline:1px solid invert; float:left; margin-right:5px;"></div>
<div id="wrapper2" style="width:250px; height:250px; outline:1px solid invert; float:left;"></div>​

还有脚本:

var img_width=100, img_height=75,
    canvas1, canvas2,
    image1, image2,
    w1, w2, // contours
    x,y,    // attributes "x" and "y"
    rx,ry;  // coordinates for the rotation pivot

// Create the canvases
canvas1 = new Raphael(document.getElementById("wrapper1"), 250, 250);  
canvas2 = new Raphael(document.getElementById("wrapper2"), 250, 250);


// Add the images to the canvases    
image1 = canvas1.image("http://picselbocs.com/projects/cakemyface/image-gallery/intermediate/3295094303_2b50e5fe03_z.jpg",0,0,img_width,img_height);
image2 = canvas2.image("http://picselbocs.com/projects/cakemyface/image-gallery/intermediate/3295094303_2b50e5fe03_z.jpg",0,0,img_width,img_height);


// Modify (x,y) 
x = 40;
y = 80;
image1.attr({"x":x,"y":y});
image2.attr({"x":x,"y":y});


// Add the objects' contours
canvas1.rect(x,y,img_width,img_height).attr({"stroke-width":"1px","stroke":"#00F"});
canvas2.rect(x,y,img_width,img_height).attr({"stroke-width":"1px","stroke":"#00F"});

// Compute the rotation pivot coordinates (the pivot should coincide with the object's center)
var scaling = 1.2;
rx = (x + img_width / 2) * scaling;
ry = (y + img_height / 2) * scaling;

// Apply transformations
image1.transform("S"+scaling+","+scaling+",0,0R45,"+rx+","+ry);
image2.transform("S"+scaling+","+scaling+",0,0R45,"+rx+","+ry);



// ----- Here starts second transformation ------ //
image2.transform("").transform("S"+scaling+","+scaling+",0,0r45,"+rx+","+ry);   // I've attempted to reset the transform string, but with no effect
​

【问题讨论】:

    标签: javascript svg raphael


    【解决方案1】:

    重置正在工作。您在第二次转换中打错了字,使用r45 而不是R45,意思是“在应用之前考虑之前的转换”,因此存在差异。

    【讨论】:

    • 在这种情况下,我要疯了。因为在我正在开发的应用程序(picselbocs.com/projects/cakemyface)中,如果我旋转一个对象,然后调整它的大小(到目前为止,很好),然后再次旋转它,在最后一次旋转中,元素被错误地移位,我只是不明白为什么。看来我完全按照小提琴中的方式计算了所有参数,但是出了点问题。
    • 只是一个想法:在你的 cakemyface 中,如果你想制作使用 &lt;svg&gt; 元素定位的“选择矩形”和“拖动角”坐标空间,以便可以将它们用作拖动手柄并让它们遵循所有转换,您可以在这里使用我的 get_metrics() 函数:stackoverflow.com/a/13111598/1691517.
    猜你喜欢
    • 1970-01-01
    • 2021-09-05
    • 2014-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多