当前位置:网站首页>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!!!");
边栏推荐
- QT learning 26 integrated example of layout management
- 2022 National latest fire-fighting facility operator (primary fire-fighting facility operator) simulation questions and answers
- 力扣(LeetCode)187. 重复的DNA序列(2022.07.06)
- Linux server development, MySQL index principle and optimization
- LeetCode简单题之找到一个数字的 K 美丽值
- Introduction to basic components of wechat applet
- Linux server development, detailed explanation of redis related commands and their principles
- ZCMU--1492: Problem D(C语言)
- Paddlepaddle 29 dynamically modify the network structure without model definition code (relu changes to prelu, conv2d changes to conv3d, 2D semantic segmentation model changes to 3D semantic segmentat
- C language queue
猜你喜欢

Call pytorch API to complete linear regression

buureservewp(2)

The simple problem of leetcode is to judge whether the number count of a number is equal to the value of the number

Li Kou interview question 04.01 Path between nodes

Game attack and defense world reverse

LeetCode简单题之判断一个数的数字计数是否等于数位的值

Implementation of replacement function of shell script

Linux server development, redis protocol and asynchronous mode

快解析内网穿透为文档加密行业保驾护航

Vulnerability recurrence fastjson deserialization
随机推荐
Network learning (I) -- basic model learning
Dedecms collects content without writing rules
Linux server development, MySQL cache strategy
轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
复杂网络建模(一)
Force buckle 144 Preorder traversal of binary tree
LeetCode简单题之判断一个数的数字计数是否等于数位的值
JS复制图片到剪切板 读取剪切板
拓维信息使用 Rainbond 的云原生落地实践
jeeSite 表单页面的Excel 导入功能
ZCMU--1396: 队列问题(2)
Introduction à l'objet blob
Linux server development, redis source code storage principle and data model
Who has docker to install MySQL locally?
Network learning (III) -- highly concurrent socket programming (epoll)
Leetcode 187 Repeated DNA sequence (2022.07.06)
2022 Inner Mongolia latest advanced fire facility operator simulation examination question bank and answers
uniapp 移动端强制更新功能
Rainbond结合NeuVector实践容器安全管理
Bugku CTF daily one question chessboard with only black chess