亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

vue項(xiàng)目中canvas實(shí)現(xiàn)截圖功能

3403771864 / 1090人閱讀

  在vue項(xiàng)目中canvas實(shí)現(xiàn)截圖功能是常用的,下面是具體代碼:

  實(shí)現(xiàn)效果:

  在vue項(xiàng)目中做的一個(gè)截圖功能(只能夠截取圖片),只用鼠標(biāo)就可以在畫面中進(jìn)行框選截取。

  實(shí)現(xiàn):做一個(gè)彈窗,打開彈窗的時(shí)候傳入要截的圖,接下來在這個(gè)窗口里面,點(diǎn)擊截圖按鈕,開始截圖;點(diǎn)擊取消按鈕,取消截圖。

  窗口里面的html主要是三個(gè)部分,一個(gè)是可截圖區(qū)域,一個(gè)是截取圖片的回顯,一個(gè)是操作按鈕(截圖按鈕和取消截圖按鈕)。

  部分html:

  <!--截圖區(qū)域-->
  <div id="clip-img-w" class="img_box">
  <canvas id="drawcanvas"></canvas>
  <canvas id="clipcanvas"></canvas>
  <img id="img_big" :src="imgSrc">
  </div>
  <!--回顯區(qū)域-->
  <div class="img_group_item">
  <img id="img" :src="cutImgSrc">
  </div>
  <!--操作按鈕-->
  <div class="btn_box" align="center">
  <span class="btn_cut" @click="cut()"></span>
  <span v-if="draw" class="btn_cut_cancel" @click="cancelCut()"></span>
  </div>

  用到的data:

  data() {
  return {
  // 儲(chǔ)存截圖區(qū)域的圖片,自己傳
  imgSrc: '',
  // 儲(chǔ)存截圖后的生成的base64圖片
  cutImgSrc: '',
  // 判斷當(dāng)前是否處于截圖狀態(tài)
  draw: false
  };
  },

  截圖:

  cut(){
  var thiz = this;
  thiz.draw = true; //顯示“取消截圖”的按鈕
  var img = document.getElementById("img");
  var wrap = document.getElementById("clip-img-w");
  var width = wrap.offsetWidth;
  var height = wrap.offsetHeight;
  var clipcanvas = document.getElementById("clipcanvas");
  var drawcanvas = document.getElementById("drawcanvas");
  clipcanvas.width = width;
  clipcanvas.height = height;
  drawcanvas.width = width;
  drawcanvas.height = height;
  var clipCtx = drawcanvas.getContext("2d");
  var clipImg = document.createElement("img");
  clipImg.classList.add('img_anonymous');
  clipImg.crossOrigin = "anonymous";
  clipImg.src = thiz.imgSrc;
  var timg = clipImg.cloneNode();
  wrap.appendChild(clipImg);
  clipImg.onload = function(){
  var x = Math.floor((width - this.width)/2);
  var y = Math.floor((height - this.height)/2);
  clipCtx.drawImage(this,0,0,timg.width,timg.height,x,y,this.width,this.height);
  };
  var ctx = clipcanvas.getContext("2d");
  ctx.fillStyle = 'rgba(0,0,0,0.6)';
  ctx.strokeStyle="rgba(0,143,255,1)";
  var start = null;
  var clipArea = {};//裁剪范圍
  clipcanvas.onmousedown = function(e){
  start = {
  x:e.offsetX,
  y:e.offsetY
  };
  };
  clipcanvas.onmousemove = function(e){
  if(start){
  fill(start.x,start.y,e.offsetX-start.x,e.offsetY-start.y)
  }
  };
  document.addEventListener("mouseup",function(){
  if(start){
  start = null;
  var url = startClip(clipArea);
  img.src= url;
  //生成base64格式的圖
  thiz.cutImgSrc = url;
  }
  });
  function fill(x,y,w,h){
  ctx.clearRect(0,0,width,height);
  ctx.beginPath();
  //遮罩層
  ctx.globalCompositeOperation = "source-over";
  ctx.fillRect(0,0,width,height);
  //畫框
  ctx.globalCompositeOperation = 'destination-out';
  ctx.fillRect(x,y,w,h);
  //描邊
  ctx.globalCompositeOperation = "source-over";
  ctx.moveTo(x,y);
  ctx.lineTo(x+w,y);
  ctx.lineTo(x+w,y+h);
  ctx.lineTo(x,y+h);
  ctx.lineTo(x,y);
  ctx.stroke();
  ctx.closePath();
  clipArea = {
  x,
  y,
  w,
  h
  };
  }
  function startClip(area){
  var canvas = document.createElement("canvas");
  canvas.width = area.w;
  canvas.height = area.h;
  var data = clipCtx.getImageData(area.x,area.y,area.w,area.h);
  var context = canvas.getContext("2d");
  context.putImageData(data,0,0);
  return canvas.toDataURL("image/png",1);
  }
  },

  取消截圖or初始化:

  /**
  * 取消截圖
  */
  cancelCut(){
  this.draw = false;
  this.init();
  },
  /**
  * 打開彈窗的時(shí)候初始化
  */
  init(){
  // canvas清空畫布
  var wrap = document.getElementById("clip-img-w");
  var width = wrap.offsetWidth;
  var height = wrap.offsetHeight;
  var clipcanvas = document.getElementById("clipcanvas");
  var drawcanvas = document.getElementById("drawcanvas");
  clipcanvas.width = width;
  clipcanvas.height = height;
  drawcanvas.width = width;
  drawcanvas.height = height;
  var clipCtx = drawcanvas.getContext("2d");
  var ctx = clipcanvas.getContext("2d");
  clipCtx.clearRect(0,0,drawcanvas.width,drawcanvas.height);
  ctx.clearRect(0,0,clipcanvas.width,clipcanvas.height);
  //移除鼠標(biāo)事件
  clipcanvas.onmousedown = null;
  clipcanvas.onmousemove = null;
  document.removeEventListener("mouseup",fn(),false);
  function fn(){}
  // 移除創(chuàng)建的img節(jié)點(diǎn),避免重復(fù)創(chuàng)建
  if($('.img_anonymous').length>0){
  $('.img_anonymous').remove();
  }
  //避免同一張圖沒有更新
  this.cutImgSrc = this.imgSrc;
  },

  部分CSS樣式:

  //less
  //截圖區(qū)域
  >.img_box{
  width: 700px;
  height: 450px;
  position:relative;
  >canvas{
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
  &#clipcanvas{
  z-index: 2;
  }
  &#drawcanvas{
  z-index: 1;
  }
  }
  //圖片居中顯示
  img{
  display: block;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  max-width: 100%;
  max-height: 100%;
  }
  }
  //回顯區(qū)域
  >.img_group_item{
  width: 250px;
  height: 250px;
  position: relative;
  margin: 0 auto;
  //圖片居中顯示
  img{
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  max-width: 100%;
  max-height: 100%;
  }
  }

上述內(nèi)容介紹vue項(xiàng)目中canvas實(shí)現(xiàn)截圖功能,請(qǐng)大家以后多多關(guān)注。

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/127667.html

相關(guān)文章

  • Vue+canvas實(shí)現(xiàn)視頻截圖功能

      上傳視頻要提供視頻封面(視頻封面必填),這是在開發(fā)中實(shí)際問題。封面可以用戶自己制作并上傳,但這樣脫離網(wǎng)站,體驗(yàn)不好,常見的處理方案就是用戶未選擇或上傳封面時(shí),自動(dòng)截取視頻第一幀作為封面,但這樣并不友好。因此考慮視頻上傳后,在播放中由人員自行截取畫面作為視頻封面?! 『?jiǎn)單效果如圖:  前端代碼如下:  <template>   <div>   <videosrc=&...

    3403771864 評(píng)論0 收藏0
  • 基于vue項(xiàng)目的知識(shí)總結(jié)

    摘要:前言用有一段時(shí)間了,從用搭建項(xiàng)目一步步配置,到之后的研究動(dòng)效這些,一直想寫些東西記錄一下做個(gè)總結(jié),剛好趁著有空就整理一下。結(jié)語有新的知識(shí)點(diǎn)會(huì)更新到知識(shí)體系中,總結(jié)和心得體會(huì)會(huì)單獨(dú)寫文章詳述,努力填坑 前言 用vue有一段時(shí)間了,從用vue-cli搭建項(xiàng)目、一步步配置axios、vuex、vue-router,到之后的研究canvas、動(dòng)效這些,一直想寫些東西記錄一下、做個(gè)總結(jié),剛好趁著...

    tianlai 評(píng)論0 收藏0
  • Vue 開發(fā)H5 微信公眾號(hào)

    摘要:具體參考官方文檔大致流程調(diào)用代碼示例調(diào)起微信支付創(chuàng)建支付返回的簽名信息公眾號(hào)名稱,由商戶傳入時(shí)間戳,自年以來的秒數(shù)隨機(jī)串微信簽名方式微信簽名校驗(yàn)支付支付成功支付失敗二實(shí)現(xiàn)簽名截圖網(wǎng)頁(yè)上傳截圖簽名使用實(shí)現(xiàn),由于基于實(shí)現(xiàn),需要引入。 一、調(diào)起微信支付 在微信瀏覽器里面打開H5網(wǎng)頁(yè)中執(zhí)行JS調(diào)起支付,WeixinJSBridge內(nèi)置對(duì)象在其他瀏覽器中無效。 具體參考官方文檔:https:/...

    kbyyd24 評(píng)論0 收藏0
  • 使用vue完成微信公眾號(hào)網(wǎng)頁(yè)小記

    摘要:前言公司最近有一個(gè)頁(yè)面的功能,比較簡(jiǎn)單的一個(gè)調(diào)查表功能,嵌套在我們微信公眾號(hào)里面。同時(shí)用到了微信的登錄和分享接口。參考鏈接使用微信接口前端部分我們用微信接口主要是做的登錄和分享功能,首先是上微信公眾平臺(tái)上邊看看,把權(quán)限搞好之后后端配置。 showImg(https://segmentfault.com/img/bVbrOkH); 前言: 公司最近有一個(gè)H5頁(yè)面的功能,比較簡(jiǎn)單的一個(gè)調(diào)查...

    phoenixsky 評(píng)論0 收藏0
  • 使用vue完成微信公眾號(hào)網(wǎng)頁(yè)小記

    摘要:前言公司最近有一個(gè)頁(yè)面的功能,比較簡(jiǎn)單的一個(gè)調(diào)查表功能,嵌套在我們微信公眾號(hào)里面。同時(shí)用到了微信的登錄和分享接口。參考鏈接使用微信接口前端部分我們用微信接口主要是做的登錄和分享功能,首先是上微信公眾平臺(tái)上邊看看,把權(quán)限搞好之后后端配置。 showImg(https://segmentfault.com/img/bVbrOkH); 前言: 公司最近有一個(gè)H5頁(yè)面的功能,比較簡(jiǎn)單的一個(gè)調(diào)查...

    notebin 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<