Advertisement
asimryu

canvas flip flop example

Jul 29th, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.75 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Canvas Flip &amp; Flop Example</title>
  6.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  7.     <style>
  8.         #mycanvas {
  9.             border: 1px solid #888;
  10.         }
  11.         #imgdrop {
  12.             width: 500px;
  13.             height: 200px;
  14.             background: #FF0;
  15.             border: 1px solid #888;
  16.             text-align: center;
  17.             line-height: 200px;
  18.             font-size: 25px;
  19.             font-weight: bold;
  20.         }
  21.         div {
  22.             margin: 20px;
  23.         }
  24.     </style>
  25. </head>
  26. <body>
  27.         <div>
  28.             <button onclick="flip()" class="btn btn-default"><span class="glyphicon glyphicon-resize-horizontal"></span> flip</button>
  29.             <button onclick="flop()" class="btn btn-default"><span class="glyphicon glyphicon-resize-vertical"></span> flop</button>
  30.         </div>
  31.         <div><canvas id="mycanvas" width="500" height="300"></canvas></div>
  32.         <div id="imgdrop" ondrop="drop(event);" ondragover="dragover(event)" ondragleave="dragleave(event)">Drop Image Here</div>
  33.     <script>
  34.         var canvas = document.getElementById("mycanvas");
  35.         var ctx = canvas.getContext("2d");
  36.         var width = canvas.width;
  37.         var height = canvas.height;
  38.  
  39.         var img = new Image();
  40.  
  41.         function drop(event){
  42.             event.preventDefault();
  43.             var data = event.dataTransfer;
  44.             var files = data.files;
  45.             if( files.length > 0){
  46.                 if( files[0].type == "image/jpeg" || files[i].type == "image/png" || files[0].type == "image/gif" ) {
  47.                     addPhoto(files[0]);
  48.                 }
  49.             }
  50.         }  
  51.  
  52.         function dragover(event){
  53.             event.preventDefault();
  54.         }
  55.  
  56.         function dragleave(event){
  57.             event.preventDefault();
  58.         }
  59.  
  60.     function addPhoto(file){
  61.         var reader = new FileReader();
  62.         reader.addEventListener("load",function(){
  63.             img.src = reader.result;
  64.             ctx.drawImage(img, 0, 0, width, height);
  65.         });
  66.         reader.readAsDataURL(file);
  67.     }          
  68.  
  69.     function flip() {
  70.                 var scaleH = -1;
  71.                 var scaleV = 1;
  72.                 var posX = width * -1;
  73.                 var posY = 0;
  74.                 ctx.save(); //현재 상태 저장
  75.                 ctx.scale(scaleH, scaleV); // 뒤집을 크기
  76.                 ctx.drawImage(img, posX, posY, width, height); // 이미지 그리기
  77.                 ctx.restore(); // 마지막 저장 상태 저장
  78.                 img.src = canvas.toDataURL(); // img의 src에 canvas에 그려진 이미지 집어 넣기
  79.     };
  80.  
  81.     function flop(){
  82.                 var scaleH = 1;
  83.                 var scaleV = -1;
  84.                 var posX =0;
  85.                 var posY = height * -1;
  86.                 ctx.save(); //현재 상태 저장
  87.                 ctx.scale(scaleH, scaleV); // 뒤집을 크기
  88.                 ctx.drawImage(img, posX, posY, width, height); // 이미지 그리기
  89.                 ctx.restore(); // img의 src에 canvas에 그려진 이미지 집어 넣기
  90.                 img.src = canvas.toDataURL();  
  91.     }
  92.     </script>
  93.  
  94. </body>
  95. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement