Advertisement
bowenac

move image

Jan 7th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <head>
  2.  
  3.     <title>Export and save the canvas as PNG</title>
  4.  
  5.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  6.     <meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
  7.    
  8.     <script src="http://code.createjs.com/easeljs-0.5.0.min.js"></script>
  9.     <script src="http://code.jquery.com/jquery-1.8.2.js"></script>
  10.  
  11.     <script src="libs/export_canvas/base64.js" type="text/javascript"></script>
  12.     <script src="libs/export_canvas/canvas2image.js" type="text/javascript"></script>
  13.  
  14. </head>
  15.  
  16. <script type="text/javascript">
  17.     var canvas;
  18.     var stage;
  19.  
  20.     /**
  21.     * Init
  22.     */
  23.     function init() {
  24.  
  25.         canvas = document.getElementById("canvas");
  26.         stage = new createjs.Stage(canvas);
  27.  
  28.         // Enable touch support
  29.         if (createjs.Touch.isSupported()) { createjs.Touch.enable(stage); }
  30.  
  31.         displayLabel();
  32.         displaytemplate();
  33.     }
  34.  
  35.  
  36.  
  37.     /**
  38.     * Display Uplaoded Picture
  39.     */
  40.     displayLabel = function () {
  41.  
  42.         var image = new Image();
  43.  
  44.  
  45.         // Invoked when the picture is loaded
  46.         image.onload = function (event) {
  47.  
  48.             // Display Uplaoded Picture
  49.             var upload = new createjs.Bitmap(event.target);
  50.  
  51.             upload.y = 50;
  52.             upload.x = 170;
  53.             upload.height = 50;
  54.             upload.width = 50;
  55.             stage.addChild(upload);
  56.             stage.update();
  57.  
  58.             // Enable drag'n'drop on Picture
  59.             enableDrag(upload)
  60.  
  61.         }
  62.         image.src = "images/sample.jpg";
  63.  
  64.     }
  65.  
  66.     /**
  67.     * Display Monalisa
  68.     */
  69.     displaytemplate = function () {
  70.  
  71.         var image = new Image();
  72.  
  73.         // Invoked when the picture is loaded
  74.         image.onload = function (event) {
  75.  
  76.             // Display template
  77.             var template = new createjs.Bitmap(event.target);
  78.             stage.addChild(template);
  79.             stage.update();
  80.  
  81.         }
  82.         image.src = "images/2.png";
  83.     }
  84.  
  85.  
  86.  
  87.  
  88.  
  89.     /**
  90.     * Enable drag'n'drop on DisplayObjects
  91.     */
  92.     enableDrag = function (item) {
  93.  
  94.         // OnPress event handler
  95.         item.onPress = function (evt) {
  96.  
  97.             var offset = { x: item.x - evt.stageX,
  98.                 y: item.y - evt.stageY
  99.             };
  100.  
  101.             // Bring to front
  102.  
  103.  
  104.             // Mouse Move event handler
  105.             evt.onMouseMove = function (ev) {
  106.  
  107.                 item.x = ev.stageX + offset.x;
  108.                 item.y = ev.stageY + offset.y;
  109.                 stage.update();
  110.             }
  111.         }
  112.     }
  113.  
  114.  
  115.  
  116.     /**
  117.     * Export and display the canvas as PNG in a new wind    ow
  118.     */
  119.     function exportAndView() {
  120.  
  121.         var screenshot = Canvas2Image.saveAsPNG(canvas, true);
  122.         var win = window.open();
  123.         $(win.document.body).html(screenshot);
  124.     }
  125.  
  126.     function download() {
  127.  
  128.         var screenshot = Canvas2Image.saveAsPNG(canvas, true);
  129.         window.location = canvas.toDataURL("image/png")
  130.     }
  131.  
  132.     function to_image() {
  133.         Canvas2Image.saveAsPNG(canvas);
  134.     }
  135.  
  136. </script>
  137.  
  138. <!-- --------------------------------------------------------------------------------End of image script-->
  139.  
  140.  
  141.  
  142.    
  143. <body onload="init()"  >
  144.  
  145.    
  146.     <canvas  width="800" height="308" id="canvas">
  147.     </canvas>
  148.  
  149.     <div style="position: relative" >
  150.         <input type="button" value="View screenshot in a new page" onClick="exportAndView()" >
  151.         <input type="button" value="View screenshot in a new page" onClick="download()" >
  152.         <button onclick="to_image()">Draw to Image</button>
  153.     </div>
  154.    
  155. </body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement