Appearance
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
html,
body {
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
</style>
</head>
<body>
<img src="./imgs/save-restore.png" alt="">
<canvas id="canvas"></canvas>
<script>
const canvas = document.getElementById('canvas')
const context = canvas.getContext('2d')
canvas.style.backgroundColor = '#ddd'
context.fillStyle = 'red'
context.font = '32px Arial'
context.fillText('Hello Canvas', 0, 50)
// 将当前状态放入栈中,保存 canvas 全部状态,保存到栈中的绘制状态由下面四部分组成:
// 1.当前的变换矩阵。
// 2.当前的剪切区域。
// 3.当前的虚线列表。
// 4.以下属性当前的值: strokeStyle, fillStyle, globalAlpha, lineWidth, lineCap,
// lineJoin, miterLimit, lineDashOffset, shadowOffsetX, shadowOffsetY,
// shadowBlur, shadowColor, globalCompositeOperation, font, textAlign,
// textBaseline, direction, imageSmoothingEnabled。
context.save()
context.fillStyle = 'green'
setTimeout(() => {
context.restore()
console.log(context.fillStyle) // -> #ff0000
}, 3000);
</script>
</body>
</html>