Skip to content

Commit b3f3981

Browse files
committed
feat: 完善添加素材的方法
1 parent 7aeea6f commit b3f3981

File tree

1 file changed

+47
-40
lines changed

1 file changed

+47
-40
lines changed

src/components/player/CanvasPlayer.vue

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,10 @@ function initCanvasEvent() {
7272
}
7373
7474
// 绘制元素
75-
function drawElements() {
76-
drawVideo()
77-
drawStaticElements()
75+
async function drawElements() {
76+
await drawVideo(movie)
77+
addSVG(Logo)
78+
addText('开发中...')
7879
canvas.renderAll()
7980
}
8081
@@ -88,64 +89,70 @@ function onDelete(obj: fabric.Object) {
8889
menuShow.value = false
8990
}
9091
91-
// 绘制图片
92-
function drawStaticElements() {
93-
// 绘制 svg
94-
fabric.loadSVGFromURL(Logo, (objects, options) => {
92+
// 绘制 svg
93+
function addSVG(url: string) {
94+
fabric.loadSVGFromURL(url, (objects, options) => {
9595
const obj = fabric.util.groupSVGElements(objects, options)
9696
obj.set({
97-
scaleX: 2,
98-
scaleY: 2,
97+
scaleX: 8,
98+
scaleY: 8,
9999
left: canvas.width! / 2 - (obj.width! * 5) / 2,
100100
top: canvas.height! / 2 - (obj.height! * 5) / 2,
101101
angle: 0
102102
})
103103
canvas.add(obj)
104104
})
105-
// 添加文字
106-
const text = new fabric.Text('开发中...', {
107-
left: 20,
108-
top: 20,
105+
}
106+
107+
// 添加文字
108+
function addText(text: string) {
109+
const textElement = new fabric.Textbox(text, {
110+
left: canvas.width! / 2,
111+
top: canvas.height! / 2,
109112
fill: 'white',
110113
fontSize: 20
111114
})
112-
canvas.add(text)
115+
canvas.add(textElement)
113116
}
114117
115118
// 绘制视频
116119
// TODO 需要实现通过 webcodecs 进行视频解码后绘制
117-
function drawVideo() {
120+
async function drawVideo(url: string) {
118121
video = document.createElement('video')
119-
// video.src = 'https://assets.fedtop.com/picbed/movie.mp4'
120-
video.src = movie
122+
video.src = url
121123
video.loop = true
122124
video.preload = 'auto' // 不加会导致未播放时元素黑屏
123-
const canvasWidth = canvas.width!
124-
const canvasHeight = canvas.height!
125125
126-
video.addEventListener('loadedmetadata', () => {
127-
// 视频源宽高
128-
const videoWidth = video.videoWidth
129-
const videoHeight = video.videoHeight
130-
video.width = videoWidth
131-
video.height = videoHeight
132-
// 适配画布大小(如果 宽>高,以宽为准,反之以高为准)
133-
const scale = Math.min(canvasWidth / videoWidth, canvasHeight / videoHeight)
134-
const videoElement = new fabric.Image(video, {
135-
scaleX: scale,
136-
scaleY: scale,
137-
// 水平垂直居中
138-
left: canvasWidth / 2 - (videoWidth * scale) / 2,
139-
top: canvasHeight / 2 - (videoHeight * scale) / 2,
140-
angle: 0,
141-
originX: 'left',
142-
originY: 'top'
126+
await new Promise<void>((resolve) => {
127+
video.addEventListener('loadedmetadata', () => {
128+
// 视频源宽高
129+
video.width = video.videoWidth
130+
video.height = video.videoHeight
131+
resolve()
143132
})
144-
duration.value = video.duration
145-
canvas.add(videoElement)
146-
canvas.setActiveObject(videoElement)
147-
continuouslyRepaint()
148133
})
134+
135+
const videoWidth = video.width
136+
const videoHeight = video.height
137+
const canvasWidth = canvas.width!
138+
const canvasHeight = canvas.height!
139+
// 适配画布大小(如果 宽>高,以宽为准,反之以高为准)
140+
const scale = Math.min(canvasWidth / videoWidth, canvasHeight / videoHeight)
141+
const videoElement = new fabric.Image(video, {
142+
scaleX: scale,
143+
scaleY: scale,
144+
// 水平垂直居中
145+
left: canvasWidth / 2 - (videoWidth * scale) / 2,
146+
top: canvasHeight / 2 - (videoHeight * scale) / 2,
147+
angle: 0,
148+
originX: 'left',
149+
originY: 'top'
150+
})
151+
canvas.add(videoElement)
152+
canvas.setActiveObject(videoElement)
153+
continuouslyRepaint()
154+
duration.value = video.duration
155+
149156
video.addEventListener('timeupdate', () => {
150157
currentTime.value = video.currentTime
151158
})

0 commit comments

Comments
 (0)