当前位置:网站首页>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!!!");
边栏推荐
- 芯片 設計資料下載
- 王爽 《汇编语言》之寄存器
- Cnopendata American Golden Globe Award winning data
- 2022 welder (elementary) judgment questions and online simulation examination
- Most elements
- Network learning (II) -- Introduction to socket
- LeetCode 90:子集 II
- paddlepaddle 29 无模型定义代码下动态修改网络结构(relu变prelu,conv2d变conv3d,2d语义分割模型改为3d语义分割模型)
- Network learning (I) -- basic model learning
- 海信电视开启开发者模式
猜你喜欢
LeetCode简单题之字符串中最大的 3 位相同数字
【数字IC验证快速入门】11、Verilog TestBench(VTB)入门
Detailed explanation of Kalman filter for motion state estimation
Thinkcmf6.0安装教程
Visualization Document Feb 12 16:42
Padavan manually installs PHP
json 数据展平pd.json_normalize
Introduction to basic components of wechat applet
Most elements
2022年茶艺师(中级)考试试题及模拟考试
随机推荐
Visualization Document Feb 12 16:42
力扣(LeetCode)187. 重复的DNA序列(2022.07.06)
探索干货篇!Apifox 建设思路
让Livelink初始Pose与动捕演员一致
json 数据展平pd.json_normalize
2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions
微信小程序基本组件使用介绍
快解析内网穿透助力外贸管理行业应对多种挑战
Binary tree and heap building in C language
Network learning (III) -- highly concurrent socket programming (epoll)
Numbers that appear only once
Es FAQ summary
【数字IC验证快速入门】12、SystemVerilog TestBench(SVTB)入门
mysql多列索引(组合索引)特点和使用场景
JS quick start (I)
央视太暖心了,手把手教你写HR最喜欢的简历
Introduction to basic components of wechat applet
Linux server development, MySQL cache strategy
Linux server development, MySQL transaction principle analysis
2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案