Allows copy and pasting or drag and drop of images onto a canvas element. The canvas will grow/shrink to accommodate the image size. Demos how to send base 64 string server-side in a form post.
<form id="myForm">
<div id="instructions">
Method 1:<br /> 1. Copy image data into clipboard, or press Print Screen <br /> 2. Press Ctrl+V (page/iframe must be focused): <br /><br /> Method 2:<br /> 1. Drag and drop an image onto the canvas
</div>
<br /><br />
<canvas style="border:1px solid grey;" id="my_canvas" width="300" height="300"></canvas>
<div class="form-group-sm">
<button id="saveButton" type="button" value="Save" class="btn btn-lg btn-primary">Save</button>
</div>
<input type="hidden" id="imageData" />
</form>
JavaScript
var CLIPBOARD = new CLIPBOARD_CLASS("my_canvas", true);
/**
* image pasting into canvas
*
* @param {string} canvas_id - canvas id
* @param {boolean} autoresize - if canvas will be resized
*/
function CLIPBOARD_CLASS(canvas_id, autoresize) {
var _self = this;
var canvas = document.getElementById(canvas_id);
var ctx = document.getElementById(canvas_id).getContext("2d");
//handlers
document.addEventListener('paste', function(e) {
_self.paste_auto(e);
}, false);
/* events fired on the drop targets */
document.addEventListener("dragover", function(e) {
// prevent default to allow drop
e.preventDefault();
}, false);
document.addEventListener('drop', function(e) {
// prevent default action (open as link for some elements)
// add event handler to canvas if desired instead of document
//debugger;
e.preventDefault();
var items = e.dataTransfer.items;
for (var i = 0; i < items.length; i++) {
if (items[i].type.indexOf("image") !== -1) {
document.getElementById("instructions").style.visibility = "hidden";
//image
var blob = items[i].getAsFile();
var URLObj = window.URL || window.webkitURL;
var source = URLObj.createObjectURL(blob);
_self.paste_createImage(source);
}
}
});
//on paste
this.paste_auto = function(e) {
if (e.clipboardData) {
var items = e.clipboardData.items;
if (!items) return;
//access data directly
for (var i = 0; i < items.length; i++) {
if (items[i].type.indexOf("image") !== -1) {
//image
var blob = items[i].getAsFile();
var URLObj = window.URL || window.webkitURL;
var source = URLObj.createObjectURL(blob);
this.paste_createImage(source);
}
}
e.preventDefault();
}
};
//draw pasted image to canvas
this.paste_createImage = function(source) {
//debugger;
var pastedImage = new Image();
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!