当前位置:网站首页>Canvas 填充渐变
Canvas 填充渐变
2022-07-25 20:44:00 【最凶残的小海豹】
Canvas的渐变可以填充在矩形, 圆形, 线条, 文本等等, 各种形状可以自己定义不同的颜色。
注意:使用渐变必须使用两种或两种以上的颜色。
Canvas渐变有两种方法:
线性 渐变
createLinearGradient(x1, y1, x2, y2)
x1、y1表示渐变色开始点的坐标
x2、y2表示渐变色结束点的坐标
注:渐变的方向是从 x1y1 到 x2y2,如果y相同,那就是从左到右渐变,如果x相同,就是从上到下渐变,斜着渐变就需要自己调整两个位置的坐标了径向/圆 渐变
createRadialGradient(x1, y1, r1, x2, y2, r2)
x1、y1表示渐变开始圆心的坐标,r1表示渐变开始圆心的半径
x2、y2表示渐变结束圆心的坐标,r2表示渐变结束圆的半径。
注:当起点圆与终点圆的圆心坐标相同时,会有一种圆形渐变的效果
Canvas添加颜色的方法(指定停止颜色):addColorStop(value, color)
value:坐标来描述渐变的位置,取值 0~1(可以理解成整个渐变是0-100%的位置,从而设定在百分比位置的颜色)
color:渐变结束的颜色(停止颜色)
示例:
线性渐变:
// myCanvas 是canvas标签的ID
var canvas = document.getElementById("myCanvas");
if (canvas && canvas.getContext) {
let ctx = canvas.getContext("2d");
let grad = ctx.createLinearGradient(0, 150, 200, 150); // 创建一个渐变色线性对象
grad.addColorStop(0, "rgba(240,250,40,1)"); // 设置渐变颜色
grad.addColorStop(0.25, "rgba(327,201,64,1)");
grad.addColorStop(0.5, "rgba(22,184,200,1)");
grad.addColorStop(1, "rgba(82,67,192,1)");
ctx.fillStyle = grad; // 设置fillStyle为当前的渐变对象
ctx.fillRect(0, 0, 400, 400); // 绘制渐变图形
}

径向/圆渐变
var canvas = document.getElementById("myCanvas");
if (canvas && canvas.getContext) {
let ctx = canvas.getContext("2d");
let grad = ctx.createRadialGradient(200, 200, 50, 260, 260, 200) // 创建一个渐变色径向/圆对象
// let grad = ctx.createRadialGradient(200, 200, 50, 200, 200, 200) // 圆形渐变效果
grad.addColorStop(0, "rgba(240,250,40,1)"); // 设置渐变颜色
grad.addColorStop(0.25, "rgba(327,201,64,1)");
grad.addColorStop(0.5, "rgba(22,184,200,1)");
grad.addColorStop(1, "rgba(82,67,192,1)");
ctx.fillStyle = grad; // 设置fillStyle为当前的渐变对象
ctx.fillRect(0, 0, 500, 500); // 绘制渐变图形
}

边栏推荐
- Remote - actual combat
- [advanced mathematics] [3] Application of differential mean value theorem and derivative
- When MySQL resets the root password and modifies the password, an error occurs. The password field does not exist
- Leetcode-6126: designing a food scoring system
- Online random coin tossing positive and negative statistical tool
- [today in history] July 18: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal
- Scan delete folder problems
- 火山引擎项亮:机器学习与智能推荐平台多云部署解决方案正式发布
- 【单细胞高级绘图】07.KEGG富集结果展示
- Cloud native, Intel arch and cloud native secret computing three sig online sharing! See you today | issues 32-34
猜你喜欢

476-82(322、64、2、46、62、114)

Today's sleep quality record 75 points

Google guava is just a brother. What is the real king of caching? (glory Collection Edition)
![[today in history] July 19: the father of IMAP agreement was born; Project kotlin made a public appearance; New breakthroughs in CT imaging](/img/e9/5751dc435cfbbefc22d84fd9ebbaea.png)
[today in history] July 19: the father of IMAP agreement was born; Project kotlin made a public appearance; New breakthroughs in CT imaging

Unity vs -- the default debugging in VS is to start rather than attach to unity debugging

【单细胞高级绘图】07.KEGG富集结果展示

Struct, enum type and union
![[today in history] June 28: musk was born; Microsoft launched office 365; The inventor of Chua's circuit was born](/img/bf/09ccf36caec099098a22f0e8b670bd.png)
[today in history] June 28: musk was born; Microsoft launched office 365; The inventor of Chua's circuit was born

【ONNX】pytorch模型导出成ONNX格式:支持多参数与动态输入

IEC61131 address representation
随机推荐
[tensorrt] dynamic batch reasoning
Arrow parquet
结构体,枚举类型与联合体
程序的编译和运行
MySQL inserts three tables with different values. The association condition is the primary foreign key. How about the syntax of the insertion statement?
毕业从事弱电3个月,我为什么会选择转行网络工程师
Online random coin tossing positive and negative statistical tool
Matlab---eeglab check EEG signal
MPI学习笔记(二):矩阵相乘的两种实现方法
Remote—实战
雷达水位计的工作原理及安装维护注意事项
Remote—基本原理介绍
leetcode-6125:相等行列对
[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay
leetcode-6130:设计数字容器系统
MySQL 日期【加号/+】条件筛选问题
【NOI模拟赛】字符串匹配(后缀自动机SAM,莫队,分块)
How to use buffer queue to realize high concurrent order business (glory Collection Edition)
leetcode-114:二叉树展开为链表
Rand1 generates rand9