RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1333538
Accepted
Denis
Denis
Asked:2022-09-30 01:59:29 +0000 UTC2022-09-30 01:59:29 +0000 UTC 2022-09-30 01:59:29 +0000 UTC

如何将图像插入 SVG?

  • 772

这是布局:

在此处输入图像描述

这是不起作用的svg代码。

<svg width="727" height="620" viewBox="0 0 727 620" fill="none" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<path d="M533.771 0H0V620H727V247.5L533.771 0Z" fill="url(#pattern0)"/>
<path d="M533.771 0H0V620H727V247.5L533.771 0Z" fill="url(#paint0_linear)"/>
<defs>
<pattern id="pattern0" patternContentUnits="objectBoundingBox" width="1" height="1">
<use xlink:href="#image0" transform="translate(0 -0.134532) scale(0.000927721 0.00108783)"/>
</pattern>
<linearGradient id="paint0_linear" x1="711" y1="620" x2="363.5" y2="620" gradientUnits="userSpaceOnUse">
<stop stop-color="#0B1424"/>
<stop offset="1" stop-opacity="0"/>
</linearGradient>
<image id="image0" width="1080" height="1080" src="https://cdn2.unrealengine.com/kena-keyart-1200x1600-1200x1600-3cc88c815329.png?h=854&resize=1&w=640" />
</defs>
</svg>

也就是说,您需要能够插入任何图像。并且右上角应该被剪掉。请告诉我我的错误在哪里。

html
  • 5 5 个回答
  • 10 Views

5 个回答

  • Voted
  1. Alexandr_TT
    2022-09-30T04:14:11Z2022-09-30T04:14:11Z

    请告诉我我的错误在哪里。

    使用标签添加图像时,您需要<image>使用src=href=

    <svg width="727" height="620" viewBox="0 0 727 620" fill="none" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <path d="M533.771 0H0V620H727V247.5L533.771 0Z" fill="url(#pattern0)"/>
    <path d="M533.771 0H0V620H727V247.5L533.771 0Z" fill="url(#paint0_linear)"/>
    <defs>
    <pattern id="pattern0" patternContentUnits="objectBoundingBox" width="1" height="1">
    <use xlink:href="#image0" transform="translate(0 -0.134532) scale(0.000927721 0.00108783)"/>
    </pattern>
    <linearGradient id="paint0_linear" x1="711" y1="620" x2="363.5" y2="620" gradientUnits="userSpaceOnUse">
    <stop stop-color="#0B1424"/>
    <stop offset="1" stop-opacity="0"/>
    </linearGradient>
    <image id="image0" width="1080" height="1080" href="https://cdn2.unrealengine.com/kena-keyart-1200x1600-1200x1600-3cc88c815329.png?h=854&resize=1&w=640" />
    </defs>
    </svg>

    • clipPath
      使用响应性添加图像width="75%" height="75%"

    <svg width="75%" height="75%" viewBox="0 0 727 620" fill="none" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <defs>
    <clipPath id="cp">
    <path transform="translate(-100)" d="M533.771 0H0V620H727V247.5L533.771 0Z" />
    </clipPath>
    </defs>
    <image clip-path="url(#cp)"  id="image0" width="100%" height="100%" href="https://isstatic.askoverflow.dev/6lNYF.png" /> 
    </svg>

    • 添加带蒙版的图像

    <svg  width="75%" viewBox="0 0 727 620" fill="none" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <defs>
    <mask id="cp">
    <path fill="white" transform="translate(-100)" d="M533.771 0H0V620H727V247.5L533.771 0Z" />
    </mask>
    </defs>
    <image mask="url(#cp)"  id="image0" width="100%" height="100%" href="https://isstatic.askoverflow.dev/6lNYF.png" /> 
    </svg> 

    • 4
  2. Pavlo Stepura
    2022-09-30T02:18:03Z2022-09-30T02:18:03Z

    图像的链接包含 svg 标签中不允许的字符 - &、<、>

    <image id="image0" width="1080" height="1080" src="https://cdn2.unrealengine.com/kena-keyart-1200x1600-1200x1600-3cc88c815329.png?h=854&resize=1&w=640" />
    

    如果将链接中的 & 替换为 %26 HTML URL Encoding Reference则一切正常

    https://cdn2.unrealengine.com/kena-keyart-1200x1600-1200x1600-3cc88c815329.png?h=854%26resize=1%26w=640
    

    最终的 svg 如下所示:

    <svg width="727" height="620" viewBox="0 0 727 620" fill="none" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
        <path d="M533.771 0H0V620H727V247.5L533.771 0Z" fill="url(#pattern0)"/>
        <path d="M533.771 0H0V620H727V247.5L533.771 0Z" fill="url(#paint0_linear)"/>
        <defs>
            <pattern id="pattern0" patternContentUnits="objectBoundingBox" width="1" height="1">
                <use href="#image0" transform="translate(0 -0.134532) scale(0.000927721 0.00108783)"/>
            </pattern>
            <linearGradient id="paint0_linear" x1="711" y1="620" x2="363.5" y2="620" gradientUnits="userSpaceOnUse">
                <stop stop-color="#0B1424"/>
                <stop offset="1" stop-opacity="0"/>
            </linearGradient>
            <image id="image0" width="1080" height="1080" href="https://cdn2.unrealengine.com/kena-keyart-1200x1600-1200x1600-3cc88c815329.png?h=854%26resize=1%26w=640" />
        </defs>
    </svg>

    *代码编辑器,如 IntelliJ 或伟大的免费编辑器 VSCode 将立即显示错误所在

    代码编辑器

    • 3
  3. Best Answer
    zhurof
    2022-09-30T17:26:53Z2022-09-30T17:26:53Z

    对于此类修剪,您可以将其clip-path与calc().

    裁剪右上角(60px 腿)的示例:

    body{
      margin: 0;
      background: #cda;
    }
    img{
      clip-path: polygon(0 0, calc(100% - 60px) 0, 100% 60px, 100% 100%, 0 100%);
    }
    <img src="https://www.placecage.com/c/300/200" alt=""/>

    • 3
  4. Leonid
    2022-09-30T02:13:19Z2022-09-30T02:13:19Z

    您可以使用画布:

    const canvas = document.querySelector('canvas');
    const ctx = canvas.getContext('2d');
    
    const image = new Image();
    image.src = "https://cdn2.unrealengine.com/kena-keyart-1200x1600-1200x1600-3cc88c815329.png?h=854&resize=1&w=640";
    image.onload = () => {
        const w = canvas.width = image.width;
        const h = canvas.height = image.height;
        const path = new Path2D(`M ${w*2/3} 0 L ${w} ${w/3} V ${h} H 0 V 0 z`);
        ctx.clip(path);
        ctx.drawImage(image,0,0);
    }
    <canvas></canvas>

    • 2
  5. Alexandr_TT
    2022-09-30T05:03:32Z2022-09-30T05:03:32Z

    为了使应用程序看起来一对一,就像在给定的布局上一样,具有自适应性,并且布局不会以不同的分辨率为不同的小工具移动,并且您不必手动调整大小scale() translate()并用渐变填充空隙,我建议使用矢量编辑器的简单技术,只需点击几下鼠标

    1. 将模板加载到矢量编辑器中,并使用“绘制贝塞尔曲线”工具shift+F6沿图像轮廓绘制锚点:

    在此处输入图像描述

    1. 将文件保存为 *.svg 格式
    2. 使用SVGOMG对其进行优化
    3. 接下来,使用生成的路径使用clipPath、蒙版裁剪图像

    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" fill="none" version="1.1" id="svg4" width="75%" height="75%" viewBox="0 0 727 620">
    <defs>
    <clipPath id="cp">
    <path  d="m0 0 535 0 191 246 0 372H0Z" />
    </clipPath>
    </defs> 
     <image clip-path="url(#cp)" xlink:href="https://isstatic.askoverflow.dev/ZKnFL.png" id="image2" height="100%" width="100%"/>
    </svg>

    • 2

相关问题

  • 具有非均匀背景的块内的渐变边框

  • 离开页脚

  • 如何将三个字段的数据收集到一封电子邮件中?

  • Html 元素刚从父元素中出来

  • 如何在css中制作这个背景?

  • 如何制作带有斜条纹的背景?

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    表格填充不起作用

    • 2 个回答
  • Marko Smith

    提示 50/50,有两个,其中一个是正确的

    • 1 个回答
  • Marko Smith

    在 PyQt5 中停止进程

    • 1 个回答
  • Marko Smith

    我的脚本不工作

    • 1 个回答
  • Marko Smith

    在文本文件中写入和读取列表

    • 2 个回答
  • Marko Smith

    如何像屏幕截图中那样并排排列这些块?

    • 1 个回答
  • Marko Smith

    确定文本文件中每一行的字符数

    • 2 个回答
  • Marko Smith

    将接口对象传递给 JAVA 构造函数

    • 1 个回答
  • Marko Smith

    正确更新数据库中的数据

    • 1 个回答
  • Marko Smith

    Python解析不是css

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5