简体   繁体   English

SVG模式,任何方式/黑客停止重复图像

[英]SVG Pattern, any way/hack to stop repeating image

I'm using an SVG <pattern> to make an <image> a fill on another user supplied SVG but I really hate how it automatically repeats (I get it) I need to know if there's a way or a hack to make it stop repeating the image as it's causing two issues. 我正在使用SVG <pattern>使<image>填充另一个用户提供的SVG,但我真的很讨厌它是如何自动重复的(我明白了)我需要知道是否有方法或者黑客来阻止它重复图像,因为它导致两个问题。

  1. When the browser thinks it has to start painting multiple of the image (tiling) it visibly janks (I think this is also in part to the poor performance of Interact.js) 当浏览器认为它必须开始绘制多个图像(平铺)时,它明显地晃动(我认为这也部分是因为Interact.js的性能不佳)

  2. Users don't want it to tile, just the empty space around it. 用户不希望它平铺,只是它周围的空白区域。

It's in an Electron app so browser support is minimal. 它位于Electron应用程序中,因此浏览器支持很少。

process.versions = {
  ares: "1.10.1-DEV"
  atom-shell: "0.34.1"
  chrome: "45.0.2454.85"
  electron: "0.34.1"
  http_parser: "2.5.0"
  modules: "46"
  node: "4.1.1"
  openssl: "1.0.2d"
  uv: "1.7.4"
  v8: "4.5.103.29"
  zlib: "1.2.8"
}

Here is the pattern as requested (with the transforms that are commonly applied), although but I'm not sure what use this is. 这是所要求的模式(通常应用的变换),但我不确定这是什么用途。

The <pattern> 's width and height are calculated by (target element's dimensions / image's size) + 1 . <pattern>widthheight(target element's dimensions / image's size) + 1计算得出。 data-x/y are used as part of a drag and drop interface, data-scale-x/y are used to scale the image without translating it. data-x/y用作拖放界面的一部分, data-scale-x/y用于缩放图像而不进行翻译。

<pattern id="user_image_container" patternUnits="objectBoundingBox" x="0" y="0" width="16.125264252110085" height="11.416432536797034" data-x="-3008" data-y="-1525" patternTransform="rotate(0 -416 203) translate(170.510014198065 170.5174437535593) scale(0.070795135733522) translate(-3008 -1525)" data-scale-x="170.510014198065" data-scale-y="170.5174437535593">
  <image xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/Users/username/Desktop/IMG_8829 2.JPG" id="user_image" width="5184" height="3456"></image>
</pattern>

The pattern is used as a fill attribute on a path in another, separate SVG. 该模式用作另一个单独SVG中路径的fill属性。

Here is a Codepen that shows the image tiling in the same way I'm using it (less transforms) 这是一个Codepen,它以与我使用它相同的方式显示图像平铺(较少的转换)

http://codepen.io/davemackintosh/pen/zvLvey http://codepen.io/davemackintosh/pen/zvLvey

There is no equivalent to CSS no-repeat for SVG patterns. 对于SVG模式,没有等效的CSS no-repeat The only way to prevent the image pattern from repeating is to make the pattern tile (defined by width , height , x and y on the <pattern> element) larger than the shape that it fills. 防止图像图案重复的唯一方法是使图案图块(由<pattern>元素上的widthheightxy定义)大于其填充的形状。

Since you're using fill (not stroke ) and the default objectBoundingBox value for patternUnits , this would normally be assured with a width/height of 1 (must be set explicitly) and x/y of 0 (the default). 由于您使用的fill (未stroke )和默认objectBoundingBox的价值patternUnits ,这通常是为1的宽/高(一定要明确设置)和0(默认值)的X / Y放心。 However, the transformations you're using would throw that off. 但是,您正在使用的转换会将其抛弃。

Without knowing how or why you are calculating the transformations the way you are, I can't tell you the reverse calculations to use to create a large enough pattern tile. 在不知道你是如何或为什么以你的方式计算变换的情况下,我无法告诉你用于创建足够大的图案拼贴的反向计算。

Instead, I would suggest that you leave the pattern transformation alone, and transform the <image> instead. 相反,我建议您单独保留模式转换,然后转换<image> The pattern transform attribute is useful because it transforms the tiling pattern as well as the contents, but since you do not want any tiling to be visible it is an unnecessary complication in this case. 模式转换属性很有用,因为它可以转换平铺模式以及内容,但由于您不希望任何平铺可见,因此在这种情况下这是不必要的复杂化。

I know it has been 2 years, but I was looking for the same solution. 我知道已经2年了,但我一直在寻找相同的解决方案。 I tried this post, and it works - Fill SVG path element with a background image without tiling or scaling 我尝试了这篇文章,它的工作原理 - 用背景图片填充SVG路径元素,不需要平铺或缩放

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

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