【问题标题】:How to fit image in canvas using cropperJS如何使用cropperJS将图像放入画布中
【发布时间】:2020-09-14 10:02:27
【问题描述】:

我是使用cropperjs 库的新手。但我正在苦苦思考如何配置它。

我有什么
https://jsfiddle.net/kaeqxfjL/

我需要什么
我想集成一个完整的图像裁剪,您只需将照片拖到画布内,而不是使用正方形来裁剪照片。我还需要使照片的宽度适合画布的宽度,这样你就不能向左/向右拖动它;你只能向上/向下拖动它。

var canvas  = $("#canvas"),
    context = canvas.get(0).getContext("2d"),
    $result = $('#result');

$('#fileInput').on( 'change', function(){
    if (this.files && this.files[0]) {
      if ( this.files[0].type.match(/^image\//) ) {
        var reader = new FileReader();
        reader.onload = function(evt) {
           var img = new Image();
           img.onload = function() {
             context.canvas.height = img.height;
             context.canvas.width  = img.width;
             context.drawImage(img, 0, 0);
             var cropper = canvas.cropper({
               aspectRatio: 16 / 9,
               dragMode: 'move'
             });
             
             $('#btnCrop').click(function() {
                // Get a string base 64 data url
                var croppedImageDataURL = canvas.cropper('getCroppedCanvas').toDataURL("image/png"); 
                $result.append( $('<img>').attr('src', croppedImageDataURL) );
             });
             $('#btnRestore').click(function() {
               canvas.cropper('reset');
               $result.empty();
             });
           };
           img.src = evt.target.result;
                };
        reader.readAsDataURL(this.files[0]);
      }
      else {
        alert("Invalid file type! Please select an image file.");
      }
    }
    else {
      alert('No file(s) selected.');
    }
});
/* Limit image width to avoid overflow the container */
img {
  max-width: 100%; /* This rule is very important, please do not ignore this! */
}

#canvas {
  height: 290px;
  width: 650px;
  background-color: #ffffff;
  cursor: default;
  border: 1px solid black;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/cropper/2.3.3/cropper.css" rel="stylesheet"/>





<p>
<!-- Below are a series of inputs which allow file selection and interaction with the cropper api -->
        <input type="file" id="fileInput" accept="image/*" />
        <input type="button" id="btnCrop" value="Crop" />
        <input type="button" id="btnRestore" value="Restore" />
</p>
<div>
  <canvas id="canvas">
    Your browser does not support the HTML5 canvas element.
  </canvas>
</div>      

<div id="result"></div>
        




<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/cropper/2.3.3/cropper.js"></script>

【问题讨论】:

    标签: javascript html jquery css cropperjs


    【解决方案1】:

    使用类设置图像宽度:

    .img-container img {
          width: 100%;
        }
    

    并设置viewModeaspectRatio如下。

    var cropper = canvas.cropper({
                    aspectRatio: 16 / 9,
                    dragMode: 'move',
                    viewMode: 3,
                    aspectRatio: 1
                  });
    

    var canvas = $("#canvas"),
      context = canvas.get(0).getContext("2d"),
      $result = $('#result');
    
    $('#fileInput').on('change', function() {
      if (this.files && this.files[0]) {
        if (this.files[0].type.match(/^image\//)) {
          var reader = new FileReader();
          reader.onload = function(evt) {
            var img = new Image();
            img.onload = function() {
              context.canvas.height = img.height;
              context.canvas.width = img.width;
              context.drawImage(img, 0, 0);
              var cropper = canvas.cropper({
                aspectRatio: 16 / 9,
                dragMode: 'move',
                viewMode: 3,
                aspectRatio: 1
    
              });
    
              $('#btnCrop').click(function() {
                // Get a string base 64 data url
                var croppedImageDataURL = canvas.cropper('getCroppedCanvas').toDataURL("image/png");
                $result.append($('<img>').attr('src', croppedImageDataURL));
              });
              $('#btnRestore').click(function() {
                canvas.cropper('reset');
                $result.empty();
              });
            };
            img.src = evt.target.result;
          };
          reader.readAsDataURL(this.files[0]);
        } else {
          alert("Invalid file type! Please select an image file.");
        }
      } else {
        alert('No file(s) selected.');
      }
    });
    /* Limit image width to avoid overflow the container */
    
    img {
      max-width: 100%;
      /* This rule is very important, please do not ignore this! */
    }
    
    #canvas {
      height: 290px;
      width: 650px;
      background-color: #ffffff;
      cursor: default;
      border: 1px solid black;
    }
    
    .img-container img {
      width: 100%;
    }
    <link href="https://cdnjs.cloudflare.com/ajax/libs/cropper/2.3.3/cropper.css" rel="stylesheet" />
    
    
    
    
    
    <p>
      <!-- Below are a series of inputs which allow file selection and interaction with the cropper api -->
      <input type="file" id="fileInput" accept="image/*" />
      <input type="button" id="btnCrop" value="Crop" />
      <input type="button" id="btnRestore" value="Restore" />
    </p>
    <div>
      <canvas id="canvas" class="img-container">
        Your browser does not support the HTML5 canvas element.
      </canvas>
    </div>
    
    <div id="result"></div>
    
    
    
    
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/cropper/2.3.3/cropper.js"></script>

    【讨论】:

    • 裁剪框还在。我想删除它并裁剪画布内的内容
    • 在参数中指定两次 aspectRatio 有什么影响?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    • 1970-01-01
    • 2023-03-21
    相关资源
    最近更新 更多