简体   繁体   English

全尺寸背景图像到SVG路径

[英]Full size background-image to SVG path

I've been using Raphael JS (…it's brilliant). 我一直在使用Raphael JS(......很棒)。 I was just wanting to understand how I can add a background-image and background-size (cover) to my SVG path I created with Raphael. 我只是想了解如何在我用Raphael创建的SVG路径中添加background-imagebackground-size (封面) Is this at all possible? 这是可能吗?

What I have tried: 我尝试过的:

  1. I've tried adding a class, but it adds it to the SVG (parent of path), so then I also tried svg path {//background properties, including the image I want to include;} 我已经尝试添加一个类,但它将它添加到SVG(路径的父),所以我还尝试了svg path {//background properties, including the image I want to include;}

  2. I've also looked into patterns, this worked but my output didn't have the background properties I wanted, as I wasn't sure how to set them with patterns ( background-image: //; and background-size: //; ). 我也研究过模式,但是我的输出没有我想要的背景属性,因为我不知道如何用模式设置它们( background-image: //;background-size: //; )。

Current Code (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"});
}

Current Raphael Output 目前的拉斐尔输出

<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>

Adjusting the width and height of both your pattern and your image tag should do it. 调整patternimage标签的宽度和高度都应该这样做。

You can set a % based width and height, which are relative to the width and height of the SVG (not your path). 您可以设置基于%的宽度和高度,它们相对于SVG的宽度和高度(而不是您的路径)。

The demo below uses a square image of 500 * 500 pixels that is resized to 80% of the width and height of your SVG (= 320 * 320 pixels). 下面的演示使用500 * 500像素的方形图像,其大小调整为SVG宽度和高度的80%(= 320 * 320像素)。

The Fiddle : 小提琴:

http://jsfiddle.net/4fo0rnfd/2/ http://jsfiddle.net/4fo0rnfd/2/


The SVG code : SVG代码:

 <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="100%" width="100%" 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="//nl.gravatar.com/userimage/24668445/02be1fd7ba95a756c1f29e61738bd6a5.png?size=500" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);" width="80%" height="80%"></image> </pattern> </defs> <path stroke="none" d="M24,48L313,12L342,174L98,280Z" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);" fill="url(#1546A235-C4BA-4ED7-A544-C43CE0BA3109)"></path> </svg> 

A screenshot : 截图:

在此输入图像描述

Is it an option to add a container around the SVG? 是否可以在SVG周围添加容器?

If yes, you could use the aspect-ratio trick originally thought for videos: http://alistapart.com/article/creating-intrinsic-ratios-for-video 如果是,您可以使用最初为视频设想的宽高比技巧: http//alistapart.com/article/creating-intrinsic-ratios-for-video

Here is another article on the matter: http://www.mademyday.de/css-height-equals-width-with-pure-css.html 这是关于此事的另一篇文章: http//www.mademyday.de/css-height-equals-width-with-pure-css.html

It should be possible to have the SVG keeping the aspect-ratio and you could add a perfectly fitting background-image on the parent-container. 应该可以让SVG保持纵横比,并且可以在父容器上添加完美拟合的背景图像。

This is quite hacky but I'd give it a try. 这很hacky但是我试试看。 =) =)

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

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