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

首頁(yè) > 網(wǎng)站 > WEB開(kāi)發(fā) > 正文

canvas-圖像截取

2024-04-27 15:08:17
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
<!DOCTYPE html><html><head lang="en">    <meta charset="UTF-8">    <title>canvas-圖像截取</title>    <style>        canvas{            border: 1px dashed red;            float: left;            position: relative;        }    </style>    </head><body>    <div id="cantox" style="position: relative">        <canvas id="oldcanvas" width="500px" height="300px">        </canvas>        <div id="cliptox" style="position: absolute;display:none"></div>    </div>    <button id ="btnclip" style="float: left">截取該區(qū)域</button>    <canvas id="nowcanvas" width="500px" height="300px" >    </canvas>    <script>        //獲取div元素        var cantox = document.getElementById("cantox");        var cliptox = document.getElementById("cliptox");        var btnclip = document.getElementById("btnclip");        //獲取到canvas元素        var oldcanvas = document.getElementById("oldcanvas");        var nowcanvas = document.getElementById("nowcanvas");        //獲取canvas中的畫(huà)圖環(huán)境        var oldcontext = oldcanvas.getContext('2d');        var nowcontext = nowcanvas.getContext('2d');        var img = new Image();        img.src = "./image/liuyifei.jpg";        //加載圖像到canvas畫(huà)布中        window.onload = function (){            oldcontext.drawImage(img,0,0,oldcanvas.width,oldcanvas.height);        }        var startPoint;//截取圖像開(kāi)始坐標(biāo)        var endPoint;//截圖圖像的結(jié)束坐標(biāo)        var w; //截取圖像的寬度        var h; //截取圖像的高度        var flag = false; //用于判斷移動(dòng)事件事物控制        //鼠標(biāo)按下事件        cantox.onmousedown = function (e){            flag = true;            cliptox.style.display = 'block';            startPoint = windowToCanvas(oldcanvas, e.clientX, e.clientY);            cliptox.style.left = startPoint.x+'px';            cliptox.style.top = startPoint.y+'px';        }        //鼠標(biāo)移動(dòng)事件        cantox.onmousemove = function (e){            if(flag){                cliptox.style.background = 'rgba(0,0,0,0.5)';                endPoint = windowToCanvas(oldcanvas, e.clientX, e.clientY);                w = endPoint.x - startPoint.x;                h = endPoint.y - startPoint.y;                cliptox.style.width = w +'px';                cliptox.style.height = h+'px';            }        }        //鼠標(biāo)釋放事件        cantox.onmouseup = function (e){            flag = false;        }        //按鈕截取事件        btnclip.onclick = function (){            imgCut(nowcontext,img,oldcanvas.width,oldcanvas.height,startPoint.x,startPoint.y,w,h);        }        /*        * 圖像截取函數(shù)        * context:繪制環(huán)境對(duì)象        * image:圖像對(duì)象        * imgElementW:圖像顯示的寬度        * imgElementH:圖像顯示的高度        * sx:截取圖像的開(kāi)始X坐標(biāo)        * sy:截取圖像的開(kāi)始Y坐標(biāo)        * w:截取圖像的寬度        * h:截取圖像的高度        * */        function imgCut(context,image,imgElementW,imgElementH,sx,sy,w,h){            //清理畫(huà)布,便于重新繪制            context.clearRect(0,0,imgElementW,imgElementH);            //計(jì)算 :比例 = 原圖像/顯示圖像            var ratioW = image.width/imgElementW;            var ratioH = image.height/imgElementH;            //根據(jù)截取圖像的所占位置及大小計(jì)算出在原圖所占的位置及大小            //.drawImage(圖像對(duì)象,原圖像截取的起始X坐標(biāo),原圖像截取的起始Y坐標(biāo),原圖像截取的寬度,原圖像截取的高度,            // 繪制圖像的起始X坐標(biāo),繪制圖像的起始Y坐標(biāo),繪制圖像所需要的寬度,繪制圖像所需要的高度);            context.drawImage(image,ratioW*sx,ratioH*sy,ratioW*w,ratioH*h,0,0,w,h);        }        /*         * 坐標(biāo)轉(zhuǎn)換:將window中的坐標(biāo)轉(zhuǎn)換到元素盒子中的坐標(biāo),并返回(x,y)坐標(biāo)         * element:canvas元素對(duì)象         * x:鼠標(biāo)在當(dāng)前窗口X坐標(biāo)值         * y:鼠標(biāo)在當(dāng)前窗口Y坐標(biāo)值         * */        function windowToCanvas(element,x,y){            //獲取當(dāng)前鼠標(biāo)在window中的坐標(biāo)值            // alert(event.clientX+"-------"+event.clientY);            //獲取元素的坐標(biāo)屬性            var box = element.getBoundingClientRect();            var bx = x - box.left;            var by = y - box.top;            return {x:bx,y:by};        }    </script></body></html>
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 泗洪县| 崇左市| 如皋市| 钦州市| 拉孜县| 余江县| 古交市| 桦甸市| 荥经县| 仙桃市| 安新县| 兴城市| 江门市| 丹巴县| 尉氏县| 洛阳市| 论坛| 邵东县| 苏尼特右旗| 阿城市| 邻水| 集安市| 封丘县| 漾濞| 方城县| 新丰县| 山丹县| 卓资县| 如皋市| 赣榆县| 丽水市| 横峰县| 石首市| 白银市| 阿拉善右旗| 呼图壁县| 玛沁县| 灵山县| 岚皋县| 秦安县| 精河县|