这里写自定义目录标题
Canvas使用技巧汇总
本文章长期汇总Canvas的使用技巧,并长期更新。
清除并重置画布
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<button onclick="clearCanvas()">clear</button>
<canvas id="clear-canvas"></canvas>
</body>
</html>
#clear-canvas{
width: 300px;
height: 200px;
border: 1px solid blue;
}
window.onload = function (){
let canvasObj = document.getElementById("clear-canvas");
let canvasContext = canvasObj.getContext("2d");
canvasContext.moveTo(10, 10)
canvasContext.lineTo(80, 80)
canvasContext.stroke();
}
function clearCanvas(){
let canvasObj = document.getElementById("clear-canvas")
canvasObj.width = canvasObj.width // 同理height赋值也可以
}