当前位置:网站首页>JS复制图片到剪切板 读取剪切板
JS复制图片到剪切板 读取剪切板
2022-07-07 05:05:00 【qiphon3650】
JS复制图片到剪切板 读取剪切板

图片写入剪切板
function handleCopyImg() {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const img = new Image();
canvas.width = testImg.width;
canvas.height = testImg.height;
img.crossOrigin = "Anonymous";
img.src = testImg.src;
img.onload = () => {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
ctx.drawImage(img, 0, 0); // 将canvas转为blob
canvas.toBlob(async blob => {
console.log(blob);
const data = [
new ClipboardItem({
[blob.type]: blob,
}),
]; // https://w3c.github.io/clipboard-apis/#dom-clipboard-write
await navigator.clipboard.write(data)
.then(
() => {
console.log("Copied to clipboard successfully!");
},
() => {
console.error("Unable to write to clipboard.");
}
);
});
}}
btn.addEventListener("click", handleCopyImg, false);
read方法返回一个Promise,可获取复制的内容
async function foo() {
const items = await navigator.clipboard.read();
const imageBlob = await items[0].getType("image/png");
console.log("imageBlob==>", imageBlob);
}
// 读取文本
async function foo() {
await navigator.clipboard.readText().then((data) => {
console.log(data);
});
}
write方法,写入剪切板内容,如图片或其它文件等
const data = [
new ClipboardItem({
"text/plain": new Blob(["hello clipboard"], {
type: "text/plain" })
})
];
navigator.clipboard.write(data).then(
() => {
console.log("Copied to clipboard successfully!");
},
() => {
console.error("Unable to write to clipboard. :-(");
}
);
// writeText方法写入文本内容
navigator.clipboard.writeText("hello!!!");
边栏推荐
猜你喜欢

Dedecms collects content without writing rules

【数字IC验证快速入门】13、SystemVerilog interface 和 program 学习

Linux server development, MySQL index principle and optimization

Network learning (II) -- Introduction to socket

Jmeter 的使用

【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO

Qt学习27 应用程序中的主窗口

Custom class loader loads network class
![[quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)](/img/e1/9a047ef13299b94b5314ee6865ba26.png)
[quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)

Bugku CTF daily one question chessboard with only black chess
随机推荐
QT learning 28 toolbar in the main window
Pytorch(六) —— 模型调优tricks
[Matlab] Simulink 自定义函数中的矩阵乘法工作不正常时可以使用模块库中的矩阵乘法模块代替
Es FAQ summary
Codeforce c.strange test and acwing
2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案
有 Docker 谁还在自己本地安装 Mysql ?
UnityHub破解&Unity破解
Figure out the working principle of gpt3
Merging binary trees by recursion
Leetcode 40: combined sum II
The principle and implementation of buffer playback of large video files
【数字IC验证快速入门】12、SystemVerilog TestBench(SVTB)入门
【无标题】
Redis technology leak detection and filling (II) - expired deletion strategy
复杂网络建模(一)
Cnopendata geographical distribution data of religious places in China
Linux server development, MySQL process control statement
LeetCode中等题之我的日程安排表 I
Yugu p1020 missile interception (binary search)