当前位置:网站首页>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!!!");
边栏推荐
- You Li takes you to talk about C language 6 (common keywords)
- Force buckle 144 Preorder traversal of binary tree
- 探索干货篇!Apifox 建设思路
- uniapp 移动端强制更新功能
- Thinkcmf6.0 installation tutorial
- [UVM practice] Chapter 2: a simple UVM verification platform (2) only driver verification platform
- [CV] Wu Enda machine learning course notes | Chapter 8
- Bugku CTF daily one question chessboard with only black chess
- 王爽 《汇编语言》之寄存器
- LeetCode简单题之判断一个数的数字计数是否等于数位的值
猜你喜欢
Ansible
Dedecms collects content without writing rules
快速使用 Jacoco 代码覆盖率统计
2022年茶艺师(中级)考试试题及模拟考试
Codeforce c.strange test and acwing
[quickstart to Digital IC Validation] 15. Basic syntax for SystemVerilog Learning 2 (operator, type conversion, loop, Task / Function... Including practical exercises)
海信电视开启开发者模式
QT learning 26 integrated example of layout management
Bugku CTF daily one question chessboard with only black chess
Linux server development, SQL statements, indexes, views, stored procedures, triggers
随机推荐
Network learning (III) -- highly concurrent socket programming (epoll)
Empire CMS collection Empire template program general
2022 tea master (intermediate) examination questions and mock examination
[UVM practice] Chapter 1: configuring the UVM environment (taking VCs as an example), run through the examples in the book
快解析内网穿透为文档加密行业保驾护航
[advanced digital IC Verification] command query method and common command interpretation of VCs tool
Jmeter 的使用
【数字IC验证快速入门】17、SystemVerilog学习之基本语法4(随机化Randomization)
B. Value sequence thinking
Qt学习27 应用程序中的主窗口
2022年茶艺师(中级)考试试题及模拟考试
青龙面板--花花阅读
3D reconstruction - stereo correction
Quickly use Jacobo code coverage statistics
Ansible
The zblog plug-in supports the plug-in pushed by Baidu Sogou 360
Yugu p1020 missile interception (binary search)
UnityHub破解&Unity破解
MySQL multi column index (composite index) features and usage scenarios
[UVM foundation] what is transaction