简体   繁体   English

在缩放的svg中使用栅格图像作为平铺背景

[英]Using a raster image as tiled background in a scaled svg

I am stuck trying to get the following to work: 我被困试图使以下工作:

  • An embedded svg picture, in an html5 document. html5文档中的嵌入式svg图片。
  • It has a single polygon (a triangle), taking all the svg viewport: ◤ 它具有一个多边形(三角形),并带有所有svg视口:
  • This should extend to take full height of its container. 它应延伸至其容器的整个高度。

So far so good, this example does the job: 到目前为止,此示例已完成工作:

 #container { position: relative; width: 5em; height: 5em; resize: both; overflow: hidden; // so we can play with it in demo } #triangle { position: absolute; top: 0; left: 0; width: 100%; height: 100%; fill: #00f; pointer-events: none; // so we can grab the resize handle } 
 <div id="container"> <svg id="triangle" viewBox="0 0 10 10" preserveAspectRatio="none"> <polygon points="0,0 0,10 10,0" /> </svg> </div> 
Try grabbing the resize handle to see how the triangle reacts to changes in container size. 尝试抓住调整大小手柄,以查看三角形如何对容器大小的变化做出反应。

There is just one last thing I miss and cannot figure out how to do: 我错过了最后一件事,无法弄清楚该怎么做:

  • The triangle must be textured using a raster image (png file) 必须使用光栅图像对三角形进行纹理处理(png文件)
  • That texture must not be scaled. 该纹理不能缩放。

So far my best attempt was using the initial coordinate system (no viewPort directive), to apply a filter to a square, like this: 到目前为止,我最好的尝试是使用初始坐标系(无viewPort指令),将滤镜应用于正方形,如下所示:

 #container { position: relative; width: 5em; height: 5em; resize: both; overflow: hidden; // so we can play with it in demo } #triangle { position: absolute; top: 0; left: 0; width: 100%; height: 100%; pointer-events: none; // so we can grab the resize handle } 
 <div id="container"> <svg id="triangle" preserveAspectRatio="none"> <defs> <filter id="Tiled"> <feImage xlink:href="https://www.w3.org/TR/SVG/images/filters/smiley.png" x="0" y="0" width="100" height="100" result="texture"/> <feTile in="texture" x="0" y="0" width="100%" height="100%"/> </filter> </defs> <rect filter="url(#Tiled)" x="0" y="0" width="100%" height="100%"/> </svg> </div> 

Again, try resizing the image using the (invisible) handle at the bottom-right corner: the background texture gets tiled. 再次尝试使用右下角的(不可见)手柄调整图像大小:背景纹理被平铺。

Thus the question: 因此,问题是:

The second example works thanks to the width="100%" . 第二个示例的工作归功于width="100%" However, polygons do not accept percentage coordinates. 但是,多边形不接受百分比坐标。

→ How to achieve the tiling behavior of the second example, but on a dynamically sized polygon like first example? →如何实现第二个示例的平铺行为,但如何像第一个示例一样在动态尺寸的多边形上?

You just need a polygon to clip the rect to get the effect you want don't you? 您只需要一个多边形来裁剪矩形即可获得想要的效果,不是吗?

 #container { position: relative; width: 5em; height: 5em; resize: both; overflow: hidden; // so we can play with it in demo } #triangle { position: absolute; top: 0; left: 0; width: 100%; height: 100%; pointer-events: none; // so we can grab the resize handle } 
 <div id="container"> <svg id="triangle" preserveAspectRatio="none"> <defs> <filter id="Tiled"> <feImage xlink:href="https://www.w3.org/TR/SVG/images/filters/smiley.png" x="0" y="0" width="100" height="100" result="texture"/> <feTile in="texture" x="0" y="0" width="100%" height="100%"/> </filter> <clipPath id="clip" clipPathUnits="objectBoundingBox"> <polygon points="0,0 0,1 1,0" /> </clipPath> </defs> <rect clip-path="url(#clip)" filter="url(#Tiled)" x="0" y="0" width="100%" height="100%"/> </svg> </div> 

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

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