Advertisement
tari

Untitled

May 25th, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 3.94 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>The Gentlemanizer</title>
  5.     <style type="text/css">
  6.         canvas {
  7.             max-width: 100%;
  8.         }
  9.     </style>
  10.     <script type="text/javascript" src="jquery.js"></script>
  11.     <!--
  12.    <script type="text/javascript" src="fileuploader.js"></script>
  13.    <link rel="stylesheet" type="text/css" href="fileuploader.css"></script>
  14.    -->
  15.     <script type="text/javascript">
  16.     $(document).ready(function() {
  17.         $("#uploadButton").click(function(e) {
  18.             $("#fileSelect").click();
  19.         });
  20.  
  21.         var overlay = null, underlay = null;
  22.         var handleOverlay = function(over, under) {
  23.             overlay = overlay == null ? over : overlay;
  24.             underlay = underlay == null ? under : underlay;
  25.             if (overlay != null && underlay != null) {
  26.                // Both ready, so render
  27.                var content = $("#content");
  28.                 content.empty();
  29.                 var canvas = $("<canvas />").attr("width", underlay.width).attr("height", underlay.height);
  30.                 var ctx = canvas.get(0).getContext('2d');
  31.                 ctx.drawImage(underlay, 0, 0);
  32.                 ctx.drawImage(overlay, 0, 0);
  33.                 content.append(canvas);
  34.                 overlay = underlay = null;
  35.                 $("#progress").hide('slow');
  36.             }
  37.         };
  38.  
  39.         $("#fileSelect").change(function(e) {
  40.             $("#progress-remote").html("Uploading");
  41.             $("#progress-local").html("Loading");
  42.             $("#progress").show("slow");
  43.             var file = $("#fileSelect").prop('files')[0];
  44.             var xhr = new XMLHttpRequest();
  45.                                            
  46.             xhr.upload.onprogress = function(e){
  47.                 if (e.lengthComputable){
  48.                     $("#progress-remote").html((e.loaded * 100 / e.total).toFixed(2));
  49.                 }
  50.             };
  51.             xhr.onreadystatechange = function(e){            
  52.                 if (xhr.readyState == 4){
  53.                     var progress = $("#progress-remote");
  54.                     if (xhr.status != 200) {
  55.                         progress.html("Error: " + xhr.statusText);
  56.                     } else {
  57.                         progress.html("Done");
  58.                         var overlay = $("<img />").attr("src", xhr.responseText).get(0);
  59.                         handleOverlay(overlay, null);
  60.                     }
  61.                 }
  62.             };
  63.             xhr.open("POST", '/gentlemanize', true);
  64.             xhr.setRequestHeader("Content-Type", file.type);
  65.             xhr.send(file);
  66.  
  67.             var reader = new FileReader();
  68.             reader.onload = function(e) {
  69.                 var underlay = $("<img />", {
  70.                     src: e.target.result,
  71.                 }).get(0);
  72.                 $("#progress-local").html("Done");
  73.                 handleOverlay(null, underlay);
  74.             };
  75.             reader.readAsDataURL(file);
  76.  
  77.         });
  78.     });
  79.     </script>
  80. </head>
  81. <body>
  82.  
  83. <noscript>
  84.     <p>Scripts appear to be disabled in your browser.  This application
  85.        uses client-side scripting to minimize the amount of data transfer
  86.        required (since images tend to be large), so please consider allowing
  87.        this page to execute scripts.  You can read more about this rationale
  88.        in the
  89.        <a href="http://www.taricorp.net/projects/gentlemanizer/#bandwidth">design notes</a>.</p>
  90.    <p>The <a href="bandwidth-waster.html">alternate version</a> of this
  91.    page doesn't require scripts, but is significantly less polished and
  92.    requires more data transfer.</p>
  93. </noscript>
  94.  
  95. <input type="file" id="fileSelect" accept="image/*" style="display:none"></input>
  96. <div id="uploadButton">
  97.     Click here to upload.
  98.     <div id="progress">
  99.         Remote: <div id="progress-remote"></div>
  100.         Local: <div id="progress-local"></div>
  101.     </div>
  102. </div>
  103.  
  104. <div id="content">
  105. </div>
  106.  
  107. </body>
  108. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement