Snap SVG-移动加载的元素 [英] Snap SVG - Moving a loaded element around

查看:127
本文介绍了Snap SVG-移动加载的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Snap SVG库 在SVG图形中移动元素在屏幕上.这是元素的代码:

I am trying to use Snap SVG library to move an element in an SVG graphic around the screen. This is the code of the element:

<g id="butterfly">
  <path id="wings" fill="#FFFFFF" stroke="#CFEA90" stroke-width="4" stroke-miterlimit="10" d="M139.741,74.038c-5.847-1.477-11.794,1.029-14.605,5.736c-0.463-5.791-5.095-10.963-11.275-12.115c-7.017-1.309-13.365,3.059-14.179,9.755c-0.654,5.381,2.469,10.622,7.36,13.155c-0.143,0.146-0.28,0.284-0.403,0.402c-3.157,3.055-2.81,7.082,0.777,8.994c3.586,1.912,9.053,0.985,12.211-2.071c0.836-0.809,1.773-2.517,2.632-4.62c-0.068,2.586,0.13,4.835,0.632,6.038c1.724,4.127,6.377,7.274,10.394,7.031c4.017-0.244,5.876-3.786,4.152-7.913c-0.067-0.16-0.14-0.343-0.215-0.538c5.45-0.264,10.315-3.753,11.775-8.957C150.814,82.459,146.669,75.79,139.741,74.038z"/>
  <path id="body" fill="#97BD40" d="M123.467,99.008c-0.3,1.287-1.587,2.086-2.873,1.786l0,0c-1.287-0.3-2.086-1.587-1.786-2.873l5.159-22.099c0.3-1.287,1.587-2.086,2.873-1.786l0,0c1.287,0.3,2.086,1.587,1.786,2.873L123.467,99.008z"/>
</g>

我想在屏幕上移动此元素(例如,我想先向上然后向左然后再向下移动到初始位置),所以我首先创建一个 canvas 并获得元素.我使用以下代码:

I want to move this element around the screen (e.g., I want to move it up and then left and then down again to the initial position), so I first create a canvas and get the element. I use this code:

var canvas = Snap("div#canvas");

Snap.load("MyImage.svg", function (image) {

    var butterfly = image.select("g#butterfly");    
    canvas3.append(butterfly);

    // Now I want to move the element around...
}

有人有想法吗?

我发现了有关移动圆或矩形的信息,这些圆或矩形具有 x y cx cy 易于修改,但就我而言,我没有这些属性...

I've found information about moving a circle or a rectangle, which have attributes like x, y, cx or cy that are easy to modify, but in my case I don't have those attributes...

推荐答案

设置转换在蝴蝶上.您想要一个平移变换来移动它.例如

Set a transform on the butterfly. You want a translate transform to move it. E.g.

butterfly.transform("t100,100");

语法与Raphael相同.但是您也可以使用快照矩阵

That syntax is the same as Raphael. But you can also use a Snap matrix

var t = new Snap.Matrix()
t.translate(100, 100);
butterfly.transform(t); 

这篇关于Snap SVG-移动加载的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆