当前位置:网站首页>JS copy picture to clipboard read clipboard
JS copy picture to clipboard read clipboard
2022-07-07 08:11:00 【qiphon3650】
JS Copy the image to the clipboard Read the clipboard
navigator.clipboard Copy pictures

Picture writing clipboard
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); // take canvas To 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 Method returns a Promise, Get the copied content
async function foo() {
const items = await navigator.clipboard.read();
const imageBlob = await items[0].getType("image/png");
console.log("imageBlob==>", imageBlob);
}
// Read text
async function foo() {
await navigator.clipboard.readText().then((data) => {
console.log(data);
});
}
write Method , Write clipboard contents , Such as pictures or other documents
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 Method to write text content
navigator.clipboard.writeText("hello!!!");
边栏推荐
- LeetCode简单题之找到一个数字的 K 美丽值
- 复杂网络建模(一)
- 复杂网络建模(二)
- Blob 对象介绍
- Pytorch(六) —— 模型调优tricks
- JS cross browser parsing XML application
- Real time monitoring of dog walking and rope pulling AI recognition helps smart city
- 【數字IC驗證快速入門】15、SystemVerilog學習之基本語法2(操作符、類型轉換、循環、Task/Function...內含實踐練習)
- Recursive construction of maximum binary tree
- 漏洞複現-Fastjson 反序列化
猜你喜欢

Make LIVELINK's initial pose consistent with that of the mobile capture actor

Introduction to basic components of wechat applet

Vulnerability recurrence easy_ tornado

LeetCode简单题之字符串中最大的 3 位相同数字

【数字IC验证快速入门】14、SystemVerilog学习之基本语法1(数组、队列、结构体、枚举、字符串...内含实践练习)

调用 pytorch API完成线性回归

buureservewp(2)

Quickly use Jacobo code coverage statistics

Who has docker to install MySQL locally?

Myabtis_ Plus
随机推荐
追风赶月莫停留,平芜尽处是春山
JSON data flattening pd json_ normalize
Thinkcmf6.0 installation tutorial
快解析内网穿透助力外贸管理行业应对多种挑战
Complete linear regression manually based on pytoch framework
Complex network modeling (III)
云原生存储解决方案Rook-Ceph与Rainbond结合的实践
青龙面板-今日头条
Force buckle 144 Preorder traversal of binary tree
What is the function of paralleling a capacitor on the feedback resistance of the operational amplifier circuit
Main window in QT learning 27 application
Pytorch(六) —— 模型调优tricks
Record a stroke skin bone error of the skirt
Network learning (III) -- highly concurrent socket programming (epoll)
【无标题】
OpenJudge NOI 2.1 1752:鸡兔同笼
Recursive method to construct binary tree from preorder and inorder traversal sequence
Qinglong panel -- finishing usable scripts
基于Pytorch 框架手动完成线性回归
Minimum absolute difference of binary search tree (use medium order traversal as an ordered array)