angularjs - 如何将图像作为墙砖放置在 svg 路径中

标签 angularjs svg

嗨,我有 svg 路径,我在其中放置图像 /image/WPksR.jpg从 。当我将其仅显示在路径中心而不填充其他区域时,如下我想要的图像 /image/rNkJD.jpg

如何将图像作为瓷砖放置在墙上?

<defs>
  <pattern  id="img1" patternUnits="userSpaceOnUse" width="100" height="100">
    <image x="0" y="0" xlink:href="stile.jpg"  width="100" height="100" />
  </pattern>
</defs>
 <path d="M 752 180 v 192 h 215 V 145 L 752 180 Z M 916 358 l -130 0.246 v -78.369 L 916 270 V 358 Z"/>
</path>



 <script>
jQuery(function($){
    $('path').click(function(){


       this.style.fill = "url(#img1)";
        alert(this.id);
    });
});
</script>

当我用 jquery 单击路径时,正在其上应用图像

最佳答案

问题是您的图像有白色边框。如果您想使用该图像,则需要将图像放入带有 viewBox 的符号内,该 viewBox 会裁剪图像的白色边框,如下所示:

<svg width="100%" height="100%" viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg">
<defs>
<symbol id="s" viewBox="2.5 2.5 38 35">
<image x="0" y="0" xlink:href="https://i.imgur.com/NFhr6hq.jpg"  width="48" height="45" />
</symbol>
  <pattern  id="img1" patternUnits="userSpaceOnUse" width="30" height="30">
   <use xlink:href="#s" width="30" height="30" />
  </pattern>
</defs>
<rect width="100%" height="100%" fill="url(#img1)" />
</svg>

或者,您可以使用不带白色边框的不同图像,在这种情况下,您可以使用@Alexandr_TT 的解决方案。

更新:

OP 正在评论:

i have updatted question, there i have no rect but path . when i click on path , jquery update path fill area with image

在这种情况下,您不是填充矩形,而是像这样填充路径:

<svg viewBox="750 145 230 230" xmlns="http://www.w3.org/2000/svg">
<defs>
<symbol id="s" viewBox="3.1 2.5 38 35">
<image x="0" y="0" xlink:href="https://i.imgur.com/NFhr6hq.jpg"  width="48" height="45" />
</symbol>
  <pattern  id="img1" patternUnits="userSpaceOnUse" width="38" height="35">
   <use xlink:href="#s" width="38" height="35" />
  </pattern>
</defs>
<!--<rect width="100%" height="100%" fill="url(#img1)" />-->
<path d="M 752 180 v 192 h 215 V 145 L 752 180 Z M 916 358 l -130 0.246 v -78.369 L 916 270 V 358 Z" fill="url(#img1)"/>
</svg>

关于angularjs - 如何将图像作为墙砖放置在 svg 路径中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58006228/

相关文章:

javascript - 需要 angularjs SPA 的建议

javascript - AngularJS 组件可以修改其包装元素吗?

javascript - 在 sum 函数中重复两次

jquery - 随机化 svg 笔划的颜色

javascript - SVG <path href =""> 问题

javascript - 如何将 $scope 传递给 angular.js 中的服务?

javascript - ng-disabled 不使用 <i> 标签使用 font-awesome

javascript - 将 SVG 对象存储在数组中以供加载

xml - 如何在 SVG 文件中嵌入任意文本/数据?

html - 如何使用 <object> 在 Firefox 中显示 SVG?