RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 803801
Accepted
Arthur
Arthur
Asked:2020-03-24 04:47:12 +0000 UTC2020-03-24 04:47:12 +0000 UTC 2020-03-24 04:47:12 +0000 UTC

样式化和动画 SVG 文本

  • 772

我想看一些文本样式的示例以及如何将pattern&mask与一些效果结合使用。

1. “爬虫”笔画(linearGradient& animate):

在此处输入图像描述

2.
用对角线(pattern& mask)从自身投射阴影的文本:

在此处输入图像描述

3.用水填充文本(SVG( pattern& mask) & CSS( @keyframes)):

在此处输入图像描述

PS在这个问题中,有一些来自开放访问(2和3)的例子。创建这个问题是为了吸引SVG和展示它的能力。

html
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Arthur
    2020-03-24T04:47:12Z2020-03-24T04:47:12Z

    我们将逐渐考虑并创建类似的示例。

    第一个例子

    1. 我们将文本放入<symbol></symbol>.

    元素<symbol>用于定义该元素可以创建的图形模板对象。<use>

    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="400" height="400">
      <symbol id="text">
        <text font-size="5em" text-anchor="middle" x="160" y="55">Some text</text>
      </symbol>
      <use xlink:href="#text"/>
    </svg>

    1. 我们用做一个任意的渐变<linearGradient></linearGradient>,我想这里一切都清楚了,它可以连接到画布的各种元素。在这个例子中,我们用渐变“填充”了文本的背景,但这并不是它所能做的全部。我们可以应用渐变,stroke而不仅仅是。

    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="400" height="400">
      <symbol id="text">
        <text font-size="5em" text-anchor="middle" x="160" y="55">Some text</text>
      </symbol>
      <linearGradient id="grad">
        <stop offset="0%" stop-color="red"/>
        <stop offset="50%" stop-color="black"/>
        <stop offset="100%" stop-color="blue"/>
      </linearGradient>
      <use xlink:href="#text" fill="url(#grad)"/>
    </svg>

    1. “让我们画出”文本的轮廓。让我们也尝试使用 更改stroke-dashoffset属性<animate/>。

    该属性定义相对于起始位置stroke-dashoffset的偏移量stroke

    一个属性stroke-dasharray有 2 个或更多值。第一个值定义行的长度,第二个定义行之间的间距。

    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="400" height="100">
      <symbol id="text">
        <text text-anchor="middle" x="160" y="55">Some text</text>
      </symbol>
      <linearGradient id="grad">
        <stop offset="0%" stop-color="red"/>
        <stop offset="60%" stop-color="black"/>
        <stop offset="100%" stop-color="blue"/>
      </linearGradient>
      <use xlink:href="#text" stroke-dashoffset="0" stroke-dasharray="10 15" fill="none" stroke-width="3" stroke="url(#grad)" font-size="5em">
        <animate attributeName="stroke-dashoffset" values="250;0" dur="7s" repeatCount="indefinite"/>
      </use>
    </svg>

    1. 您可以进行最“毛毛虫”的效果。我们必须在文本上覆盖文本并更改stroke-dashoffset&属性stroke-dasharray。

      第一个示例已准备就绪:

    @import url(https://fonts.googleapis.com/css?family=Open+Sans:800);
    * {
      margin: 0;
      padding: 0;
    }
    
    body {
      background-color: #000000;
      display: flex;
      justify-content: center;
      align-items: center;
      font-family: Open Sans;
      text-transform: uppercase;
      letter-spacing: 5px;
    }
    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/2000/svg" version="1.1">
      <symbol id="stroke-dash">
        <text text-anchor="middle" x="50%" y="50%">Stroke</text>
      </symbol>
      <linearGradient id="linear" x1="0%" y1="0%" x2="100%" y2="0%">
        <stop offset="0%" stop-color="#05a"/>
        <stop offset="100%" stop-color="#0a5"/>
      </linearGradient>
      <use xlink:href="#stroke-dash" stroke="url(#linear)" stroke-dasharray="10 1" stroke-width="3" stroke-dashoffset="0" font-size="4em">
        <animate attributeName="stroke-dashoffset" values="250;0" dur="15s" repeatCount="indefinite"/>
      </use>
      <use xlink:href="#stroke-dash" stroke="url(#linear)" stroke-dasharray="10 15" stroke-width="3" stroke-dashoffset="0" font-size="4em">
        <animate attributeName="stroke-dashoffset" values="250;0" dur="15s" repeatCount="indefinite"/>
      </use>
    </svg>

    第二个例子

    PS在这个例子中,我们来看看pattern& mask。

    1. 让我们尝试pattern在任何正方形上使用它,用圆圈填充它:

    pattern可以用作各种元素的填充或描边,无论是文本、形状还是图像。

    <link href='https://fonts.googleapis.com/css?family=Cabin+Condensed:700' rel='stylesheet' type='text/css'>
    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">
      <defs>
        <pattern id="pattern" width="10" height="10" patternUnits="userSpaceOnUse">
          <circle id="circle" cx="5" cy="5" r="5" fill="#000"/>
        </pattern>
      </defs>
      <rect x="0" y="0" width="250" height="250" fill="url(#pattern)"/>
    </svg>

    1. 现在让我们试试pattern& mask。

    mask在我们的上下文中是隐藏一些内容的表面层。

    .stripes {
      width: 100px;
      height: 50px;
      mask: url("#mask");
    }
    
    .red {
      fill: red;
    }
    
    .blue {
      fill: blue;
    }
    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/2000/svg" version="1.1" height="150" width="150">
      <defs>
        <pattern id="strokeEffect" width="1" height="3" patternUnits="userSpaceOnUse" patternTransform="rotate(45)">
          <rect x="0" y="0" width="1" height="1" fill="#fff"/>
        </pattern>
        <mask id="mask">
          <rect height="100%" width="100%" style="fill: url(#strokeEffect)"/>
        </mask>
      </defs>
      <rect class="stripes red" y="0"/>
      <rect class="stripes" y="50"/>
      <rect class="stripes blue" y="100"/>
    </svg>

    1. 让我们使用上面的方法重新创建第二个示例。

      第二个例子准备好了:

    body {
      background-color: #2e2e2e;
    }
    <link href='https://fonts.googleapis.com/css?family=Cabin+Condensed:700' rel='stylesheet' type='text/css'>
    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="620" height="500">
      <defs>
        <pattern id="strokeEffect" width="1" height="3" patternUnits="userSpaceOnUse" patternTransform="rotate(45)">
          <rect x="0" y="0" width="1" height=".8" fill="#ffffff"/>
        </pattern>
        <text id="text" x="250" y="100" font-size="5em" text-anchor="middle" letter-spacing="15" font-family="'Cabin Condensed'">Some text</text>
        <mask id="mask">
          <rect x="0" y="0" width="100%" height="100%" fill="#fff"/>
          <use x="-6" y="-6" stroke="#000" stroke-width="5" xlink:href="#text" opacity="1" fill="#000"/>
        </mask>
      </defs>
      <use x="6" y="6" xlink:href="#text" opacity="1" fill="url(#strokeEffect)" mask="url(#mask)"/> 
      <use x="0" y="0" xlink:href="#text" fill="#fff"/>
    </svg>

    第三个例子

    1. 我们先来看代码:

    body {
      background: #141414;
      text-align: center;
    }
    
    .water-fill {
      -webkit-animation: wave 0.7s infinite linear, fill-up 10s infinite ease-out alternate;
      animation: wave 0.7s infinite linear, fill-up 10s infinite ease-out alternate;
    }
    
    @-webkit-keyframes wave {
      0% {
        x: -400px;
      }
      100% {
        x: 0;
      }
    }
    
    @keyframes wave {
      0% {
        x: -400px;
      }
      100% {
        x: 0;
      }
    }
    
    @-webkit-keyframes fill-up {
      0% {
        height: 0;
        y: 130px;
      }
      100% {
        height: 160px;
        y: -30px;
      }
    }
    
    @keyframes fill-up {
      0% {
        height: 0;
        y: 130px;
      }
      100% {
        height: 160px;
        y: -30px;
      }
    }
    <link href='https://fonts.googleapis.com/css?family=Cabin+Condensed:700' rel='stylesheet' type='text/css'>
    <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="575px" height="120px" viewBox="0 0 575 120" xml:space="preserve">
      <defs>
        <pattern id="water" width=".25" height="1.1" patternContentUnits="objectBoundingBox">
          <path fill="white" d="M0.25,1H0c0,0,0-0.659,0-0.916c0.083-0.303,0.158,0.334,0.25,0C0.25,0.327,0.25,1,0.25,1z"/>
        </pattern>
        <text id="text" transform="matrix(1 0 0 1 -8 117)" font-family="'Cabin Condensed'" font-size="161">LOADING</text>
        <mask id="text_mask">
          <use xlink:href="#text" fill="white"/>
        </mask>
      </defs>
    <rect class="water-fill" mask="url(#text_mask)" fill="url(#water)" width="1600" height="120"/>
    </svg>

    1. 我们得出结论:)

      2.1。pattern沿轴有偏移X。


      2.2. 波浪效应的发生是由于宽度rect,它等于从到1600沿轴的负偏移。我们可以做实验,也就是改变数值,看看会发生什么:X-4000

      PS结果并不令人印象深刻,我想你知道为什么会这样了。如果不是,那么这是简短的答案:我们制作rect,而它pattern“调整”到那个宽度,然后我们rect偏移X.


      2.3. 接下来,我们将它粘贴进去mask,瞧,文本的波浪效果就准备好了:)

    .water-fill {
      animation: wave 0.7s infinite linear, fill-up 10s ease-out alternate;
    }
    
    @keyframes wave {
      0% {
          /*New*/
        x: -200px;
      }
      100% {
        x: 0;
      }
    }
    
    @keyframes fill-up {
      0% {
        y: 120px;
        height: 0;
      }
      100% {
        y: 0;
        height: 120px;
      }
    }
    <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
      <defs>
        <pattern id="water" width="0.25" height="1" patternContentUnits="objectBoundingBox">
          <path fill="dodgerblue" d="M0.25,1H0c0,0,0-0.659,0-0.916c0.083-0.303,0.158,0.334,0.25,0C0.25,0.327,0.25,1,0.25,1z"/>
        </pattern>
      </defs>	
      <!--New (width)-->
      <rect class="water-fill" fill="url(#water)" width="400" height="120"/>
    </svg>

    • 11
  2. Best Answer
    Alexandr_TT
    2020-03-24T15:37:07Z2020-03-24T15:37:07Z

    向位图中添加和设置 svg 文本样式

    您需要以对话、漫画的风格将文本添加到现有位图。
    同时,图像和文本需要自适应,即在调整大小时保持它们的相对位置。


    在此处输入图像描述

    分步说明:

    • 将位图添加到SVG

    <image xlink:href="https://isstatic.askoverflow.dev/6HVU6.jpg" width="400" height="300"/>

    • 我们在矢量编辑器中绘制文本所在的轮廓

      <path d="M350 280a400 400 0 0 0 4 -150a100 60 0 1 0 -14 4a400 400 0 0 1 10 146z"/>

    • 在大纲内添加文本。不幸的是,svg没有自动文本换行,如HTML,但有可能使用该属性进行手动文本换行<tspan>

    • 如有必要,我们会对容器的大小、位置和文本进行更精细的调整

      <g transform="scale(1.1) translate(-40 0)" >

    svg {
      width: 40%;
      height: 40%;
    }
    
    text {
      font-family: Lobster;
      font-size: 24px;
      text-anchor: middle;
      fill: #474447;
    }
    
    path {
      fill: #fff;
      stroke: #474447;
      stroke-width: 2;
    }
    <link href='https://fonts.googleapis.com/css?family=Lobster|Raleway' rel='stylesheet' type='text/css'>
    <link href="google-font.css" rel="stylesheet" type="text/css">
    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 400 300" preserveAspectRatio="xMinYMin meet">
      <image xlink:href="https://isstatic.askoverflow.dev/6HVU6.jpg" width="400" height="300"/>
      <g transform="scale(1.1) translate(-40 0)">
        <path stroke-miterlimit="50" d="M350 280a400 400 0 0 0 4 -150a100 60 0 1 0 -14 4a400 400 0 0 1 10 146z"/>
        <text>
          <tspan x="302" y="60">Вы вызываете</tspan>
          <tspan x="302" y="85">больше проблем,</tspan>
          <tspan x="302" y="110">чем пользы.</tspan>
        </text> 
      </g>
    </svg>

    图像和文本是响应式的。
    适用于所有现代浏览器 + IE11、Edge

    • 10

相关问题

Sidebar

Stats

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

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +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