首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何旋转SVG内蒙版?

如何旋转SVG内蒙版?
EN

Stack Overflow用户
提问于 2019-02-07 00:49:30
回答 1查看 209关注 0票数 2

我有一个SVG遮罩,我想旋转里面的元素。然而,当我尝试它时,它看起来像是整个蒙版正在旋转。

我试了试这个SO post,但是小提琴不工作(图像不再显示)。这是它最初的样子

怎么了

我希望发生的事情

我只想旋转" liquid“元素,但不知何故,liquid元素被旋转到了错误的方向。

这是SVG (带掩码元素):

代码语言:javascript
复制
<svg width="200px" height="200px" viewBox="0 0 200 200">
    <defs>
        <rect id="path-1" x="75" y="65" width="50" height="100" rx="5"></rect>
    </defs>
    <g id="bottle" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
        <rect id="bottle-lid" stroke="#916548" stroke-width="5" x="82.5" y="52.5" width="35" height="15" rx="5"></rect>
        <rect id="bottle-holder" stroke="#916548" stroke-width="5" x="117.5" y="55.5" width="30" height="7" rx="3.5"></rect>
        <mask id="mask-2" fill="white">
            <use xlink:href="#path-1"></use>
        </mask>
        <g id="Mask"></g>
        <rect id="bottle-liquid" fill="#3498DB" mask="url(#mask-2)" x="40" y="80" width="120" height="120"></rect>
        <rect id="bottle-outline" stroke="#916548" stroke-width="5" mask="url(#mask-2)" x="77.5" y="67.5" width="45" height="95" rx="5"></rect>
    </g>
</svg>

这是JS (我使用的是GSAP库)

代码语言:javascript
复制
const liquid = document.querySelector("#bottle-liquid")

const tl = new TimelineMax()

tl.to(liquid, 1, {
  rotation: 20
})

我怎么才能旋转一个被屏蔽的SVG中的某个元素呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-07 01:03:07

我对SVG做了一些更改,其中最重要的是:我将瓶子液体放入g元素中,并将掩码应用于g元素,将转换应用于瓶子液体。我希望它能帮上忙。

代码语言:javascript
复制
<svg width="200px" height="200px" viewBox="0 0 200 200">
    <defs>
        <rect id="path-1" x="75" y="65" width="50" height="100" rx="5"></rect>
      
      
        <mask id="mask-2" fill="white">
            <use xlink:href="#bottle-outline"></use>
        </mask>
      
    </defs>
    <g id="bottle" stroke-width="5" stroke="#916548" fill="none" fill-rule="evenodd">
        <rect id="bottle-lid"  x="82.5" y="52.5" width="35" height="15" rx="5"></rect>
        <rect id="bottle-holder"  x="117.5" y="55.5" width="30" height="7" rx="3.5"></rect>
        <g mask="url(#mask-2)">
        <rect id="bottle-liquid" stroke="none" fill="#3498DB"  x="40" y="80" width="120" height="120" transform="rotate(20 100 140)"></rect>
        </g>
        <rect id="bottle-outline" x="77.5" y="67.5" width="45" height="95" rx="5"></rect>
    </g>
</svg>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54558645

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档