javascript - 有没有办法在 SVG 元素中绘制光栅图像?

标签 javascript css svg

我有以下包含要在网页 map 上显示的标记的 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 的东西吗?属性来指定每个图像的查看窗口?

最佳答案

包装<image>元素进入内部 <svg>元素,在 <svg> 上指定一定的宽度和高度元素和 background-position 上的“<image> ”使用 x 的元素和 y属性可能是一个解决方案:

<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>

(参见 test on Tinkerbin 。)

您还可以使用 clip-path属性,但我想这比上面的解决方案更乏味。

关于javascript - 有没有办法在 SVG 元素中绘制光栅图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13410496/

相关文章:

javascript - 如何使用 JavaScript 将访问参数传递到 HTML 页面

html - CSS图像最大高度不保持纵横比

html - Css 侧边栏高度 100% in foundation

javascript - 不同 div 中 2 个相同布局的单个滚动?

javascript - Angular 2插值中的连接变量

javascript - Jquery 插件。每个实例的选项

javascript - 为什么使用数组作为键访问对象中的元素有效?

python - 如何获取(网络)字体中单个字符的大小?

css - D3 树布局 : weird behavior of shadow filter when . nodeSize() 被应用

javascript - 绘图 svg :circle on d3. svg.area().interpolate ("basis")