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>
<canvas id="canvas"></canvas>
<img src="" alt="">
<script>
const img = document.querySelector('img')
const canvas = document.getElementById('canvas')
const context = canvas.getContext('2d')
canvas.style.backgroundColor = '#ddd'
// 离屏
canvas.style.display = 'none'
context.fillStyle = 'red'
context.font = '32px Arial'
context.fillText('Hello Canvas', 0, 50)
// 反映离屏 canvas 内容的图像
img.src = canvas.toDataURL()
</script>
</body>
</html>