Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use callback for toBlob in clipboard write example #33000

Merged
merged 6 commits into from
May 3, 2024
Merged
12 changes: 6 additions & 6 deletions files/en-us/web/api/clipboard/write/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,17 @@ A `try..catch` block could be used to catch any errors writing the data.
This example writes the canvas to a blob, using the default MIME type of `image/png`, and then writes the blob to the clipboard.

```js
// Get canvas can add an event handler for the click event.
const canvas = document.getElementById("canvas");
canvas.addEventListener("click", copyCanvasContentsToClipboard);

async function copyCanvasContentsToClipboard() {
// Copy canvas to blob
const blob = await canvas.toBlob();
// Create ClipboardItem with blob and it's type, and add to an array
const data = [new ClipboardItem({ [blob.type]: blob })];
// Write the data to the clipboard
await navigator.clipboard.write(data);
canvas.toBlob(async (blob) => {
// Create ClipboardItem with blob and it's type, and add to an array
const data = [new ClipboardItem({ [blob.type]: blob })];
Josh-Cena marked this conversation as resolved.
Show resolved Hide resolved
// Write the data to the clipboard
await navigator.clipboard.write(data);
});
}
```

Expand Down