Skip to content

Commit cec2ec2

Browse files
committed
refactor: canvas
1 parent 696dd4b commit cec2ec2

File tree

4 files changed

+55
-91
lines changed

4 files changed

+55
-91
lines changed

app/pages/v-screen/data/web-camera.vue

+1-45
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function loadSequenceFrame() {
2121
const elapsed = timestamp - lastFrameTime
2222
2323
if (elapsed >= frameDuration) {
24-
drawSequenceFrame(`/sequence-frame/${frameCount}.jpg`)
24+
drawSequenceFrame(`/sequence-frame/${frameCount}.jpg`, canvasRef.value!)
2525
frameCount++
2626
lastFrameTime = timestamp - (elapsed % frameDuration)
2727
@@ -36,50 +36,6 @@ function loadSequenceFrame() {
3636
requestAnimationFrame(animate)
3737
}
3838
39-
function drawSequenceFrame(url: string) {
40-
const canvas = canvasRef.value
41-
if (!canvas) {
42-
console.error('Canvas element not found!')
43-
return
44-
}
45-
46-
const ctx = canvas.getContext('2d') as CanvasRenderingContext2D
47-
48-
const img = new Image()
49-
img.onload = () => {
50-
// 设置canvas尺寸
51-
canvas.width = canvas.clientWidth
52-
canvas.height = canvas.clientHeight
53-
54-
// 图片比例和canvas比例
55-
const imgRatio = img.width / img.height
56-
const canvasRatio = canvas.width / canvas.height
57-
58-
let drawWidth, drawHeight, drawX, drawY
59-
60-
// 根据比例确定绘制尺寸和位置
61-
if (imgRatio > canvasRatio) {
62-
drawWidth = canvas.width
63-
drawHeight = canvas.width / imgRatio
64-
drawX = 0
65-
drawY = (canvas.height - drawHeight) / 2
66-
}
67-
else {
68-
drawWidth = canvas.height * imgRatio
69-
drawHeight = canvas.height
70-
drawX = (canvas.width - drawWidth) / 2
71-
drawY = 0
72-
}
73-
74-
// 清除之前的内容
75-
ctx.clearRect(0, 0, canvas.width, canvas.height)
76-
77-
// 绘制图片
78-
ctx.drawImage(img, drawX, drawY, drawWidth, drawHeight - 15)
79-
}
80-
img.src = url
81-
}
82-
8339
// function drawSequenceFrameOnResize() {
8440
// if (imgArr.value.length > 0) {
8541
// drawKeyFrame(imgArr.value[0].src); // 当窗口大小变化时重新绘制图片

app/pages/v-screen/index/web-camera.vue

+2-46
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ const containerRef = ref<HTMLCanvasElement | null>(null)
66
const canvasRef = ref<HTMLCanvasElement | null>(null)
77
88
const { sendMessageToCpp } = useWebChannel({
9-
onDataUpdated(data: any) {
9+
async onDataUpdated(data: any) {
1010
window.console.log('这是QT数据:', data)
11-
drawSequenceFrame(data.img)
11+
await drawSequenceFrame(data, canvasRef.value!)
1212
sendMessageToCpp('这是JS数据')
1313
},
1414
})
@@ -62,50 +62,6 @@ const { sendMessageToCpp } = useWebChannel({
6262
// requestAnimationFrame(animate)
6363
// }
6464
65-
function drawSequenceFrame(url: string) {
66-
const canvas = canvasRef.value
67-
if (!canvas) {
68-
console.error('Canvas element not found!')
69-
return
70-
}
71-
72-
const ctx = canvas.getContext('2d') as CanvasRenderingContext2D
73-
74-
const img = new Image()
75-
img.onload = () => {
76-
// 设置canvas尺寸
77-
canvas.width = canvas.clientWidth
78-
canvas.height = canvas.clientHeight
79-
80-
// 图片比例和canvas比例
81-
const imgRatio = img.width / img.height
82-
const canvasRatio = canvas.width / canvas.height
83-
84-
let drawWidth, drawHeight, drawX, drawY
85-
86-
// 根据比例确定绘制尺寸和位置
87-
if (imgRatio > canvasRatio) {
88-
drawWidth = canvas.width
89-
drawHeight = canvas.width / imgRatio
90-
drawX = 0
91-
drawY = (canvas.height - drawHeight) / 2
92-
}
93-
else {
94-
drawWidth = canvas.height * imgRatio
95-
drawHeight = canvas.height
96-
drawX = (canvas.width - drawWidth) / 2
97-
drawY = 0
98-
}
99-
100-
// 清除之前的内容
101-
ctx.clearRect(0, 0, canvas.width, canvas.height)
102-
103-
// 绘制图片
104-
ctx.drawImage(img, drawX, drawY, drawWidth, drawHeight - 15)
105-
}
106-
img.src = url
107-
}
108-
10965
// function drawSequenceFrameOnResize() {
11066
// if (imgArr.value.length > 0) {
11167
// drawKeyFrame(imgArr.value[0].src); // 当窗口大小变化时重新绘制图片

app/utils/canvas.ts

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
export function drawSequenceFrame(url: string, canvas: HTMLCanvasElement): Promise<void> {
2+
return new Promise((resolve, reject) => {
3+
if (!canvas) {
4+
reject(new Error('Canvas element not found!'))
5+
return
6+
}
7+
8+
const ctx = canvas.getContext('2d') as CanvasRenderingContext2D
9+
10+
const img = new Image()
11+
img.onload = () => {
12+
// 设置canvas尺寸
13+
canvas.width = canvas.clientWidth
14+
canvas.height = canvas.clientHeight
15+
16+
// 图片比例和canvas比例
17+
const imgRatio = img.width / img.height
18+
const canvasRatio = canvas.width / canvas.height
19+
20+
let drawWidth, drawHeight, drawX, drawY
21+
22+
// 根据比例确定绘制尺寸和位置
23+
if (imgRatio > canvasRatio) {
24+
drawWidth = canvas.width
25+
drawHeight = canvas.width / imgRatio
26+
drawX = 0
27+
drawY = (canvas.height - drawHeight) / 2
28+
}
29+
else {
30+
drawWidth = canvas.height * imgRatio
31+
drawHeight = canvas.height
32+
drawX = (canvas.width - drawWidth) / 2
33+
drawY = 0
34+
}
35+
36+
// 清除之前的内容
37+
ctx.clearRect(0, 0, canvas.width, canvas.height)
38+
39+
// 绘制图片
40+
ctx.drawImage(img, drawX, drawY, drawWidth, drawHeight - 15)
41+
42+
resolve()
43+
}
44+
45+
img.onerror = () => {
46+
reject(new Error('Failed to load image'))
47+
}
48+
49+
img.src = url
50+
})
51+
}

app/utils/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './date'
22
export * from './storage'
33
export * from './mock'
4+
export * from './canvas'

0 commit comments

Comments
 (0)