我一直在使用 Raphael JS(…它很棒)。我只想知道如何在我用 Raphael 创建的 SVG 路径中添加 background-image
和 background-size
(cover)。这有可能吗?
我尝试过的:
我尝试添加了一个 class,但是它添加到了 SVG(路径的父元素),于是我也尝试了
svg path {// 包括我想要的背景属性,包括图像;}
我还研究了一下 patterns,这个方法行得通,但我的输出没有我想要的背景属性,因为我不知道如何使用 patterns 设置它们(
background-image: //;
和background-size: //;
)。
当前代码(Raphael):
function myFunction() {
var paper = Raphael("drama", 400,400);
var c = paper.path("M24,48 L313,12L342,174L98,280Z").attr({fill: "url('../imgs/screen.png')", stroke: "none"});
}
当前拉斐尔输出
<svg height="400" version="1.1" width="400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="overflow: hidden; position: relative;">
<desc style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);">Created with Raphaël 2.1.2</desc>
<defs style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);">
<pattern id="1546A235-C4BA-4ED7-A544-C43CE0BA3109" x="0" y="0" patternUnits="userSpaceOnUse" height="1737" width="2880" patternTransform="matrix(1,0,0,1,0,0) translate(24,12)" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);">
<image x="0" y="0" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="../imgs/screen.png" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);" width="2880" height="1737"></image>
</pattern>
</defs>
<path fill="url(#1546A235-C4BA-4ED7-A544-C43CE0BA3109)" stroke="none" d="M24,48L313,12L342,174L98,280Z" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
</svg>
background-size:cover;
是不够的。它会被裁剪并填满容器。 - web-tiki