本文最后更新于:2023年10月23日 晚上
创建画布
1 2 3 4
| <body> <canvas id="canvas" width="300" height="300"></canvas> <script type="module" src="/src/main.ts"></script> </body>
|
获取画布
1 2 3
| const el: HTMLCanvasElement = document.querySelector<HTMLCanvasElement>("#canvas")!; const app = el.getContext("2d")!;
|
填充矩形
1 2 3 4
| app.fillStyle = "red";
app.fillRect(0, 0, el.offsetWidth, el.offsetHeight);
|

1 2 3
| app.fillStyle = "green";
app.fillRect(el.offsetWidth / 2 - 50, el.offsetHeight / 2 - 50, 100, 100);
|

画矩形
1 2 3 4 5 6 7 8
| app.strokeStyle = "#99B080";
app.lineWidth = 30;
app.lineJoin = "round";
app.strokeRect(50, 50, 200, 200);
|

画圆
官方文档
1 2 3 4 5 6 7
| app.strokeStyle = "#ED7D31"; app.lineWidth = 20;
app.arc(150, 150, 100, 0, 2 * Math.PI);
app.stroke();
|

不规则形状
绘制倒正三角
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| app.beginPath();
app.strokeStyle = "#E95793";
app.moveTo(50, 50);
app.lineTo(150, 50);
app.lineTo(100, 100);
app.closePath();
app.lineWidth = 20;
app.stroke();
|

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| app.beginPath();
app.fillStyle = "#E95793";
app.moveTo(50, 50);
app.lineTo(150, 50);
app.lineTo(100, 100);
app.closePath();
app.lineWidth = 20;
app.fill();
|

渐变色
官方文档
1 2 3 4 5 6
| const gradient = app.createLinearGradient(150, 0, 300, 300); gradient.addColorStop(0, "#DA0C81"); gradient.addColorStop(1, "#610C9F");
app.fillStyle = gradient; app.fillRect(0, 0, 300, 300);
|

1 2 3 4 5 6 7 8
| const gradient = app.createLinearGradient(150, 0, 300, 300); gradient.addColorStop(0, "#DA0C81"); gradient.addColorStop(1, "#610C9F");
app.strokeStyle = gradient; app.lineWidth = 20; app.lineJoin = "round"; app.strokeRect(50, 50, 200, 200);
|

文本绘制
官方文档
1 2 3 4 5 6 7 8 9 10 11
| app.fillStyle = "#b84592 "; app.fillRect(0, 0, el.width, el.height);
app.font = "40px Arial";
app.strokeStyle = "white";
app.textBaseline = "top"; app.lineWidth = 2;
app.strokeText("晚生隆海", 70, 100);
|

图片贴图
官方文档
贴图无法放的位置和缩放的大小,图片绘制可实现
1 2 3 4 5 6 7 8 9 10 11
| const img = document.createElement("img"); img.src = "./images/wepay.png"; img.onload = () => { const pattern = app.createPattern(img, "repeat")!; app.fillStyle = pattern; app.fillRect(0, 0, 300, 300); };
|

图片绘制
官方文档
1 2 3 4 5 6 7
| const img = document.createElement("img"); img.src = "./images/1.jpg"; img.onload = () => { app.drawImage(img, 50, 50, 200, 200); };
|

等比例缩放
1 2 3 4 5 6 7 8 9 10 11 12
| const img = document.createElement("img"); img.src = "./images/1.jpg"; img.onload = () => { el.width = img.naturalWidth * scale(img, el); el.height = img.naturalHeight * scale(img, el); app.drawImage(img, 0, 0, el.width, el.height); };
function scale(img: HTMLImageElement, el: HTMLCanvasElement) { return Math.min(el.width / img.naturalWidth, el.height / img.naturalHeight); }
|

黑板案例
创建项目
1 2 3 4 5 6 7 8 9 10 11 12
| PS D:\thl\ts_demo\canvas> npm create vite √ Project name: ... blackboard ? Select a frameork: » - Use arrow-keys. Return to submit. > Vanilla Vue React Preact Lit Svelte Solid Qwik Others
|
输入项目名
1
| √ Project name: ... blackboard
|
选择脚手架
1 2 3
| ? Select a framework: » - Use arrow-keys. Return to submit. > Vanilla > TypeScript
|
安装依赖并运行
详情代码见仓库
https://github.com/pythl/blackboard