国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

原生js基于canvas實現(xiàn)一個簡單的前端截圖工具代碼實例

2019-11-19 10:50:45
字體:
來源:轉載
供稿:網(wǎng)友

這篇文章主要介紹了原生js基于canvas實現(xiàn)一個簡單的前端截圖工具代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

先看效果

代碼如下

<!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>         <!-- 結果 -->    <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>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 金门县| 咸宁市| 界首市| 五原县| 陇南市| 霍山县| 安仁县| 浑源县| 安福县| 永靖县| 霍州市| 东乌珠穆沁旗| 苗栗县| 霍城县| 乌苏市| 汨罗市| 定南县| 饶河县| 泾川县| 邹城市| 万安县| 黄冈市| 金川县| 江山市| 西充县| 沧源| 肥西县| 伊金霍洛旗| 明星| 稷山县| 武乡县| 吴堡县| 保山市| 四会市| 定南县| 珠海市| 呈贡县| 彭阳县| 保定市| 河间市| 蕉岭县|