drawing a div element inside a canvas - Javascript Canvas

Javascript examples for Canvas:Draw

Description

drawing a div element inside a canvas

Demo Code

ResultView the demo in separate window

<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">
         &nbsp;
      </div> 
      <canvas id="c" width="200" height="200">  
      </canvas>
   </body>
</html>

Related Tutorials