当前位置:网站首页>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
}

边栏推荐
- [cloud native] use of Nacos taskmanager task management
- 7.23
- MySQL date [plus sign / +] condition filtering problem
- 一道golang中defer和函数结合的面试题
- [advanced drawing of single cell] 07. Display of KEGG enrichment results
- Behind every piece of information you collect, you can't live without TA
- Explain in detail the principle of MySQL master-slave replication "suggestions collection"
- Go language go language built-in container
- Unity VS—— VS中默认调试为启动而不是附加到Unity调试
- 【TensorRT】动态batch进行推理
猜你喜欢

Today's sleep quality record 75 points
![[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

Struct, enum type and union

Too many passwords, don't know how to record? Why don't you write a password box applet yourself

How to use buffer queue to realize high concurrent order business (glory Collection Edition)

IEC61131 address representation

FanoutExchange交换机代码教程

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

How to choose a microservice registration center?

Recommended books | essentials of industrial digital transformation: methods and Practice
随机推荐
Matlab---eeglab check EEG signal
Key network protocols in tcp/ip four layer model
一道golang中关于接口和实现的面试题
Success factors of software R & D effectiveness measurement
Explain the principle of MySQL master-slave replication in detail
Kubernetes advanced part learning notes
每条你收藏的资讯背后,都离不开TA
MPI learning notes (II): two implementation methods of matrix multiplication
Hello, I'd like to ask questions about C and database operation.
Illustration leetcode - 3. longest substring without repeated characters (difficulty: medium)
How to choose a microservice registration center?
Docker builds redis cluster
The database empties the table data and makes the primary key start from 1
Has baozi ever played in the multi merchant system?
day04_ array
C language file reading and writing
[tensorrt] dynamic batch reasoning
What's special about Huawei's innovative solutions to consolidate the foundation of ERP for small and medium-sized enterprises?
Use of C log4net: add file name and line number to the output log content; Repackaged class output file name and line number
[MCU] 51 MCU burning those things