Appearance
Canvans基本绘制
文字的绘制,动画
示例
代码
vue
<template>
<canvas id="canvas" :ref="el => canvasRef = (el as HTMLCanvasElement)"></canvas>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue'
const canvasRef = ref<HTMLCanvasElement>()
function initCanvasContext() {
const mainDom = document.querySelector("main.main");
const cvs = canvasRef.value
if (cvs && mainDom) {
cvs.width = mainDom.clientWidth * devicePixelRatio;
cvs.height = mainDom.clientWidth * devicePixelRatio;
const ctx = cvs.getContext("2d");
if (ctx) {
ctx.fillStyle = '#000'
ctx.fillRect(0, 0, cvs.width, cvs.height)
}
return ctx
}
return undefined
}
/** 获取随机字符 */
function getRandomChar() {
const str = '0123456789abcdefghijklmnopqrstuvwxyz'
return str[Math.floor(Math.random() * (str.length))]
}
function draw(ctx: CanvasRenderingContext2D, fontSize: number, charArr: number[]) {
const canvas = ctx.canvas
ctx.fillStyle = "rgba(0,0,0,0.1)"
ctx.fillRect(0, 0, canvas.width, canvas.height);
// 画文字
ctx.fillStyle = '#6be445';
ctx.textBaseline = 'top'
for (let i = 0; i < charArr.length; i++) {
const x = i * fontSize
const y = charArr[i] * fontSize
ctx.fillText(getRandomChar(), x, y)
if (y > canvas.height && Math.random() > 0.99) {
charArr[i] = 0
} else {
charArr[i]++
}
}
}
onMounted(() => {
const ctx = initCanvasContext()
if (ctx) {
const fontSize = 16 * devicePixelRatio
ctx.font = `${fontSize}px "Roboto Mono"`
const columnCount = Math.floor(ctx.canvas.width / fontSize)
const charArr = new Array(columnCount).fill(0)
const drawFont = () => draw(ctx, fontSize, charArr)
drawFont()
setInterval(drawFont, 50)
}
})
</script>
<style scoped lang="less">
#canvas {
width: 100%;
}
</style>
<template>
<canvas id="canvas" :ref="el => canvasRef = (el as HTMLCanvasElement)"></canvas>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue'
const canvasRef = ref<HTMLCanvasElement>()
function initCanvasContext() {
const mainDom = document.querySelector("main.main");
const cvs = canvasRef.value
if (cvs && mainDom) {
cvs.width = mainDom.clientWidth * devicePixelRatio;
cvs.height = mainDom.clientWidth * devicePixelRatio;
const ctx = cvs.getContext("2d");
if (ctx) {
ctx.fillStyle = '#000'
ctx.fillRect(0, 0, cvs.width, cvs.height)
}
return ctx
}
return undefined
}
/** 获取随机字符 */
function getRandomChar() {
const str = '0123456789abcdefghijklmnopqrstuvwxyz'
return str[Math.floor(Math.random() * (str.length))]
}
function draw(ctx: CanvasRenderingContext2D, fontSize: number, charArr: number[]) {
const canvas = ctx.canvas
ctx.fillStyle = "rgba(0,0,0,0.1)"
ctx.fillRect(0, 0, canvas.width, canvas.height);
// 画文字
ctx.fillStyle = '#6be445';
ctx.textBaseline = 'top'
for (let i = 0; i < charArr.length; i++) {
const x = i * fontSize
const y = charArr[i] * fontSize
ctx.fillText(getRandomChar(), x, y)
if (y > canvas.height && Math.random() > 0.99) {
charArr[i] = 0
} else {
charArr[i]++
}
}
}
onMounted(() => {
const ctx = initCanvasContext()
if (ctx) {
const fontSize = 16 * devicePixelRatio
ctx.font = `${fontSize}px "Roboto Mono"`
const columnCount = Math.floor(ctx.canvas.width / fontSize)
const charArr = new Array(columnCount).fill(0)
const drawFont = () => draw(ctx, fontSize, charArr)
drawFont()
setInterval(drawFont, 50)
}
})
</script>
<style scoped lang="less">
#canvas {
width: 100%;
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68