当前位置:首页 > JavaScript

js实现画笔功能

2026-03-01 14:58:52JavaScript

实现基础画布与画笔功能

创建一个HTML文件并添加<canvas>元素作为画布。通过JavaScript获取画布上下文并设置初始样式:

<canvas id="drawCanvas" width="800" height="600"></canvas>
<script>
  const canvas = document.getElementById('drawCanvas');
  const ctx = canvas.getContext('2d');
  ctx.strokeStyle = '#000000';
  ctx.lineWidth = 5;
  ctx.lineCap = 'round';
</script>

监听鼠标事件实现绘制

绑定鼠标事件来跟踪绘制动作。需要处理mousedownmousemovemouseup事件:

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;
  ctx.beginPath();
  ctx.moveTo(lastX, lastY);
  ctx.lineTo(e.offsetX, e.offsetY);
  ctx.stroke();
  [lastX, lastY] = [e.offsetX, e.offsetY];
});

canvas.addEventListener('mouseup', () => isDrawing = false);
canvas.addEventListener('mouseout', () => isDrawing = false);

添加触摸屏支持

为移动设备添加触摸事件处理:

js实现画笔功能

canvas.addEventListener('touchstart', (e) => {
  e.preventDefault();
  isDrawing = true;
  const touch = e.touches[0];
  const rect = canvas.getBoundingClientRect();
  [lastX, lastY] = [touch.clientX - rect.left, touch.clientY - rect.top];
});

canvas.addEventListener('touchmove', (e) => {
  if (!isDrawing) return;
  e.preventDefault();
  const touch = e.touches[0];
  const rect = canvas.getBoundingClientRect();
  const x = touch.clientX - rect.left;
  const y = touch.clientY - rect.top;
  ctx.beginPath();
  ctx.moveTo(lastX, lastY);
  ctx.lineTo(x, y);
  ctx.stroke();
  [lastX, lastY] = [x, y];
});

canvas.addEventListener('touchend', () => isDrawing = false);

实现画笔属性调整

添加控制面板来调整画笔颜色和粗细:

<input type="color" id="colorPicker" value="#000000">
<input type="range" id="brushSize" min="1" max="50" value="5">
const colorPicker = document.getElementById('colorPicker');
const brushSize = document.getElementById('brushSize');

colorPicker.addEventListener('input', () => {
  ctx.strokeStyle = colorPicker.value;
});

brushSize.addEventListener('input', () => {
  ctx.lineWidth = brushSize.value;
});

添加清除画布功能

实现清除按钮来重置画布:

js实现画笔功能

<button id="clearBtn">清除画布</button>
document.getElementById('clearBtn').addEventListener('click', () => {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
});

优化绘制性能

使用requestAnimationFrame优化绘制性能:

function draw(e) {
  if (!isDrawing) return;
  ctx.beginPath();
  ctx.moveTo(lastX, lastY);
  ctx.lineTo(e.offsetX, e.offsetY);
  ctx.stroke();
  [lastX, lastY] = [e.offsetX, e.offsetY];
  requestAnimationFrame(() => draw(e));
}

canvas.addEventListener('mousedown', (e) => {
  isDrawing = true;
  [lastX, lastY] = [e.offsetX, e.offsetY];
  draw(e);
});

保存绘制内容

添加保存功能将画布内容导出为图片:

<button id="saveBtn">保存图片</button>
document.getElementById('saveBtn').addEventListener('click', () => {
  const link = document.createElement('a');
  link.download = 'drawing.png';
  link.href = canvas.toDataURL('image/png');
  link.click();
});

标签: 画笔功能
分享给朋友:

相关文章

vue实现聊天功能

vue实现聊天功能

Vue实现聊天功能的方法 使用WebSocket实现实时通信 安装WebSocket客户端库如socket.io-client,在Vue组件中建立连接。创建一个消息列表和数据输入框,通过WebSock…

vue实现名字查找功能

vue实现名字查找功能

实现名字查找功能 在Vue中实现名字查找功能,可以通过多种方式完成。以下是几种常见的实现方法: 使用v-model和computed属性 创建一个输入框,使用v-model绑定输入的值,通过comp…

js实现复制功能

js实现复制功能

使用 document.execCommand 方法 这种方法适用于较旧的浏览器,但在现代浏览器中可能被逐步淘汰。通过创建一个临时的 textarea 元素,将文本内容放入其中,然后执行复制命令。…

uniapp实现支付功能

uniapp实现支付功能

支付功能实现概述 在UniApp中实现支付功能通常需要对接第三方支付平台(如微信支付、支付宝支付等)。以下是基于微信支付和支付宝支付的通用实现流程。 微信支付实现步骤 1. 配置支付权限 在微信开放…

vue电影功能实现

vue电影功能实现

Vue 电影功能实现 数据获取与展示 使用 Axios 或 Fetch API 从电影 API(如 TMDb、豆瓣 API)获取数据。在 Vue 的 created 或 mounted 生命周期钩子中…

vue实现收货功能

vue实现收货功能

实现收货功能的基本思路 在Vue中实现收货功能通常涉及前端界面交互、后端API调用以及状态管理。以下是关键步骤和代码示例: 页面布局与数据绑定 使用Vue的模板语法构建收货表单,包含收货人、联系方式…