国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

原生JS實(shí)現(xiàn)圖片裁剪功能

這篇具有很好參考價(jià)值的文章主要介紹了原生JS實(shí)現(xiàn)圖片裁剪功能。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

功能介紹:圖片通過原生input上傳,使用canvas進(jìn)行圖片裁剪。 裁剪框限制不允許超出圖片范圍,圖片限制了最大寬高(自行修改要的尺寸),點(diǎn)擊確認(rèn)獲取新的base64圖片數(shù)據(jù)

注:fixed布局不適用該方案,若是fixed布局請(qǐng)查看另一篇文章
效果圖:
原生JS實(shí)現(xiàn)圖片裁剪功能
上代碼文章來源地址http://www.zghlxwxcb.cn/news/detail-507263.html

<!DOCTYPE HTML>
<html>

<head lang="en">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>圖片裁剪</title>
</head>

<body>
  <input id="npt" type="file">
  <div id="box">
    <img style="position:absolute;top:0px;left:0px;opacity: 0.3;" src="" id="img1" />
    <img style="position:absolute;top:0px;left:0px;clip: rect(50px, 250px, 250px, 50px);" src="" id="img2" />
    <!--第三層需用絕對(duì)定位浮在上面-->
    <div id="dragDiv" style="position: absolute;width: 200px;height: 200px;border: 1px solid #fff;top:50px;left:50px;">
      <div class="Divmin up-left"></div>
      <div class="Divmin up"></div>
      <div class="Divmin up-right"></div>
      <div class="Divmin right"></div>
      <div class="Divmin right-down"></div>
      <div class="Divmin down"></div>
      <div class="Divmin left-down"></div>
      <div class="Divmin left"></div>
      <div class="Divmin-btn" style="right: 68px;background-color: #2d87f5;" id="confirmBtn">確定</div>
      <div class="Divmin-btn" style="right: 0px;background-color: #f5a52d;">取消</div>
    </div>
    <div style="position: absolute; right: 0;">
      <img src="" id="later" alt="">
    </div>

  </div>

</body>

</html>

<style>
  body {}

  #box {
    width: 1200px;
    height: 600px;
    background: #333;
    position: absolute;
    top: 50px;
    left: 50px;
  }

  .Divmin-btn {
    bottom: -40px;
    width: 60px;
    height: 30px;
    line-height: 30px;
    color: white;
    font-size: 12px;
    text-align: center;
    display: inline-block;
    position: absolute;
    border-radius: 3px 3px 3px 3px;
  }

  .Divmin-btn:hover {
    background-color: rgba(60, 103, 222, 0.6);
    color: #efeeee;
  }

  .Divmin-btn:active {
    background-color: rgba(69, 94, 167, 0.6);
    color: #efeeee;
  }

  .Divmin {
    position: absolute;
    width: 8px;
    height: 8px;
    background: #fff;
  }

  .up-left {
    margin-top: -4px;
    margin-left: -4px;
    cursor: nw-resize;
  }

  .up {
    left: 50%;
    /*父元素盒子dragDiv寬度的一半,注意要有絕對(duì)定位*/
    margin-left: -4px;
    top: -4px;
    cursor: n-resize;
  }

  .up-right {
    top: -4px;
    right: -4px;
    cursor: ne-resize;
  }

  .right {
    top: 50%;
    margin-top: -4px;
    right: -4px;
    cursor: e-resize;
  }

  .right-down {
    right: -4px;
    bottom: -4px;
    cursor: se-resize;
  }

  .down {
    bottom: -4px;
    left: 50%;
    margin-left: -4px;
    cursor: s-resize;
  }

  .left-down {
    left: -4px;
    bottom: -4px;
    cursor: sw-resize;
  }

  .left {
    left: -4px;
    top: 50%;
    margin-top: -4px;
    cursor: w-resize;
  }

  #img1,
  #img2 {
    max-width: 600px;
    max-height: 300px;
  }
</style>

<script type="text/javascript">
  //禁止圖片被選中
  document.onselectstart = new Function('event.returnValue = false;');
  let confirmBtn = document.getElementById('confirmBtn')
  confirmBtn.addEventListener('click', () => {
    drawRect();
  })


  // 獲取圖片base64數(shù)據(jù)
  let npt = document.getElementById("npt");
  npt.onchange = function () {
    let reader = new FileReader();
    reader.readAsDataURL(npt.files[0]);
    reader.onloadend = function (e) {
      img1.src = e.target.result;
      img2.src = e.target.result;
      // console.log(e.target.result);// 圖片的base64數(shù)據(jù)
      getImage(e.target.result)
    };
  }

  let canvas = document.createElement("canvas");
  let ctx = canvas.getContext('2d');

  // 創(chuàng)建圖片
  let getImage = function (b64) {
    // 創(chuàng)建圖片對(duì)象
    let image = new Image();
    image.src = `${b64}`;
    image.onload = function () {
      // 獲取原圖寬高
      let height = img1.offsetHeight;
      let width = img1.offsetWidth;
      //設(shè)置canvas大小與原圖寬高一致
      canvas.height = height;
      canvas.width = width;
      // 在canvas繪制圖片
      ctx.drawImage(this, 0, 0, width, height);
      // 截圖:
      // drawRect();

      // 圖片上傳后設(shè)置裁剪框與圖片大小一致
      dragDiv.style.height = img1.offsetHeight + 'px'
      dragDiv.style.width = img1.offsetWidth + 'px'
      dragDiv.style.top = 0 + 'px';
      dragDiv.style.left = 0 + 'px';
      setChoice();
    }
  };

  // 繪制截圖矩陣
  let drawRect = function () {
    let top = dragDiv.offsetTop;
    let right = dragDiv.offsetLeft + dragDiv.offsetWidth;
    let bottom = dragDiv.offsetTop + dragDiv.offsetHeight;
    let left = dragDiv.offsetLeft;

    // 截圖寬度
    let w = right - left;
    // 截圖高度
    let h = bottom - top;
    // 獲取截圖區(qū)域內(nèi)容,截圖區(qū)域的像素點(diǎn)矩陣
    let cutImage = ctx.getImageData(left, top, w, h);
    // 裁剪后的base64數(shù)據(jù)
    let newImage = createNewCanvas(cutImage, w, h);
    later.src = newImage;
    // console.log(newImage);// 裁剪后的base64數(shù)據(jù)
  };

  var createNewCanvas = function (content, width, height) {
    var nCanvas = document.createElement('canvas');
    var nCtx = nCanvas.getContext('2d');
    nCanvas.width = width;
    nCanvas.height = height;
    nCtx.putImageData(content, 0, 0);// 將畫布上指定矩形的像素?cái)?shù)據(jù),通過 putImageData() 方法將圖像數(shù)據(jù)放回畫布
    return nCanvas.toDataURL('image/png');
  }

  //獲取id的函數(shù)
  function $(id) {
    if (id.indexOf(".") == 0) {
      let className = id.substring(1, id.length);
      let els = document.getElementsByClassName(className);
      return els[0];
    }
    return document.getElementById(id);
  }

  //獲取元素相對(duì)于屏幕左邊及上邊的距離,利用offsetLeft
  function getPosition(el) {
    let left = el.offsetLeft;
    let top = el.offsetTop;
    let parent = el.offsetParent;
    while (parent != null) {
      left += parent.offsetLeft;
      top += parent.offsetTop;
      parent = parent.offsetParent;
    }
    return { "left": left, "top": top };
  }

  let dragDiv = $('dragDiv');
  let box = $('box')
  let img1 = $('img1')
  let rightDiv = $('.right');
  let isDraging = false;
  let contact = "";//表示被按下的觸點(diǎn)
  //鼠標(biāo)按下時(shí)
  $('.right').onmousedown = function () {
    isDraging = true;
    contact = "right";
  }
  $('.left').onmousedown = function () {
    isDraging = true;
    contact = "left";
  }
  $('.down').onmousedown = function () {
    isDraging = true;
    contact = "down";
  }
  $('.up').onmousedown = function () {
    isDraging = true;
    contact = "up";
  }
  $('.up-right').onmousedown = function () {
    isDraging = true;
    contact = "up-right";
  }
  $('.right-down').onmousedown = function () {
    isDraging = true;
    contact = "down-right";
  }
  $('.up-left').onmousedown = function () {
    isDraging = true;
    contact = "up-left";
  }
  $('.left-down').onmousedown = function () {
    isDraging = true;
    contact = "down-left";
  }

  //鼠標(biāo)松開時(shí)
  window.onmouseup = function () {
    isDraging = false;
  }

  //鼠標(biāo)移動(dòng)時(shí)
  window.onmousemove = function (e) {
    var e = e || window.event;
    if (isDraging == true) {
      switch (contact) {
        case "up":
          upMove(e);
          break;
        case "right":
          rightMove(e);
          break;
        case "down":
          downMove(e);
          break;
        case "left":
          leftMove(e);
          break;
        case "up-right":
          upMove(e);
          rightMove(e);
          break;
        case "down-right":
          downMove(e);
          rightMove(e);
          break;
        case "down-left":
          downMove(e);
          leftMove(e);
          break;
        case "up-left":
          upMove(e);
          leftMove(e);
          break;
      }
    }
  }

  //up移動(dòng)
  function upMove(e) {
    let y = e.clientY;//鼠標(biāo)位置的縱坐標(biāo)
    let heightBefore = dragDiv.offsetHeight - 2;//選取框變化前的高度
    let addHeight = getPosition(dragDiv).top - y;//增加的高度
    let height = heightBefore + addHeight
    let top = dragDiv.offsetTop - addHeight
    if (top <= 1 || height <= 1) return
    dragDiv.style.height = height + 'px';//選取框變化后的寬度
    dragDiv.style.top = top + 'px';//相當(dāng)于變化后左上角的縱坐標(biāo),鼠標(biāo)向上移縱坐標(biāo)減小,下移增大
    setChoice();
  }

  //right移動(dòng)
  function rightMove(e) {
    let allWidth = img1.offsetWidth + box.offsetLeft
    let x = e.clientX;//鼠標(biāo)位置的橫坐標(biāo)
    let widthBefore = dragDiv.offsetWidth - 2;//選取框變化前的寬度
    //let widthBefore = dragDiv.clientWidth;
    if (x >= allWidth) return
    let addWidth = x - getPosition(dragDiv).left - widthBefore;//鼠標(biāo)移動(dòng)后選取框增加的寬度
    dragDiv.style.width = widthBefore + addWidth + 'px';//選取框變化后的寬度
    setChoice();
  }

  //down移動(dòng)
  function downMove(e) {
    let heightBefore = dragDiv.offsetHeight - 2;
    let bottom = box.offsetTop + img1.offsetHeight
    let addHeight = e.clientY - getPosition(dragDiv).top - dragDiv.offsetHeight;
    if (e.clientY >= bottom) return
    let height = heightBefore + addHeight
    dragDiv.style.height = heightBefore + addHeight + 'px';
    setChoice();

  }

  //left移動(dòng)
  function leftMove(e) {
    let widthBefore = dragDiv.offsetWidth - 2;
    let addWidth = getPosition(dragDiv).left - e.clientX;//增加的寬度等于距離屏幕左邊的距離減去鼠標(biāo)位置橫坐標(biāo)
    let width = widthBefore + addWidth
    let left = dragDiv.offsetLeft - addWidth

    if (left <= 1 || width <= 1) return
    dragDiv.style.width = width + 'px';
    dragDiv.style.left = left + 'px';//左邊的距離(相當(dāng)于左邊位置橫坐標(biāo))等于選取框距父級(jí)元素的距離減去增加的寬度
    setChoice();
  }

  //設(shè)置選取框圖片區(qū)域明亮顯示
  function setChoice() {
    let top = dragDiv.offsetTop;
    let right = dragDiv.offsetLeft + dragDiv.offsetWidth;
    let bottom = dragDiv.offsetTop + dragDiv.offsetHeight;
    let left = dragDiv.offsetLeft;
    $('img2').style.clip = "rect(" + top + "px," + right + "px," + bottom + "px," + left + "px)";
  }
</script>

到了這里,關(guān)于原生JS實(shí)現(xiàn)圖片裁剪功能的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 微信小程序---圖片裁剪、旋轉(zhuǎn)、預(yù)覽、上傳功能實(shí)現(xiàn)(已經(jīng)封裝成組件,需要的到資源下載)

    微信小程序---圖片裁剪、旋轉(zhuǎn)、預(yù)覽、上傳功能實(shí)現(xiàn)(已經(jīng)封裝成組件,需要的到資源下載)

    1、可以拍攝或選擇本地圖片上傳圖片數(shù)據(jù) 2、圖片上傳數(shù)據(jù)可以進(jìn)行裁剪、選擇、取消、裁剪后預(yù)覽、上傳以及限制大小,還可以縮放操作,需要的可以解除限制即可 1、點(diǎn)擊圖片上傳按鈕時(shí),跳轉(zhuǎn)頁面到cropper進(jìn)行圖片選擇剪切 wx.navigateTo({ ??????url:?`/pages/cropper/cropper?d

    2023年04月26日
    瀏覽(97)
  • 微信原生實(shí)現(xiàn)一個(gè)簡(jiǎn)易的圖片上傳功能

    微信原生實(shí)現(xiàn)一個(gè)簡(jiǎn)易的圖片上傳功能

    wx.showActionSheet():顯示操作菜單,選擇是從相冊(cè)選擇還是相機(jī)拍攝照片 wx.chooseImage():從本地相冊(cè)選擇圖片或使用相機(jī)拍照。 wx.uploadFile():將本地資源上傳到服務(wù)器。客戶端發(fā)起一個(gè) HTTPS POST 請(qǐng)求,其中 content-type 為 multipart/form-data。 wx.previewMedia(): 預(yù)覽圖片和視頻。 upload.wx

    2024年02月16日
    瀏覽(16)
  • fabric.js 組件 圖片上傳裁剪并進(jìn)行自定義區(qū)域標(biāo)記

    fabric.js 組件 圖片上傳裁剪并進(jìn)行自定義區(qū)域標(biāo)記

    目錄 0. 前言 1. 安裝fabric與引入 2. fabric組件的使用 3. 屬性相關(guān)設(shè)置 4. 初始化加載 4. 方法 5. 全代碼 利用fabric組件,實(shí)現(xiàn)圖片上傳、圖片”裁剪“、自定義的區(qū)域標(biāo)記一系列操作 先放一張效果圖吧?? 我用的是全局引入方式,視情況調(diào)整? 先放一個(gè)fabric.js API地址?Api | Fabric中

    2024年01月22日
    瀏覽(43)
  • 原生JS:100行js代碼帶你實(shí)現(xiàn)【像素鳥】小游戲(完整代碼+素材圖片)

    原生JS:100行js代碼帶你實(shí)現(xiàn)【像素鳥】小游戲(完整代碼+素材圖片)

    JS:經(jīng)典小游戲:像素鳥 JS:經(jīng)典小游戲:貪吃蛇 JS:經(jīng)典小游戲:掃雷 目錄 系列文章目錄 像素鳥 1.游戲介紹 2.代碼分析 3.代碼實(shí)現(xiàn) 3.1 隨機(jī)生成水管 3.2?當(dāng)水管超過游戲顯示區(qū)域時(shí),刪除這一對(duì)水管 3.3 讓小鳥不斷下降 3.4 通過鼠標(biāo)點(diǎn)擊事件來讓小鳥上升 3.5 檢測(cè)小鳥是否撞

    2024年02月08日
    瀏覽(133)
  • 微信小程序?qū)崿F(xiàn)圖片多點(diǎn)裁剪

    話不多說,直接上代碼 1、頁面布局 2、頁面樣式 3、頁面邏輯

    2024年02月13日
    瀏覽(26)
  • 已實(shí)現(xiàn):JS如何根據(jù)視頻的http(s)地址,來截取幀圖片,并實(shí)現(xiàn)大圖壓縮的功能

    已實(shí)現(xiàn):JS如何根據(jù)視頻的http(s)地址,來截取幀圖片,并實(shí)現(xiàn)大圖壓縮的功能

    現(xiàn)在,我們已經(jīng)有了視頻的http地址,我們?cè)趺唇厝瑘D片呢?我以Vue為基礎(chǔ)架構(gòu),來寫寫代碼。 1、先寫布局,先得有video,然后得有canvas 界面上很簡(jiǎn)單,就一個(gè)視頻容器,一個(gè)畫布canvas還不讓它顯示,還有一個(gè)就是截取幀圖片了。 接下來是js關(guān)鍵方法部分: 以上的代碼就是

    2024年01月17日
    瀏覽(21)
  • 【python腳本】python實(shí)現(xiàn):目標(biāo)檢測(cè)裁剪圖片樣本,根據(jù)類標(biāo)簽文件進(jìn)行裁剪保存

    我在進(jìn)行目標(biāo)檢測(cè)時(shí)候,比如紅綠燈檢測(cè),目標(biāo)區(qū)域很小,樣本雜亂。 想要篩選錯(cuò)誤樣本的話,很困難??梢园涯繕?biāo)區(qū)域裁剪出來。人大腦處理對(duì)于這樣的異己比較敏感。樣本量較少的話可以自己篩一篩。樣本量較大的話,可以訓(xùn)練一個(gè)分類模型幫你篩一下。 它就可以實(shí)現(xiàn)

    2024年02月15日
    瀏覽(17)
  • 微信小程序?qū)ι蟼鲌D片進(jìn)行裁剪實(shí)現(xiàn)記錄

    微信小程序?qū)ι蟼鲌D片進(jìn)行裁剪實(shí)現(xiàn)記錄

    媒體 / 圖片 / wx.cropImage (qq.com) 小程序圖片裁剪插件 image-cropper | 微信開放社區(qū) (qq.com) 1、將插件項(xiàng)目中image-cropper文件內(nèi)容復(fù)制到本地項(xiàng)目中的compoent中 wxml: js: ?json: wxss:根據(jù)自己需求調(diào)整 ? ?2、然后在要引用插件的頁面json文件中添加image-cropper 3、在引用插件的wxml文件中引

    2024年04月16日
    瀏覽(92)
  • latex如何對(duì).pdf格式的圖片實(shí)現(xiàn)裁剪

    latex如何對(duì).pdf格式的圖片實(shí)現(xiàn)裁剪

    目錄 問題描述: 問題解決: 在使用draw.io進(jìn)行繪圖,導(dǎo)出的時(shí)候不知道為什么周圍會(huì)有留白,比如下圖: 在導(dǎo)入latex的時(shí)候,會(huì)因?yàn)閮蓚?cè)的留白導(dǎo)致整張圖片縮小。 如果直接進(jìn)行裁剪.pdf,可能需要開通會(huì)員 不得不感慨latex的強(qiáng)大,可以直接使用命令進(jìn)行裁剪。 下面是原始的

    2024年02月07日
    瀏覽(21)
  • aws對(duì)象存儲(chǔ)s3基于lambda實(shí)現(xiàn)圖片裁剪

    存儲(chǔ)桶名稱:wmmzz 1.存儲(chǔ)桶策略設(shè)置 2. 創(chuàng)建lambda函數(shù) 點(diǎn)擊跳轉(zhuǎn)到IAM控制臺(tái),創(chuàng)建自定義角色,選擇服務(wù)類型lambda,創(chuàng)建策略 輸入策略下一步取名resize-policy,回到創(chuàng)建角色頁面,搜索剛才創(chuàng)建的策略選中,再搜索AmazonS3FullAccess選中 點(diǎn)擊下一步,輸入角色名稱resize-role,點(diǎn)擊“創(chuàng)建角色

    2024年02月11日
    瀏覽(24)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包