当前位置:首页 > JavaScript

js实现涂鸦面板

2026-03-15 14:42:57JavaScript

实现涂鸦面板的基本思路

涂鸦面板的核心功能是允许用户在画布上通过鼠标或触摸操作自由绘制线条。HTML5的Canvas API是实现这一功能的理想选择。

js实现涂鸦面板

创建基础HTML结构

<!DOCTYPE html>
<html>
<head>
    <title>涂鸦面板</title>
    <style>
        #drawingCanvas {
            border: 1px solid #000;
            cursor: crosshair;
        }
    </style>
</head>
<body>
    <canvas id="drawingCanvas" width="800" height="600"></canvas>
    <script src="drawing.js"></script>
</body>
</html>

初始化画布和绘制上下文

const canvas = document.getElementById('drawingCanvas');
const ctx = canvas.getContext('2d');
let isDrawing = false;
let lastX = 0;
let lastY = 0;

实现鼠标事件处理

canvas.addEventListener('mousedown', startDrawing);
canvas.addEventListener('mousemove', draw);
canvas.addEventListener('mouseup', stopDrawing);
canvas.addEventListener('mouseout', stopDrawing);

function startDrawing(e) {
    isDrawing = true;
    [lastX, lastY] = [e.offsetX, e.offsetY];
}

function draw(e) {
    if (!isDrawing) return;

    ctx.beginPath();
    ctx.moveTo(lastX, lastY);
    ctx.lineTo(e.offsetX, e.offsetY);
    ctx.strokeStyle = '#000';
    ctx.lineWidth = 5;
    ctx.lineCap = 'round';
    ctx.stroke();

    [lastX, lastY] = [e.offsetX, e.offsetY];
}

function stopDrawing() {
    isDrawing = false;
}

添加触摸支持

canvas.addEventListener('touchstart', handleTouchStart);
canvas.addEventListener('touchmove', handleTouchMove);
canvas.addEventListener('touchend', handleTouchEnd);

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

function handleTouchMove(e) {
    e.preventDefault();
    if (!isDrawing) return;

    const touch = e.touches[0];
    const rect = canvas.getBoundingClientRect();
    const currentX = touch.clientX - rect.left;
    const currentY = touch.clientY - rect.top;

    ctx.beginPath();
    ctx.moveTo(lastX, lastY);
    ctx.lineTo(currentX, currentY);
    ctx.strokeStyle = '#000';
    ctx.lineWidth = 5;
    ctx.lineCap = 'round';
    ctx.stroke();

    [lastX, lastY] = [currentX, currentY];
}

function handleTouchEnd() {
    isDrawing = false;
}

添加颜色选择功能

<input type="color" id="colorPicker" value="#000000">
const colorPicker = document.getElementById('colorPicker');
colorPicker.addEventListener('input', function() {
    ctx.strokeStyle = this.value;
});

添加线宽控制

<input type="range" id="lineWidth" min="1" max="50" value="5">
const lineWidth = document.getElementById('lineWidth');
lineWidth.addEventListener('input', function() {
    ctx.lineWidth = this.value;
});

实现清除画布功能

<button id="clearCanvas">清除</button>
const clearButton = document.getElementById('clearCanvas');
clearButton.addEventListener('click', function() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
});

保存绘制内容

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

优化绘制性能

// 使用requestAnimationFrame优化绘制
function drawOptimized(e) {
    if (!isDrawing) return;

    requestAnimationFrame(() => {
        ctx.beginPath();
        ctx.moveTo(lastX, lastY);
        ctx.lineTo(e.offsetX, e.offsetY);
        ctx.stroke();
        [lastX, lastY] = [e.offsetX, e.offsetY];
    });
}

添加橡皮擦功能

<button id="eraser">橡皮擦</button>
const eraser = document.getElementById('eraser');
eraser.addEventListener('click', function() {
    ctx.strokeStyle = '#FFFFFF'; // 假设背景是白色
    ctx.lineWidth = 20; // 设置较大的橡皮擦尺寸
});

这个实现提供了基本的涂鸦功能,包括鼠标和触摸支持、颜色选择、线宽调整、清除和保存功能。可以根据需要进一步扩展,比如添加撤销/重做功能、不同画笔样式或图层支持等。

标签: 涂鸦面板
分享给朋友:

相关文章

react实现切换面板

react实现切换面板

使用状态管理切换面板 在React中,可以通过useState钩子来管理当前显示的面板状态。定义一个状态变量存储当前激活的面板标识,通过点击事件切换状态值。 import React, { useS…

react实现折叠面板

react实现折叠面板

React 实现折叠面板的方法 使用 useState 管理状态 通过 React 的 useState 钩子管理折叠面板的展开/折叠状态。定义一个布尔值状态变量(如 isExpanded)和对应的更…

vue实现收起展开面板

vue实现收起展开面板

Vue 实现收起展开面板 使用 Vue 实现收起展开面板可以通过多种方式完成,以下是几种常见的方法: 使用 v-show 或 v-if 控制显示隐藏 通过 Vue 的指令 v-show 或 v-if…

vue实现聊天面板

vue实现聊天面板

实现聊天面板的基本结构 使用Vue的单文件组件结构,创建聊天面板的模板部分。需要一个消息列表容器和消息输入框。 <template> <div class="chat-pane…

php实现颜色面板

php实现颜色面板

PHP 实现颜色面板的方法 使用 HTML 和 PHP 生成基础颜色选择器 通过 HTML 的 input 类型为 color 可以直接调用浏览器原生颜色选择器,PHP 用于处理提交后的颜色值。 &…

vue实现滑动面板

vue实现滑动面板

Vue 实现滑动面板的方法 使用第三方库(如 vue-swipe) 安装 vue-swipe 库: npm install vue-swipe --save 在组件中引入并使用: <temp…