当前位置:网站首页>Canvas fill gradient
Canvas fill gradient
2022-07-25 20:50:00 【The most ferocious little seal】
Canvas The gradient of can be filled in the rectangle , circular , line , Text, etc. , Various shapes can define their own colors .
Be careful : You must use two or more colors to use gradients .
Canvas There are two ways to gradient :
linear The gradient
createLinearGradient(x1, y1, x2, y2)
x1、y1 Coordinates representing the starting point of the gradient
x2、y2 Represents the coordinates of the end point of the gradient
notes : The direction of the gradient is from x1y1 To x2y2, If y identical , That is the gradient from left to right , If x identical , It's a gradient from top to bottom , Gradient obliquely requires you to adjust the coordinates of the two positionsRadial / round The gradient
createRadialGradient(x1, y1, r1, x2, y2, r2)
x1、y1 Represents the coordinates of the center of the gradient start circle ,r1 Represents the radius of the center of the gradient start circle
x2、y2 Represents the coordinates of the center of the circle at the end of the gradient ,r2 Indicates the radius of the circle at the end of the gradient .
notes : When the center coordinates of the starting circle and the ending circle are the same , There will be a circular gradient effect
Canvas How to add color ( Specify stop color ):addColorStop(value, color)
value: Coordinates to describe the position of the gradient , Value 0~1( It can be understood that the whole gradient is 0-100% The location of , So as to set the color at the percentage position )
color: The color at the end of the gradient ( Stop color )
Example :
Linear gradient :
// myCanvas yes canvas Labeled ID
var canvas = document.getElementById("myCanvas");
if (canvas && canvas.getContext) {
let ctx = canvas.getContext("2d");
let grad = ctx.createLinearGradient(0, 150, 200, 150); // Create a gradient linear object
grad.addColorStop(0, "rgba(240,250,40,1)"); // Set gradient color
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; // Set up fillStyle For the current gradient object
ctx.fillRect(0, 0, 400, 400); // Draw gradient graphics
}

Radial / Circle gradient
var canvas = document.getElementById("myCanvas");
if (canvas && canvas.getContext) {
let ctx = canvas.getContext("2d");
let grad = ctx.createRadialGradient(200, 200, 50, 260, 260, 200) // Create a gradient radial / Circular object
// let grad = ctx.createRadialGradient(200, 200, 50, 200, 200, 200) // Circular gradient effect
grad.addColorStop(0, "rgba(240,250,40,1)"); // Set gradient color
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; // Set up fillStyle For the current gradient object
ctx.fillRect(0, 0, 500, 500); // Draw gradient graphics
}

边栏推荐
- [tensorrt] dynamic batch reasoning
- [paper reading] unpaired image to image translation using cycle consistent advantageous networks
- Online random coin tossing positive and negative statistical tool
- How to realize reliable transmission with UDP?
- Success factors of software R & D effectiveness measurement
- How to obtain the subordinate / annotation information of KEGG channel
- [noi simulation] string matching (suffix automata Sam, Mo team, block)
- Interpretation of filter execution sequence source code in sprigboot
- leetcode-114:二叉树展开为链表
- FanoutExchange交换机代码教程
猜你喜欢
![[MCU] 51 MCU burning those things](/img/fa/8f11ef64a18114365c084fff7d39f6.png)
[MCU] 51 MCU burning those things

Leetcode-79: word search

Embedded development: embedded foundation -- threads and tasks

Solution to oom exceptions caused by improper use of multithreading in production environment (supreme Collection Edition)
![Vulnhub | dc: 6 | [actual combat]](/img/7e/de7d5b56724bde5db2bb8338c35aa8.png)
Vulnhub | dc: 6 | [actual combat]

Brush questions with binary tree (4)

Remote—基本原理介绍
![[depth] the new LAAS agreement elephant: the key to revitalizing the development of the defi track](/img/ef/33f93225171e2c3e14b7d090c68196.png)
[depth] the new LAAS agreement elephant: the key to revitalizing the development of the defi track

Interpretation of filter execution sequence source code in sprigboot

Step num problem
随机推荐
网络协议:TCP Part2
How to use buffer queue to realize high concurrent order business (glory Collection Edition)
【TensorRT】动态batch进行推理
【TensorRT】trtexec工具转engine
C language file reading and writing
Network protocol: TCP part2
“链”接无限可能:数字资产链,精彩马上来!
ROS_ Rqt toolbox
【ONNX】pytorch模型导出成ONNX格式:支持多参数与动态输入
Pycharm跑程序时自动进入测试模式
2022.7.24-----leetcode.1184
Vulnhub | dc: 5 | [actual combat]
Automated testing ----- selenium (I)
Leetcode-6126: designing a food scoring system
Unity vs -- the default debugging in VS is to start rather than attach to unity debugging
A detailed explanation of SCP command
[workplace rules] it workplace rules | poor performance
Leetcode-6125: equal row and column pairs
Leetcode-919: complete binary tree inserter
The uniapp project starts with an error binding Node is not a valid Win32 Application ultimate solution