[英]Using a raster image as tiled background in a scaled svg
I am stuck trying to get the following to work: 我被困试图使以下工作:
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>
There is just one last thing I miss and cannot figure out how to do: 我错过了最后一件事,无法弄清楚该怎么做:
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.