[TOC]
## 線性漸變
語法:
```js
let gnt = cxt.createLinearGradient(x1, y1, x2, y2)
gnt.addColorStop(value1, color1)
gnt.addColorStop(value2, color2)
cxt.fillStyle = gnt
cxt.fill()
```
說明:在 Canvas 中,實現線性漸變有以下三個步驟
(1) 調用 createLinearGradient() 方法創建一個 linearGradient 對象,并賦值給變量 gnt
(2) 調用 linearGradient 對象的 addColorStop() 方法 N 次,第 1 次表示漸變開始的顏色,第 2 次表示漸變結束時的顏色。然后第 3 次則以第 2 次漸變顏色作為開始顏色,進行漸變,依此類推。
(3) 把 linearGradient 對象賦值給 fillStyle 屬性,并調用 fill() 方法來繪制有漸變色的圖形。
```
var gnt = cxt.createLinearGradient(x1, y1, x2, y2)
```
x1、y1 表示漸變色開始點的坐標,x2、y2 表示漸變色結束點的坐標,其有以下關系:
- 如果 y1 與 y2 相同,表示沿水平方向從左到右漸變
- 如果 x1 與 x2 相同,表示沿著垂直方向從左到右漸變
- 如果 x1 與 x2 不相同,且 y1 與 y2 也不相同,則表示漸變色沿著矩形對角線方向漸變
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>漸變</title>
</head>
<body>
<canvas id="canvas" style="margin:0 auto; border: 1px solid gray"></canvas>
<script>
window.onload = function () {
let canvas = document.getElementById('canvas')
canvas.width = 600
canvas.height = 600
let context = canvas.getContext('2d')
let linearGrad = context.createLinearGradient(0,0,400,400) // 改變坐標來改變漸變方向
linearGrad.addColorStop(0,'white') // ColorStop 可以添加無數個,相當于一個中間狀態
linearGrad.addColorStop(0.25,'yellow') // 只能用小數不能用百分號...
linearGrad.addColorStop(0.5,'green')
linearGrad.addColorStop(0.75,'blue')
linearGrad.addColorStop(1,'black')
context.fillStyle = linearGrad
context.fillRect(0,0,800,800)
}
</script>
</body>
</html>
<!--
createLinearGradient(xstart,ystart,xend,yend) 漸變開始及結束的 x y 坐標 創建線性漸變
addColorStop(stop,color) 規定漸變對象中的顏色和停止位置
-->
```
下圖分別是橫向,縱向,對角的線型漸變的效果
  
## 陰影
| 屬性 | 說明 |
| --- | --- |
| shadowOffsetX | 陰影與圖形的水平距離,默認值為 0。大于 0 時向右偏移,小于 0 時向左偏移 |
| shadowOffsetY | 陰影與圖形的垂直距離,默認值為 0。大于 0 時向下偏移,小于 0 時向上偏移 |
| shadowColor | 陰影的顏色,默認為黑色 |
| shadowBlur | 陰影的模糊值,默認為 0。該值越大,模糊度越強 |
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>canvas</title>
</head>
<body>
<canvas id="canvas" width="1024" height="700" style="border: 1px solid gray; display: block; margin: 0 auto"></canvas>
<script>
window.onload = function(){
let cnv = document.getElementById('canvas')
let cxt = cnv.getContext('2d')
// 定義文字
const text = 'Canvas 陰影'
cxt.font = "bold 60px 微軟雅黑"
// 定義陰影
cxt.shadowOffsetX = 5
cxt.shadowOffsetY = 5
cxt.shadowColor = 'LightSkyBlue'
cxt.shadowBlur = 10
// 填充文字
cxt.fillStyle = 'HotPink'
cxt.fillText(text, 10, 90)
// 圖片陰影
let image = new Image()
image.src = './image.jpg'
image.onload = function () {
cxt.shadowOffsetX = 0
cxt.shadowOffsetY = 0
cxt.shadowColor = '#FF6666'
cxt.shadowBlur = 10
cxt.fillRect(10, 150, 500, 281)
cxt.drawImage(image, 10, 150)
}
}
</script>
</body>
</html>
```

- 序言 & 更新日志
- H5
- Canvas
- 序言
- Part1-直線、矩形、多邊形
- Part2-曲線圖形
- Part3-線條操作
- Part4-文本操作
- Part5-圖像操作
- Part6-變形操作
- Part7-像素操作
- Part8-漸變與陰影
- Part9-路徑與狀態
- Part10-物理動畫
- Part11-邊界檢測
- Part12-碰撞檢測
- Part13-用戶交互
- Part14-高級動畫
- CSS
- SCSS
- codePen
- 速查表
- 面試題
- 《CSS Secrets》
- SVG
- 移動端適配
- 濾鏡(filter)的使用
- JS
- 基礎概念
- 作用域、作用域鏈、閉包
- this
- 原型與繼承
- 數組、字符串、Map、Set方法整理
- 垃圾回收機制
- DOM
- BOM
- 事件循環
- 嚴格模式
- 正則表達式
- ES6部分
- 設計模式
- AJAX
- 模塊化
- 讀冴羽博客筆記
- 第一部分總結-深入JS系列
- 第二部分總結-專題系列
- 第三部分總結-ES6系列
- 網絡請求中的數據類型
- 事件
- 表單
- 函數式編程
- Tips
- JS-Coding
- Framework
- Vue
- 書寫規范
- 基礎
- vue-router & vuex
- 深入淺出 Vue
- 響應式原理及其他
- new Vue 發生了什么
- 組件化
- 編譯流程
- Vue Router
- Vuex
- 前端路由的簡單實現
- React
- 基礎
- 書寫規范
- Redux & react-router
- immutable.js
- CSS 管理
- React 16新特性-Fiber 與 Hook
- 《深入淺出React和Redux》筆記
- 前半部分
- 后半部分
- react-transition-group
- Vue 與 React 的對比
- 工程化與架構
- Hybird
- React Native
- 新手上路
- 內置組件
- 常用插件
- 問題記錄
- Echarts
- 基礎
- Electron
- 序言
- 配置 Electron 開發環境 & 基礎概念
- React + TypeScript 仿 Antd
- TypeScript 基礎
- React + ts
- 樣式設計
- 組件測試
- 圖標解決方案
- Storybook 的使用
- Input 組件
- 在線 mock server
- 打包與發布
- Algorithm
- 排序算法及常見問題
- 劍指 offer
- 動態規劃
- DataStruct
- 概述
- 樹
- 鏈表
- Network
- Performance
- Webpack
- PWA
- Browser
- Safety
- 微信小程序
- mpvue 課程實戰記錄
- 服務器
- 操作系統基礎知識
- Linux
- Nginx
- redis
- node.js
- 基礎及原生模塊
- express框架
- node.js操作數據庫
- 《深入淺出 node.js》筆記
- 前半部分
- 后半部分
- 數據庫
- SQL
- 面試題收集
- 智力題
- 面試題精選1
- 面試題精選2
- 問答篇
- 2025面試題收集
- Other
- markdown 書寫
- Git
- LaTex 常用命令
- Bugs