欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

怎么在JavaScript中使用canvas實(shí)現(xiàn)一個(gè)前端截圖工具-創(chuàng)新互聯(lián)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)怎么在JavaScript中使用canvas實(shí)現(xiàn)一個(gè)前端截圖工具,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

站在用戶的角度思考問題,與客戶深入溝通,找到阿壩州網(wǎng)站設(shè)計(jì)與阿壩州網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名申請(qǐng)、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋阿壩州地區(qū)。

JavaScript是什么

JavaScript是一種直譯式的腳本語言,其解釋器被稱為JavaScript引擎,是瀏覽器的一部分,JavaScript是被廣泛用于客戶端的腳本語言,最早是在HTML網(wǎng)頁上使用,用來給HTML網(wǎng)頁增加動(dòng)態(tài)功能。

代碼如下

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
    <style type="text/css">
      *{
        padding: 0;
        margin: 0;
      }
      .clip-img-w{
        position: relative;
        width: 100%;
        height: 100%;
        font-size: 0;
      }
      .clip-img-w img{
        max-width: 100%;
        max-height: 100%;
        position: absolute;
        left: 0;
        top: 0;
        right: 0;
        bottom: 0;
        margin: auto;
      }
      .clip-img-w canvas{
        position: absolute;
        left: 0;
        top: 0;
      }
      .clip-img-w #clipcanvas{
        z-index: 2;
      }
      .clip-img-w #drawcanvas{
        background: #fff;
        z-index: 1;
      }       
      #img{
        display: block;
        margin: 0 auto;
      }
      .box-c{
        width: 400px;
        height: 200px;
        border: 1px solid #F35252;
        margin: 20px auto;
      }
    </style>
  </head>
  <body>
    <div class="box-c">
      <div class="clip-img-w" id="clip-img-w">
        <canvas id="drawcanvas" ></canvas>
        <canvas id="clipcanvas" ></canvas>
      </div>
    </div>
     
    <!-- 結(jié)果 -->
    <img src="" id="img">
  </body>
  <script type="text/javascript">
    var img = document.getElementById("img");
    var url = 'http://img.muchengfeng.cn/FvC7i-GkXYoHE7kGFlNfj7xEzvIQ';
     
    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.crossOrigin = "anonymous";
    clipImg.src = url;
    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="green";
    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;
      }
    })     
    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");
    }       
  </script>
</html>

上述就是小編為大家分享的怎么在JavaScript中使用canvas實(shí)現(xiàn)一個(gè)前端截圖工具了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

文章題目:怎么在JavaScript中使用canvas實(shí)現(xiàn)一個(gè)前端截圖工具-創(chuàng)新互聯(lián)
鏈接分享:http://chinadenli.net/article34/ghese.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站導(dǎo)航、網(wǎng)站改版、ChatGPT、軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)