【html】图片多矩形框裁剪

说明

由于项目中需要对一个图片进行多选择框进行裁剪,所以特写当前的示例代码。

代码

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <base href="/">
    <title>图片裁剪</title>
</head>
<body>
<canvas id="myCanvas" style="border:1px solid #d3d3d3;">
    Your browser does not support the HTML5 canvas tag.
</canvas>
<script>
    let canvas = document.getElementById("myCanvas");
    let ctx = canvas.getContext("2d");
    let img = new Image();
    let rate = 1
    img.onload = function () {
        let width = img.width
        let height = img.height
        if (img.width > window.innerWidth || img.height > window.innerHeight) {
            if (window.innerWidth / img.width > window.innerHeight / img.height) {
                rate = window.innerHeight / img.height
                width = img.width * rate
                height = window.innerHeight
            } else {
                width = window.innerWidth
                rate = window.innerWidth / img.width
                height = img.height * rate
            }
        }
        // 等比缩小
        canvas.width = width;
        canvas.height = height;
        ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
    }
    let url = new URL(window.location.href);
    let params = new URLSearchParams(url.search);

    img.src = "https://img1.baidu.com/it/u=1486134966,661096340&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500";

    // 坐标数组
    let coorArr = []
    // 当前坐标
    let coor = {}

    // 鼠标按下
    canvas.onmousedown = function (e) {
        coor.begin = {
            x: e.clientX - canvas.offsetLeft,
            y: e.clientY - canvas.offsetTop
        }
        coor.end = {
            x: 0,
            y: 0
        }
    }

    // 移动鼠标
    canvas.onmousemove = function (e) {
        let begin = coor.begin;
        if (begin === undefined || begin.x === undefined) {
            return
        }
        coor.begin = coor.begin
        coor.end = {
            x: e.clientX - canvas.offsetLeft,
            y: e.clientY - canvas.offsetTop
        }

        draw();
        drawLine(coor);
    }

    // 鼠标放开
    canvas.onmouseup = function (e) {
        let begin = coor.begin;
        if (begin === undefined || begin.x === undefined) {
            return
        }
        coorArr.push({
            begin: coor.begin,
            end: {
                x: e.clientX - canvas.offsetLeft,
                y: e.clientY - canvas.offsetTop
            }
        })

        draw();

        coor.begin = {}
    }

    // 双击裁剪
    canvas.ondblclick = function () {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
        coorArr = []
        coor = {}
    }

    // 鼠标离开则清理
    canvas.onmouseout = function () {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
        coorArr = []
        coor = {}
    }

    // 画框
    function draw() {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        ctx.drawImage(img, 0, 0, canvas.width, canvas.height);

        ctx.beginPath();
        ctx.strokeStyle = 'green';
        ctx.lineWidth = 5;
        ctx.lineCap = 'round';
        ctx.lineJoin = 'round';

        // 先画之前的框
        coorArr.forEach(coor => {
            drawLine(coor);
        });

        // 显示光标位置信息
        ctx.font = "18px Arial";
        ctx.fillStyle = "red";
        // 在canvas外显示光标位置
        ctx.fillText("缩放:" + rate.toFixed(2) + ";说明:鼠标离开画布清理,鼠标双击进行裁剪。", 5, 30);
    }

    // 画矩形
    function drawLine(coor) {
        let begin = coor.begin;
        let end = coor.end;
        // 画矩形
        ctx.moveTo(begin.x, begin.y);
        ctx.lineTo(end.x, begin.y);

        ctx.moveTo(end.x, begin.y);
        ctx.lineTo(end.x, end.y);

        ctx.moveTo(end.x, end.y);
        ctx.lineTo(begin.x, end.y);

        ctx.moveTo(begin.x, end.y);
        ctx.lineTo(begin.x, begin.y);
        ctx.stroke();
    }
</script>
</body>
</html>

示例

相关推荐
开心工作室_kaic4 分钟前
ssm068海鲜自助餐厅系统+vue(论文+源码)_kaic
前端·javascript·vue.js
有梦想的刺儿23 分钟前
webWorker基本用法
前端·javascript·vue.js
cy玩具44 分钟前
点击评论详情,跳到评论页面,携带对象参数写法:
前端
qq_390161771 小时前
防抖函数--应用场景及示例
前端·javascript
John.liu_Test2 小时前
js下载excel示例demo
前端·javascript·excel
Yaml42 小时前
智能化健身房管理:Spring Boot与Vue的创新解决方案
前端·spring boot·后端·mysql·vue·健身房管理
PleaSure乐事2 小时前
【React.js】AntDesignPro左侧菜单栏栏目名称不显示的解决方案
前端·javascript·react.js·前端框架·webstorm·antdesignpro
哟哟耶耶2 小时前
js-将JavaScript对象或值转换为JSON字符串 JSON.stringify(this.SelectDataListCourse)
前端·javascript·json
getaxiosluo2 小时前
react jsx基本语法,脚手架,父子传参,refs等详解
前端·vue.js·react.js·前端框架·hook·jsx
理想不理想v2 小时前
vue种ref跟reactive的区别?
前端·javascript·vue.js·webpack·前端框架·node.js·ecmascript