• 利用canvas图片剪切


    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
    </script>

    <style type="text/css">
    * {
    padding: 0px;
    margin: 0px;
    }

    .main {
    position: relative;
    height: 500px;
    500px;
    border: 1px solid gray;
    overflow: hidden;
    }

    #btnfile {
    }

    .image {
    position: absolute;
    height: 500px;
    500px;
    z-index: 1;
    }

    .cut_container {
    position: absolute;
    border: 1px solid orange;
    height: 150px;
    150px;
    left: 175px;
    top: 175px;
    z-index: 8888;
    }

    .btn_container {
    position: absolute;
    top: 500px;
    }
    </style>
    </head>
    <body>
    <canvas id="mycan" width="200" height="200" style="display:none"></canvas>
    <div class="main" id="img_container">
    <div class="cut_container" id="cut_container" draggable="false"></div>

    <img id="myImg" class="image" src="images/6.jpg" draggable="false" />

    </div>
    <div class="btn_container">
    <input type="file" id="btnfile" />
    <button id="btn_cut" onclick="cut_picture()">确定</button>
    </div>


    </body>
    </html>
    <script type="text/javascript">
    var img = document.getElementById("myImg");
    var width = parseInt(img.width);
    var height = parseInt(img.height);
    var x = 0; var y = 0;
    var cut = $(document.getElementById("cut_container"))
    $("#btnfile").change(function () {
    var fr = new FileReader();
    var file = this.files[0];
    fr.readAsDataURL(file);
    fr.onload = function () {
    console.log(fr.result);
    img.src = fr.result;
    width = img.width;
    height = img.height;
    }

    })
    var x = 0, y = 0;
    var canvas = document.getElementById("mycan");
    var ctx = canvas.getContext("2d");
    img.onload = function () {
    ctx.drawImage(img, 0, 0, width, height);
    }

    document.getElementById("img_container").onmousewheel = function (e) {
    e = event || e;
    var v = e.wheelDelta;
    if (v > 0) {
    width += 10;
    height += 10;
    $(img).css({ height: height, width })
    }
    else {
    width -= 10;
    height -= 10;
    if (width < 150 || height < 150) {
    width = 150;
    height = 150;
    }
    $(img).css({ height: height, width })

    }
    }

    $("#myImg").mousedown(function (e) {
    e = event || e;
    var W = e.offsetX * 1;
    var H = e.offsetY * 1;

    $("#img_container").bind("mousemove", function (ex) {
    ex = event || ex;
    var left = ex.clientX * 1;
    var top = ex.clientY * 1;
    var l = left - W;
    var t = top - H +$(document).scrollTop()*1;

    $("#myImg").css({ left: l, top: t })
    })
    })
    $("#img_container").mouseup(function () {
    $("#img_container").unbind("mousemove")
    })
    function cut_picture() {
    canvas.height = 150;
    canvas.width = 150;
    x = parseInt(img.style.left) - parseInt(cut.css("left"));
    y = parseInt(img.style.top) - parseInt(cut.css("top"));
    ctx.drawImage(img, x, y, width, height);
    console.log(canvas.toDataURL())
    window.open(canvas.toDataURL())
    }

    </script>

  • 相关阅读:
    4月19日 疯狂猜成语-----第五次站立会议 参会人员:杨霏,袁雪,胡潇丹,郭林林,尹亚男,赵静娜
    prototype
    angularJs scope的简单模拟
    angularjs DI简单模拟
    洗牌算法
    深入探索 TCP TIME-WAIT
    Logitech k480 蓝牙键盘连接 ubuntu 系统
    在 centos6 上安装 LAMP
    vlc 播放器的点播和广播服务
    Linux 文件系统及 ext2 文件系统
  • 原文地址:https://www.cnblogs.com/-maomao/p/5276444.html
Copyright © 2020-2023  润新知