Skip to content

Instantly share code, notes, and snippets.

@JRGould
Last active August 5, 2026 17:01
Show Gist options
  • Select an option

  • Save JRGould/599e267321850a2f836c0690b86a5cca to your computer and use it in GitHub Desktop.

Select an option

Save JRGould/599e267321850a2f836c0690b86a5cca to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Paste and View Image</title>
<style>
html, body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
background-color: #f4f4f9;
font-family: Arial, sans-serif;
}
#ui-wrapper {
text-align: center;
padding: 20px;
box-sizing: border-box;
}
#paste-zone {
border: 3px dashed #cccccc;
padding: 40px;
background: #ffffff;
cursor: pointer;
max-width: 500px;
margin: 20px auto;
border-radius: 8px;
}
#paste-zone:hover {
border-color: #007bff;
background-color: #f0f7ff;
}
/* Style for the isolated image preview */
#preview-container {
display: flex;
justify-content: center;
align-items: flex-start;
width: 100%;
}
img {
height: auto;
display: block;
}
</style>
</head>
<body>
<!-- Container for all setup elements so we can easily hide them -->
<div id="ui-wrapper">
<h1>Paste & View Your Image</h1>
<p>Click inside the box below and press <strong>Ctrl + V</strong> (Windows) or <strong>Cmd + V</strong> (Mac).</p>
<div id="paste-zone" tabindex="0">
Click here and paste your image
</div>
</div>
<!-- The area where the isolated image will display -->
<div id="preview-container"></div>
<script>
const uiWrapper = document.getElementById('ui-wrapper');
const pasteZone = document.getElementById('paste-zone');
const previewContainer = document.getElementById('preview-container');
// Automatically auto-focus the paste box on page load
pasteZone.focus();
pasteZone.addEventListener('paste', function (e) {
const items = (e.clipboardData || e.originalEvent.clipboardData).items;
for (let i = 0; i < items.length; i++) {
if (items[i].type.indexOf('image') !== -1) {
const blob = items[i].getAsFile();
const imageURL = URL.createObjectURL(blob);
// 1. Completely remove the instructions and setup UI from view
uiWrapper.style.display = 'none';
document.body.style.backgroundColor = '#ffffff';
// 2. Create the image element
const img = document.createElement('img');
img.src = imageURL;
img.alt = "Pasted Image";
// 3. Detect pixel density and apply 50% physical width if 2x or higher
// window.devicePixelRatio returns 2 on Retina/High-DPI screens
if (window.devicePixelRatio >= 2) {
img.srcset = imageURL + ' 2x';
}
img.style.maxWidth = '100%';
// 4. Render the image cleanly on the page
previewContainer.appendChild(img);
return;
}
}
alert("No image data found in your clipboard!");
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment