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

边栏推荐
- MPI学习笔记(二):矩阵相乘的两种实现方法
- Leetcode-919: complete binary tree inserter
- Open source SPL enhances mangodb computing
- 牛客-TOP101-BM37
- The uniapp project starts with an error binding Node is not a valid Win32 Application ultimate solution
- Vulnhub | dc: 6 | [actual combat]
- 每条你收藏的资讯背后,都离不开TA
- 【TensorRT】trtexec工具转engine
- Unity VS—— VS中默认调试为启动而不是附加到Unity调试
- [leetcode] 28. Implement strstr ()
猜你喜欢

两数,三数之和

Success factors of software R & D effectiveness measurement

基于腾讯地图实现精准定位,实现微信小程序考勤打卡功能
What's special about Huawei's innovative solutions to consolidate the foundation of ERP for small and medium-sized enterprises?

Learn FPGA from the bottom structure (16) -- customization and testing of pll/mmcm IP

【网络教程】IPtables官方教程--学习笔记2

103. (cesium chapter) cesium honeycomb diagram (square)

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

Leetcode-155: minimum stack

Fanoutexchange switch code tutorial
随机推荐
matlab----EEGLab查看脑电信号
Kubernetes advanced part learning notes
[advanced drawing of single cell] 07. Display of KEGG enrichment results
[MCU] 51 MCU burning those things
MPI learning notes (II): two implementation methods of matrix multiplication
【FiddlerTX插件】使用Fiddler抓包腾讯课堂视频下载(抓不到包解决方案)
Detailed explanation of document operation
Clickhouse notes 02 -- installation test clickvisual
Using the OAP aspect causes the controller to be called repeatedly
leetcode-155:最小栈
103. (cesium chapter) cesium honeycomb diagram (square)
Golang language quickly get started to comprehensive practical notes (go language, beego framework, high concurrency chat room, crawler)
Behind every piece of information you collect, you can't live without TA
Volcanic engine Xiang Liang: machine learning and intelligent recommendation platform multi cloud deployment solution officially released
C language file reading and writing
Force deduction ----- calculate the money of the force deduction bank
Solution to oom exceptions caused by improper use of multithreading in production environment (supreme Collection Edition)
KEGG通路的从属/注释信息如何获取
476-82(322、64、2、46、62、114)
牛客-TOP101-BM38