原生的渐变方法

在SVG中提供的原生渐变方法有两种,分别为线性渐变linearGradient和径向渐变radialGradient。我们以一个稍微复杂的路径来作为模板,为其添加两种渐变效果:

<svg width="800" height="300">
<defs>
<linearGradient id="linear-grad">
<stop offset="0" stop-color="#f7ff00" />
<stop offset="1" stop-color="#db36a4" />
</linearGradient>
<radialGradient id="radial-grad">
<stop offset="0" stop-color="#f7ff00" />
<stop offset="1" stop-color="#db36a4" />
</radialGradient> <path id="grad-path" d="M 50,50 H 200 V 150 L 50,100 V 200 H 200"/>
</defs> <use xlink:href="#grad-path" stroke="black" fill="none" stroke-width="3" />
<use xlink:href="#grad-path" stroke="url(#linear-grad)" fill="none" stroke-width="9" x="250" />
<use xlink:href="#grad-path" stroke="url(#radial-grad)" fill="none" stroke-width="9" x="500" />
</svg>

展示效果如下:

虽然这两种渐变类型还有许多其他的属性可以设置,但它们的渐变颜色始终是只能沿着某一方向直线分布的,无法满足我们希望颜色可以沿着任意路径作渐变的需求。

对于某些特殊的路径如<circle> 可以通过一些比较hack的方式来仅依靠CSS就能实现沿路径颜色渐变:例如这个示例。但我们需要的是足够通用的办法,可以做到任意路径上的颜色渐变;这就需要借助JavaScript来实现。

模拟渐变

既然SVG没有原生提供我们想要的渐变效果,就需要想想其他办法来“曲线救国”。

我们首先考虑下在Canvas画布中要实现沿路径渐变颜色是如何实现的:Canvas的2D上下文同样是只提供了线性渐变createLinearGradient()和径向渐变createRadialGradient()两种方法。但不同之处在于,Canvas画布是可以逐像素绘制颜色的。在绘制路径时根据渐变颜色做插值计算,得出每个像素应用的颜色值即可。

将这种思考迁移到SVG中来。尽管SVG是矢量图绘制,不支持像素级操作的;但通过方法getTotalLength()getPointAtLength()可以获得路径的总长度及组成路径的这些离散点在SVG视图内的坐标。再利用<circle>图形来模拟“像素点”,依次摆放到前述的点坐标上,再设置对应的渐变颜色:

这些“像素点”越密集呈现的效果就越好。出于对页面性能的考虑,事先选定一个合适的密度很重要。

<svg width="800" height="300">
<defs>
<path id="grad-path" d="M 50,50 H 200 V 150 L 50,100 V 200 H 200" />
</defs> <g id="dots-container">
<!-- 放置circle的容器 -->
</g>
</svg> <script src="https://cdn.jsdelivr.net/npm/d3@7"></script>
<script src="./createDots.js"></script>

这里引入d3.js库只是为了方便做颜色的插值计算。

const strokeWidth = 9
const startColor = '#f7ff00', endColor = '#db36a4'
const gradientPath = document.querySelector('#grad-path')
const dotsContainer = document.querySelector('#dots-container')
// 选择合适的点间距来控制密度
//const dotsDensity = strokeWidth * 1.0
const dotsDensity = strokeWidth * 0.2
const numberOfDots = Math.ceil(gradientPath.getTotalLength() / dotsDensity) createDots() function createDots() {
for(let i = 0; i < numberOfDots; ++i) {
const circle = document.createElementNS('http://www.w3.org/2000/svg', 'circle')
dotsContainer.appendChild(circle) const steps = i / numberOfDots
// 计算坐标点
const pos = gradientPath.getPointAtLength(steps * gradientPath.getTotalLength())
circle.setAttribute('cx', pos.x)
circle.setAttribute('cy', pos.y)
circle.setAttribute('r', strokeWidth / 2)
// 计算颜色插值
const interpolator = d3.interpolate(startColor, endColor)
const curColor = interpolator(steps)
circle.setAttribute('fill', curColor)
}
}

动画

接下来我们再为其加入动画,使渐变颜色沿着路径移动来产生动效。

<svg width="800" height="300">
<defs>
<path id="grad-path" d="M 50,50 H 200 V 150 L 50,100 V 200 H 200" />
</defs> <use xlink:href="#grad-path" stroke="#868e96" fill="none" stroke-width="1" />
<g id="dots-container">
<!-- 放置circle的容器 -->
</g>
</svg> <script src="https://cdn.jsdelivr.net/npm/d3@7"></script>
<script src="./test.js"></script>

除了渐变色外,定义动效的宽度、高度及时长:

const startColor = '#f7ff00', endColor = '#db36a4'
const gradientPath = document.querySelector('#grad-path')
const dotsContainer = document.querySelector('#dots-container')
// 动效的宽高
const strokeWidth = 9
const effectLength = 100
// 选择合适的点间距来控制密度
const dotsDensity = strokeWidth * 0.4
const numberOfDots = Math.ceil(effectLength / dotsDensity)
// 动画时长
const duration = 1500

根据配置创建所有的像素点,初始化颜色与半径等属性后将其引用存储起来:

// 初始化所有点
const dots = []
createDots() function createDots() {
for(let i = 0; i < numberOfDots; ++i) {
const circle = document.createElementNS('http://www.w3.org/2000/svg', 'circle')
dotsContainer.appendChild(circle) const color = d3.interpolate(startColor, endColor)(i / numberOfDots)
circle.setAttribute('fill', color)
circle.setAttribute('display', 'none')
circle.setAttribute('r', strokeWidth / 2) dots.push(circle)
}
}

执行动画。由window.requestAnimationFrame决定动画的帧数。传入每帧的回调函数,从动效的尾部开始处理,由dotsDensity做像素点的间隔向前依次更新位置,对于不在路径范围内的点做隐藏处理:

window.requestAnimationFrame(animationTask)
let startTime = -1
function animationTask(timestamp) {
if(startTime === -1) {
startTime = timestamp
} const process = (timestamp - startTime) / duration
// 动效的终点坐标
const tailIdx = process * gradientPath.getTotalLength()
for(let i = numberOfDots - 1; i >= 0; --i) {
const posIdx = tailIdx - (numberOfDots - i) * dotsDensity
const hide = posIdx < 0 || posIdx > gradientPath.getTotalLength() const pos = gradientPath.getPointAtLength(posIdx) updateDot(i, pos, hide)
} window.requestAnimationFrame(animationTask)
} function updateDot(idx, pos, isHide) {
const circle = dots[idx] if(isHide) {
circle.setAttribute('display', 'none') } else {
circle.setAttribute('display', 'inline')
circle.setAttribute('cx', pos.x)
circle.setAttribute('cy', pos.y)
}
}

最终效果如下~

沿SVG路径的颜色渐变的更多相关文章

  1. SVG.js 颜色渐变使用

    一.SVG.Gradient 1.线性渐变.径向渐变,设置渐变的起始点,设置径向渐变的外层半径 var draw = SVG('svg1').size(300, 300); //SVG.Gradien ...

  2. 深度掌握SVG路径path的贝塞尔曲线指令

    一.数字.公式.函数.变量,哦,NO! 又又一次说起贝塞尔曲线(英语:Bézier curve,维基百科详尽中文释义戳这里),我最近在尝试实现复杂的矢量图形动画,发现对贝塞尔曲线的理解馒头那么厚,是完 ...

  3. iOS 动画绘制线条颜色渐变的折线图

    效果图 .................... 概述 现状 折线图的应用比较广泛,为了增强用户体验,很多应用中都嵌入了折线图.折线图可以更加直观的表示数据的变化.网络上有很多绘制折线图的demo,有 ...

  4. CAGradientLayer颜色渐变器

    使用CAGradientLayer可以实现颜色的渐变, 我们先看下头文件 @interface CAGradientLayer : CALayer @property(nullable, copy) ...

  5. SVG路径

    前面的话 本文将详细介绍SVG路径 path字符串 路径(path)是一个非常强大的绘图工具,可以用path元素绘制矩形(直角矩形或者圆角矩形).圆形.椭圆.折线形.多边形,以及一些其他的形状,例如贝 ...

  6. svg路径蒙版动画

    svg路径蒙版动画,是比较实用的一种动画效果,能够绘制如下图所示的动画. 接下来细说这样的动画是如何做成的: 1.准备工作 2.SVG路径动画 3.SVG路径蒙版动画 4.复杂图形的编辑技巧 1.准备 ...

  7. DrawSVG - SVG 路径动画 jQuery 插件

    jQuery DrawSVG 使用了 jQuery 内置的动画引擎实现 SVG 路径动画,用到了 stroke-dasharray 和 stroke-dashoffset 属性.DrawSVG 是完全 ...

  8. CSS3颜色渐变模式

       1.线性渐变:linear-gradient 语法:<linear-gradient> = linear-gradient([ [ <angle> | to <si ...

  9. 【iOS实现一个颜色渐变的弧形进度条】

    在Github上看到一些进度条的功能,都是通过Core Graph来实现.无所谓正确与否,但是开发效率明显就差很多了,而且运行效率还是值得考究的.其实使用苹果提供的Core Animation能够非常 ...

  10. IOS导航栏颜色渐变与常用属性

    (转:http://www.cnblogs.com/Lingchen-start/archive/2015/10/23/4904361.html) 今年很忙,忙的写日志的时间都很少.  少的可怜. 自 ...

随机推荐

  1. python之十进制、二进制、八进制、十六进制转换

    数字处理的时候偶尔会遇到一些进制的转换,以下提供一些进制转换的方法 一.十进制转化成二进制 使用bin()函数 1 x=10 2 print(bin(x)) 二.十进制转化为八进制 使用oct()函数 ...

  2. Tomcat 与 JVM 中classpath的理解和设置总结

    本文为博主原创,转载请注明出处: 1.介绍 classpath是java运行时环境搜索类和其他资源文件(比如jar\zip等资源)的路径.类路径告诉JDK工具和应用程序在哪里可以找到第三方和用户定义的 ...

  3. Nacos源码 (4) 配置中心

    本文阅读nacos-2.0.2的config源码,编写示例,分析推送配置.监听配置的原理. 客户端 创建NacosConfigService对象 Properties properties = new ...

  4. org.yaml.snakeyaml.error.YAMLException: java.nio.charset.MalformedInputException: Input length = 2

    1.报错 在运行SpringBoot项目时遇到报错: 17:44:47.558 [main] ERROR org.springframework.boot.SpringApplication -- A ...

  5. [转帖]深入理解mysql-第六章 mysql存储引擎InnoDB的索引-B+树索引

    一.引入索引 在没有索引的情况下,不论是根据主键列或者其他列的值进行查找,由于我们并不能快速的定位到记录所在的页,所以只能从第一个页沿着双向链表一直往下找,因为要遍历所有的数据页,时间复杂度就是O(n ...

  6. [转帖]使用Rclone实现minio数据的迁移

    使用Rclone实现minio数据的迁移 一.准备 1.1 使用工具 rclone:开源的对象存储在线迁移工具,用于文件和目录的同步,支持阿里云的oss.minio .亚马逊S3 等. 1.2 注意事 ...

  7. 【转帖】eBay 流量管理之 Kubernetes 网络硬核排查案例

    https://www.infoq.cn/article/L4vyfdyvHYM5EV8d3CdD 一.引子 在 eBay 新一代基于 Kubernetes 的云平台 Tess 环境中,流量管理的实现 ...

  8. [转帖]Linux 监测服务心跳、服务重启策略

    文章目录 前言 背景 一.curl服务可用验证 二.服务探测脚本 三.配置系统定时任务 四.Linux特殊字符转义 总结 前言 请各大网友尊重本人原创知识分享,谨记本人博客:南国以南i. 提示:以下是 ...

  9. 银河麒麟(Ubuntu)无法上网问题的解决方法

    最近部门借了几台银河麒麟的服务器. 因为有特殊用途, 不允许连接互联网,所以没办法只能搭建一个小的局域网进行处理. 但是发现在搭建过程中遇到了一些坑, 之前协助同事解决odoo问题时也遇到过, 当时本 ...

  10. JS中typeof和instanceof的区别

    01==> 浅谈JS中的typeof和instanceof的区别 // JS中的typeof和instanceof常用来变量是什么类型. // typeof一般返回以下几个字符串: // Str ...