Skip to content

Commit

Permalink
feat:优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
gzydong committed Dec 17, 2023
1 parent 67b8957 commit 9fc7686
Showing 1 changed file with 45 additions and 10 deletions.
55 changes: 45 additions & 10 deletions src/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,48 @@ export async function clipboardImage(src, callback) {
const data = await fetch(src)
const blob = await data.blob()

if (blob.type != 'image/png') {
return alert('当前图片类型不支持复制')
}
// navigator.clipboard.write 仅支持 png 图片
if (blob.type == 'image/png') {
await navigator.clipboard.write([
new ClipboardItem({
[blob.type]: blob
})
])

await navigator.clipboard.write([
new ClipboardItem({
[blob.type]: blob
})
])
return callback()
}

callback()
const objectURL = URL.createObjectURL(blob)

const img = new Image()
img.src = URL.createObjectURL(blob)
img.onload = () => {
const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d')

canvas.width = img.width
canvas.height = img.height
ctx.drawImage(img, 0, 0)

canvas.toBlob(
(blob) => {
const data = [new ClipboardItem({ [blob.type]: blob })]

navigator.clipboard
.write(data)
.then(() => {
callback()
})
.catch((err) => {
console.error(err)
})

URL.revokeObjectURL(objectURL)
},
'image/png',
1
)
}
} catch (err) {
console.error(err.name, err.message)
}
Expand Down Expand Up @@ -129,7 +160,9 @@ export function getVideoImage(file, time = 1) {
return new Promise((resolve) => {
let video = document.createElement('video')

video.src = URL.createObjectURL(file)
const objectURL = URL.createObjectURL(file)

video.src = objectURL
video.currentTime = time
video.autoplay = true
video.muted = true
Expand All @@ -156,6 +189,8 @@ export function getVideoImage(file, time = 1) {
lastModified: Date.now()
})

URL.revokeObjectURL(objectURL)

resolve(data)
}, 'image/jpeg')
}
Expand Down

0 comments on commit 9fc7686

Please sign in to comment.