0

我创建了一个组,然后缩放。我只想复制缩放组的形状而不是其他属性。特别是我想摆脱转换后的矩阵。我需要一个独立于缩放组的新组。我不知道是否可以这样做?这里的代码:

车组:

<svg id="game" version="1.1" xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink" onload="loadFunction()" >
    <!-- Car -->
    <g id="exCar" x="50" y="500"   transform="" >
        <path id="front" d="M 40 500  Q 110 450 110 500 L 40 500" fill="pink" stroke="black" />
        <path id="window" d="M 40 500 L 50 530 L 100 530 L 110 500 L 40 500 "stroke="black" />
        <path id="sides" d="M 40 500 L 40 580 L 50 560 Q 55 550 50 530 L 40 500 M 110 500 L 110 580 L 100 560 Q 95 550 100 530 L 110 500" fill="pink" stroke="black" />
        <path id ="back" d="M 40 580 L 100 560  L 40 580" fill="pink" stroke="blue" />
        <path id="wheels" d="M 40 475 L 30 475 L 30 500 L 40 500 z M 110 475 L 120 475 L 120 500 L 110 500 z M 40 545 L 30 545 L 30 570 L 40 570 z" fill="yellow" stroke="black" />
        <image id="carpicture2" x="50" y="455" xlink:href="img.jpg"></image>
        <text id="carName" x="50" y="575"></text>
    </g>
</svg>

调用函数的按钮:

<rect x="120" y="600" width="80" height="30" stroke:#FF0066"  onclick="cloning()"/> 

复制功能:

function cloning() {
    var newCar = document.getElementById("exCar").cloneNode(true);
    newCar.setAttribute("x", 400);
    newCar.setAttribute("y", 600);
    document.getElementById("game").appendChild(newCar);
    alert("!!!!");
};

缩放:

document.getElementById("exCar").setAttribute(
        "transform",
        "matrix(" + result / 100 + ",0,0," + result / 100 + ","
                + (x - (result / 100 * x)) + "," + (y - (result / 100 * y))
                + ")");

(结果是从滑块计算出来的)

4

1 回答 1

0

好的,我想我现在明白了这个问题,你正在点击克隆,所以 exCar 在那个时候已经被缩放了。

我想唯一的选择是清除克隆函数内的转换矩阵:

newCar.setAttribute("transform", "matrix(1,0,0,1,0,0)");

并进行翻译:

newCar.setAttribute("transform", "translate(350,100)");

http://jsfiddle.net/mihaifm/ZR4r8/5/

于 2012-04-04T11:02:30.213 回答