当前位置:网站首页>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
What is the function of paralleling a capacitor on the feedback resistance of the operational amplifier circuit
Linux server development, MySQL transaction principle analysis
提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
Main window in QT learning 27 application
jeeSite 表单页面的Excel 导入功能
Padavan manually installs PHP
Introduction to basic components of wechat applet
Record a stroke skin bone error of the skirt
【数字IC验证快速入门】15、SystemVerilog学习之基本语法2(操作符、类型转换、循环、Task/Function...内含实践练习)
随机推荐
追风赶月莫停留,平芜尽处是春山
[quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)
数据库实时同步利器——CDC(变化数据捕获技术)
Linux server development, detailed explanation of redis related commands and their principles
The element with setfieldsvalue set is obtained as undefined with GetFieldValue
Main window in QT learning 27 application
Find the mode in the binary search tree (use medium order traversal as an ordered array)
JSON data flattening pd json_ normalize
eBPF Cilium实战(2) - 底层网络可观测性
互动送书-《Oracle DBA工作笔记》签名版
LeetCode简单题之找到一个数字的 K 美丽值
Lattice coloring - matrix fast power optimized shape pressure DP
The charm of SQL optimization! From 30248s to 0.001s
Recursive construction of maximum binary tree
Roulette chart 2 - writing of roulette chart code
拓维信息使用 Rainbond 的云原生落地实践
Rainbond 5.7.1 支持对接多家公有云和集群异常报警
【数字IC验证快速入门】11、Verilog TestBench(VTB)入门
buureservewp(2)
Example of file segmentation