[TOC]
# \<canvas\>元素
```html
<canvas id="tutorial" width="300" height="300"></canvas>
```
如果不給`<canvas>`設置`width`、`height`屬性,則默認`width`為 300、`height`為 150,單位都是`px`。也可以使用 css 屬性來設置寬高,但是如寬高屬性和初始比例不一致,他會出現扭曲。所以,建議永遠不要使用 css 屬性來設置`<canvas>`的寬高。
# 直線
```js
context.moveTo(x, y)
```
`moveTo()`的含義可以理解為 "將畫筆移動到(x,y)位置上,然后開始繪圖"。
```js
context.lineTo(x, y)
```
`lineTo()`理解為繪制直線到(x,y)位置即可。
```js
context.stroke()
context.stroke(path) // Path2D對象,IE 瀏覽器不支持
```
`stroke()`方法用于對路徑進行描邊。
<br />
繪制一個箭頭:之后代碼不一定會貼出完整的 html 了
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>arrow</title>
</head>
<body>
<canvas id="canvas" width="200" height="150" style="border: 1px dashed gray;"></canvas>
</body>
</html>
<script>
window.onload = function () {
let cnv = document.getElementById('canvas')
let cxt = cnv.getContext('2d')
cxt.moveTo(40, 60)
cxt.lineTo(100, 60)
cxt.lineTo(100, 30)
cxt.lineTo(150, 75)
cxt.lineTo(100, 120)
cxt.lineTo(100, 90)
cxt.lineTo(40, 90)
cxt.lineTo(40, 60)
cxt.stroke()
}
</script>
```
效果圖:

# 矩形
## "描邊" 矩形
我們可以使用`strokeStyle`屬性和`strokeRect()`方法來繪制一個"描邊"矩形,注意,我們稱`strokeStyle`為 context 對象的一個屬性,`strokeRect()`為 context 對象的一個方法。
<br />
語法:和描邊是相同的
```js
cxt.strokeStyle = 屬性值
cxt.strokeRect(x, y, width, height)
```
說明:x、y 表示矩形左上角的坐標,width、height 分別表示矩形的寬度和高度,如圖。

## "填充" 矩形
我們可以使用`fillStyle`屬性與`fillRect()`方法來繪制一個"填充"矩形。
語法:
```js
cxt.fillStyle = 屬性值
cxt.fillRect(x, y, width, height)
```

我們同時使用描邊和填充的效果如下:
```js
cxt.strokeStyle = 'red' // 先確定 Style 再繪制
cxt.strokeRect(50, 50, 80, 80)
cxt.fillStyle = '#FFE8E8'
cxt.fillRect(50, 50, 80, 80)
```

## rect() 方法
`rect()`方法也可以繪制矩形,與上面兩種方法略有不同。
語法:
```js
rect(x, y, width, height)
```
同樣的,x、y 為矩形的最左上角的坐標,width、height 分別表示矩形的寬度和高度。
其不同之處在于:`strokeRect()`和`fillRect()`這兩個方法在調用之后,會立即把矩形繪制出來,而`rect()`方法在調用之后,并不會把矩形繪制出來,只有在使用`rect()`方法之后再調用`stroke()`或`fill()`方法,才會繪制矩形。
<br />
代碼示例如下:
```js
// 繪制描邊矩形
cxt.strokeStyle = 'red'
cxt.rect(50, 50, 80, 80)
cxt.stroke()
// 繪制填充矩形
cxt.fillStyle = '#FFE8E8'
cxt.fill()
```

## 清空矩形
我們可以使用`clearRect()`方法來清空指定矩形區域。
語法:
```js
cxt.clearRect(x, y, width, height)
```
我們可以使用下面的代碼來清空整個 Canvas:
```js
context.clearRect(0, 0, context.width, context.height)
```
# 多邊形
## 繪制正多邊形
我們可以根據正多邊形的特點封裝一個函數`createPolygon()`來繪制正多邊形,以最簡單的正三角形為例:

```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>繪制正多邊形</title>
</head>
<body>
<canvas id="canvas" width="200" height="150" style="border: 1px dashed gray;"></canvas>
</body>
</html>
<script>
window.onload = function () {
let cnv = document.getElementById('canvas')
let cxt = cnv.getContext('2d')
// 調用自定義方法
createPolygon(cxt, 3, 100, 75, 50)
cxt.fillStyle = 'HotPink'
cxt.fill()
/*
* n: 表示 n 邊形
* dx、dy: n 邊形的中心坐標
* size: 表示 n 邊形的大小
*/
function createPolygon (cxt, n, dx, dy, size) {
cxt.beginPath() // 開始一條新的路徑
let degree = (2 * Math.PI) / n
for (let i = 0; i < n; i++) {
let x = Math.cos(i * degree)
let y = Math.sin(i * degree)
cxt.lineTo(x * size + dx, y * size + dy)
}
cxt.closePath() // 關閉路徑
}
}
</script>
```
  
## 繪制七巧板
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>canvas七巧板</title>
</head>
<body>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>canvas</title>
</head>
<body>
<!--不用 css 樣式指定而直接使用屬性-->
<canvas id="canvas" style="border: 1px solid gray; display: block; margin: 50px auto;"></canvas>
<script>
let tangram = [
{p:[{x:0,y:0},{x:800,y:0},{x:400,y:400}],color:'#caff67'},
{p:[{x:0,y:0},{x:400,y:400},{x:0,y:800}],color:'#67becf'},
{p:[{x:800,y:0},{x:800,y:400},{x:600,y:600},{x:600,y:200}],color:'#ef3d61'},
{p:[{x:600,y:200},{x:600,y:600},{x:400,y:400}],color:'#f9f51a'},
{p:[{x:400,y:400},{x:600,y:600},{x:400,y:800},{x:200,y:600}],color:'#a594c0'},
{p:[{x:200,y:600},{x:400,y:800},{x:0,y:800}],color:'#fa8ecc'},
{p:[{x:800,y:400},{x:800,y:800},{x:400,y:800}],color:'#caff67'},
]
window.onload = function () {
let canvas = document.getElementById('canvas');
canvas.width = 800;
canvas.height = 800;
let context = canvas.getContext('2d');
for(let i = 0; i < tangram.length; i++){
draw(tangram[i], context);
}
}
function draw (piece, cxt) { // 第一個參數是七巧板中的一塊,第二個是 context
cxt.beginPath();
cxt.moveTo(piece.p[0].x, piece.p[0].y);
for(let i = 1;i < piece.p.length; i++){
cxt.lineTo(piece.p[i].x, piece.p[i].y);
}
cxt.closePath();
cxt.fillStyle = piece.color;
cxt.fill();
cxt.strokeStyle = 'black';
cxt.lineWidth = 3;
cxt.stroke();
}
</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