Javascript examples for Canvas:Draw
drawing a div element inside a canvas
<html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.js"></script> <script type="text/javascript" src="https://code.jquery.com/ui/1.9.2/jquery-ui.js"></script> <link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css"> <style id="compiled-css" type="text/css"> body {/* ww w. j a v a 2 s . co m*/ padding: 20px; } #x { border: 1px solid green; cursor: move; background:url('https://www.java2s.com/style/demo/Google-Chrome.png') no-repeat; height:40px; width:40px; } #x, #c { display: block; clear: both; } #c { margin-top: 30px; border: 1px solid red } </style> <script type="text/javascript"> $(window).load(function(){ $('#x').draggable(); $('#c').droppable({ drop: function (event, ui) { var img = ui.draggable; var bkURL = img.css("background-image"); bkURL = bkURL.substr(5).substr(0, bkURL.length - 7); var ctxt = $("#c")[0].getContext("2d"); var img = new Image(); img.onload = function () { ctxt.drawImage(img, 0, 0); } img.src = bkURL return false; } }); }); </script> </head> <body> <div id="x"> </div> <canvas id="c" width="200" height="200"> </canvas> </body> </html>