html5 自动扣图,利用canvas实现一个抠图小工具

本文作者:IMWeb 孙世吉

未经同意,禁止转载

利用canvas实现一个抠图小工具

0 前言

作为新一代的前端开发工程师,PS抠图切图已经不是必备技能了,我们有UI/交互/视觉等更专业的设计同学帮我们做这个事情。但是有时候还是有一些简单的图片处理工作需要我们去做,例如对图片进行剪裁,调整透明度或者调整图片内的文字等等等。这时候如果有一点PS经验那当然更好,如果没有或者当前的开发环境不一定安装了PS等工具,那我们可能需要去找在线图片处理工具来帮我们完成这些工作。

1 Canvas

老话说得好,一个不想当将军的士兵不是好士兵;一个不想自己写工具的程序猿不是好程序猿。那其实上面提及的一些简单图片处理是可以通过Canvas来帮我们实现的。

canvas是一个可以使用脚本(通常为JavaScript)在其中绘制图形的 HTML 元素。它可以用于制作照片集或者制作简单(也不是那么简单)的动画.。

宋丹丹老师说得好:

要把大象放冰箱,总共分三步:一先把冰箱门打开;二把大象放进去;三把冰箱门带上。

整个Canvas图片处理工具大致也可以分为三步。

1、读取图片资源;

2、把图片资源绘制到画板上;

3、作为前端开发的你可以开始为所欲为了;

先看一下很简单的HTML结构和CSS样式

Canvas图片处理演示

原图:

画布:

点我下载

样式主要是为了增加图片背景文理,用于显示透明度

#input-img {

max-width:400px;

}

#input-upload {

width:200px;

}

/* 图片背景纹理 */

.img-container {

font-size:0;

background-image:-webkit-gradient(linear, 0 0, 100% 100%, color-

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的绘图板交互功能示例: 首先,我们需要在HTML文件中添加一个canvas标签,用于绘制图形: ```html <canvas id="canvas" width="500" height="500"></canvas> ``` 然后,我们需要使用JavaScript来实现交互功能。具体步骤如下: 1. 获取canvas元素和2D绘图上下文对象。 ```javascript const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); ``` 2. 添加鼠标事件监听器,用于处理鼠标在canvas上的操作。包括鼠标按下、移动和松开。 ```javascript let isDrawing = false; let lastX = 0; let lastY = 0; canvas.addEventListener('mousedown', (e) => { isDrawing = true; [lastX, lastY] = [e.offsetX, e.offsetY]; }); canvas.addEventListener('mousemove', (e) => { if (!isDrawing) return; const x = e.offsetX; const y = e.offsetY; ctx.beginPath(); ctx.moveTo(lastX, lastY); ctx.lineTo(x, y); ctx.stroke(); [lastX, lastY] = [x, y]; }); canvas.addEventListener('mouseup', () => { isDrawing = false; }); ``` 3. 添加触摸事件监听器,用于处理触摸在canvas上的操作。包括触摸开始、移动和结束。 ```javascript canvas.addEventListener('touchstart', (e) => { isDrawing = true; [lastX, lastY] = [e.touches[0].clientX - canvas.offsetLeft, e.touches[0].clientY - canvas.offsetTop]; }); canvas.addEventListener('touchmove', (e) => { if (!isDrawing) return; const x = e.touches[0].clientX - canvas.offsetLeft; const y = e.touches[0].clientY - canvas.offsetTop; ctx.beginPath(); ctx.moveTo(lastX, lastY); ctx.lineTo(x, y); ctx.stroke(); [lastX, lastY] = [x, y]; }); canvas.addEventListener('touchend', () => { isDrawing = false; }); ``` 4. 添加清除画布功能,可以清除整个画布或者指定区域。 ```javascript function clearCanvas() { ctx.clearRect(0, 0, canvas.width, canvas.height); } function clearRect(x, y, w, h) { ctx.clearRect(x, y, w, h); } ``` 完整的代码示例如下: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Canvas Drawing Board</title> <style> canvas { border: 1px solid black; } </style> </head> <body> <canvas id="canvas" width="500" height="500"></canvas> <br> <button onclick="clearCanvas()">Clear All</button> <button onclick="clearRect(100, 100, 200, 200)">Clear Rect</button> <script> const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); let isDrawing = false; let lastX = 0; let lastY = 0; canvas.addEventListener('mousedown', (e) => { isDrawing = true; [lastX, lastY] = [e.offsetX, e.offsetY]; }); canvas.addEventListener('mousemove', (e) => { if (!isDrawing) return; const x = e.offsetX; const y = e.offsetY; ctx.beginPath(); ctx.moveTo(lastX, lastY); ctx.lineTo(x, y); ctx.stroke(); [lastX, lastY] = [x, y]; }); canvas.addEventListener('mouseup', () => { isDrawing = false; }); canvas.addEventListener('touchstart', (e) => { isDrawing = true; [lastX, lastY] = [e.touches[0].clientX - canvas.offsetLeft, e.touches[0].clientY - canvas.offsetTop]; }); canvas.addEventListener('touchmove', (e) => { if (!isDrawing) return; const x = e.touches[0].clientX - canvas.offsetLeft; const y = e.touches[0].clientY - canvas.offsetTop; ctx.beginPath(); ctx.moveTo(lastX, lastY); ctx.lineTo(x, y); ctx.stroke(); [lastX, lastY] = [x, y]; }); canvas.addEventListener('touchend', () => { isDrawing = false; }); function clearCanvas() { ctx.clearRect(0, 0, canvas.width, canvas.height); } function clearRect(x, y, w, h) { ctx.clearRect(x, y, w, h); } </script> </body> </html> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值