Skip to content

Instantly share code, notes, and snippets.

@nexpr
Created August 16, 2019 05:58
Show Gist options
  • Save nexpr/fb9186c1e0d5c6a37221837790679118 to your computer and use it in GitHub Desktop.
Save nexpr/fb9186c1e0d5c6a37221837790679118 to your computer and use it in GitHub Desktop.
clipboard api write image demo
<!DOCTYPE html>
<div>
<input type="color" id="color">
<input id="text">
<button id="wac">Write & Copy</button>
</div>
<div>
<canvas id="canvas" width=400 height=40 style="border: 1px solid silver"></canvas>
</div>
<script>
wac.onclick = () => {
const ctx = canvas.getContext("2d")
ctx.clearRect(0, 0, canvas.width, canvas.height)
ctx.fillStyle = color.value
ctx.font = "20px seif"
ctx.fillText(text.value, 15, 25)
const blob = canvas.toBlob(blob => {
navigator.clipboard.write([
new ClipboardItem({ [blob.type]: blob })
])
})
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment