简体   繁体   English

使用javascript更改SVG属性

[英]Changing the SVG attributes with javascript

So trying to get my head around how to change a SVG using javascript (I tried d3js library. And I have some issues because I am not sure wether to use it or not). 因此,我想着手解决如何使用JavaScript更改SVG (我尝试了d3js库。由于我不确定是否要使用它,因此我d3js一些问题)。

I have created a simple icon (a star and a circle, to have something). 我创建了一个简单的图标(一个星和一个圆,有一些东西)。 As far as I understand, what I need to do is to change the attributes of the <g> tag. 据我了解,我需要做的是更改<g>标记的属性。 The icon have three id 's, start circle(should be the whole picture), star (which is the star) and circle Here is the SVG image. 该图标具有三个id ,开始圆(应为整个图片), star (即星形)和circle这是SVG图像。

<svg
        xmlns:dc="http://purl.org/dc/elements/1.1/"
        xmlns:cc="http://creativecommons.org/ns#"
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns:svg="http://www.w3.org/2000/svg"
        xmlns="http://www.w3.org/2000/svg"
        xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
        xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
        width="120"
        height="120"
        viewBox="0 0 120 120"
        id="starcircle"
        version="1.1"
        inkscape:version="0.91 r13725"
        sodipodi:docname="starcircle.svg">
    <defs
            id="defstarcircle-def" />
    <sodipodi:namedview
            id="starcircle"
            pagecolor="#ffffff"
            bordercolor="#666666"
            borderopacity="1.0"
            inkscape:pageopacity="0.0"
            inkscape:pageshadow="2"
            inkscape:zoom="2.32"
            inkscape:cx="53.146552"
            inkscape:cy="47.894737"
            inkscape:document-units="px"
            inkscape:current-layer="layer1"
            showgrid="false"
            units="px"
            inkscape:window-width="1440"
            inkscape:window-height="821"
            inkscape:window-x="0"
            inkscape:window-y="1"
            inkscape:window-maximized="1" />
    <metadata
            id="metadata5539">
        <rdf:RDF>
            <cc:Work
                    rdf:about="">
                <dc:format>image/svg+xml</dc:format>
                <dc:type
                        rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
                <dc:title></dc:title>
            </cc:Work>
        </rdf:RDF>
    </metadata>
    <g
            inkscape:label="Layer 1"
            inkscape:groupmode="layer"
            id="scLayer"
            transform="translate(0,-932.36216)">
        <path
                sodipodi:type="star"
                style="opacity:0.5;fill:#00aeef;fill-opacity:1;stroke:#ffffff;stroke-opacity:1"
                id="star"
                sodipodi:sides="5"
                sodipodi:cx="59.05172"
                sodipodi:cy="992.44832"
                sodipodi:r1="49.260658"
                sodipodi:r2="24.630329"
                sodipodi:arg1="-0.27469391"
                sodipodi:arg2="0.35362463"
                inkscape:flatsided="false"
                inkscape:rounded="0"
                inkscape:randomized="0"
                d="m 106.46551,979.08623 -24.307496,21.89157 4.253455,32.4346 -28.331542,-16.3529 -29.532743,14.0681 6.79764,-31.99826 -22.505694,-23.74002 32.532715,-3.42306 15.623459,-28.74026 13.308684,29.88267 z"
                inkscape:transform-center-x="-0.60060082"
                inkscape:transform-center-y="-4.1291064" />
        <ellipse
                style="opacity:0.5;fill:#0d00ef;fill-opacity:1;stroke:#ffffff;stroke-opacity:1"
                id="circle"
                cx="58.620693"
                cy="991.80182"
                rx="16.379311"
                ry="16.594828" />
    </g>
</svg>

So lets say that I want to rotate the star, for now only rotate it 180 degrees . 因此,可以说我想旋转星星,现在仅将其旋转180 degrees I would need to get the SVG image, then the shape, then append something like 我需要获取SVG图片,然后获取形状,然后添加类似

rotate(180 , 50 , 50)

I tried doing it using d3, because it seemed like you needed a library to do it, but nothing I do seem to change the actual document. 我尝试使用d3进行此操作,因为似乎您需要一个库来执行此操作,但是我似乎并没有更改任何实际文档。 So what is the easiest way. 那么最简单的方法是什么。

I created a jsfiddle with this example, and my attempt to do the rotate, but so far no luck. 我用此示例创建了一个jsfiddle,并且尝试进行旋转,但到目前为止还没有运气。

I also hid the circle, mostly just to prove that the code is at least finding the objects, even if the hide function is a css option, and I want to do it using SVG transform 我也隐藏了圆圈,主要是为了证明代码至少可以找到对象,即使hide函数是一个css选项,我也想使用SVG变换来实现。

https://jsfiddle.net/qyt5o0s7/ https://jsfiddle.net/qyt5o0s7/

Well your approach in the fiddle is wrong, Since you are not sure what the library to use or how to use it I like to give you the steps. 好吧,您摆弄小提琴的方法是错误的,因为您不确定要使用什么库或如何使用它,所以我想为您提供步骤。

Use d3.js , It's great. 使用d3.js ,太好了。

You just have to add a <g> , then create the star inside the <g> . 您只需要添加<g> ,然后在<g>内创建星形。 Give the <g> and id of something like starGroup 给出<g>和id之类的starGroup

<g id="starGroup"></g>

then you can use d3 to translate the group. 那么您可以使用d3来翻译组。

d3.select('#starGroup')
.style(...);

Your javascript code was appending a <g> element to the #star element and then rotating the #star element. 您的JavaScript代码是将<g>元素附加到#star元素,然后旋转#star元素。 Remove the append() function and your code will then rotate the #star element. 删除append()函数,然后您的代码将旋转#star元素。 If you want to rotate the #star element around its center then you will also have add tranlate operation before and after the rotate operation. 如果要围绕其中心旋转#star元素,则在旋转操作之前和之后还需要添加tranlate操作。 For example, change... 例如,更改...

vard3star = d3.select("#star").append("g")
.attr("transform","rotate("+10 + "," + 15 +","+ 15 +")");

to... 至...

var d3star = d3.select("#star").attr("transform","translate(60,992) rotate(10,15,15) translate(-60,-992)");

Also note that you were missing a space between var and d3star which was resulting in a global variable called vard3star. 还要注意,您缺少var和d3star之间的空格,这导致了一个名为vard3star的全局变量。

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

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