有没有一种方法可以在SVG元素内部精灵化栅格图像?

3
我有以下包含标记的SVG元素,需要在网页地图上显示。标记是由<image>元素组成,引用各种小型PNG图像:
<svg id="OL_105_svgRoot" width="1246" height="373" viewBox="0 0 1246 373">
 <g id="OL_105_root" style="visibility: visible;" transform="">
  <g id="OL_vroot">
   <image id="P115" cx="843" cy="203" r="1" x="827" y="188" width="32" height="32" href="spider.png" ...>
   <image id="P119" cx="453" cy="269" r="1" x="437" y="254" width="32" height="32" href="zoo.png" ...>
   <image id="P123" cx="628" cy="82" r="1" x="612" y="67" width="32" height="32" href="wild.png" ...>
   <image id="P131" cx="10495" cy="69" r="1" x="1034" y="53" width="32" height="32" href="export.png" ...>
...
  </g>
 </g>
</svg>

我能否用单个大图文件替换这些图片,然后使用类似于CSS中的background-position属性来指定每个图片的可视窗口?

1个回答

2
<image>元素包装到内部的<svg>元素中,指定<svg>元素的特定宽度和高度以及<image>元素上使用xy属性的"background-position"可能是一种解决方案。
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">
  <svg width="17" height="19">
    <image xlink:href="http://s14.postimage.org/f66u116ap/rainbow_Hello_World4.png" width="188" height="19"/>
  </svg>
  <svg width="17" height="19" x="12" y="19">
    <image xlink:href="http://s14.postimage.org/f66u116ap/rainbow_Hello_World4.png" width="188" height="19" x="-17"/>
  </svg>
  <svg width="17" height="19" x="35" y="6">
    <image xlink:href="http://s14.postimage.org/f66u116ap/rainbow_Hello_World4.png" width="188" height="19" x="-34"/>
  </svg>
  <svg width="17" height="19" x="46" y="14">
    <image xlink:href="http://s14.postimage.org/f66u116ap/rainbow_Hello_World4.png" width="188" height="19" x="-51"/>
  </svg>
  <svg width="17" height="19" x="60" y="24">
    <image xlink:href="http://s14.postimage.org/f66u116ap/rainbow_Hello_World4.png" width="188" height="19" x="-68"/>
  </svg>
</svg>

(在Tinkerbin上查看测试。)

您也可以使用clip-path属性,但我猜这比上面的解决方案更加费时。


我无法更改文档结构,因为<image>元素是由地图API自动生成的。是否可以通过更改图像元素的属性来实现此操作? - Question Overflow
是的,可以使用“clip-path”属性来实现,但很抱歉我现在无法详细说明。 - Thomas W
非常感谢您!这也可以用于精灵动画:http://jsfiddle.net/syyvp700/ - VirtuosiMedia

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接