当前位置:首页 > JavaScript

js实现涂鸦面板

2026-03-15 14:42:57JavaScript

js实现涂鸦面板

js实现涂鸦面板

实现涂鸦面板的基本思路

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

创建基础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; // 设置较大的橡皮擦尺寸
});

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

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

相关文章

vue实现收起展开面板

vue实现收起展开面板

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

react实现折叠面板

react实现折叠面板

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

java如何设置面板

java如何设置面板

设置面板的基本方法 在Java中,通常使用JPanel类来创建和设置面板。JPanel是Swing库中的一个轻量级容器,用于组织其他组件。 import javax.swing.JPanel; im…

vue实现涂鸦工具

vue实现涂鸦工具

Vue 实现涂鸦工具的方法 使用 HTML5 Canvas 和 Vue 结合 HTML5 的 <canvas> 元素是实现涂鸦功能的核心,结合 Vue 的数据绑定和事件处理能力,可以轻松构…

vue实现面板拖拽

vue实现面板拖拽

Vue 实现面板拖拽的方法 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性和相关事件实现拖拽功能。在 Vue 中,可以通过绑定这些事件来实…

vue实现滑动面板

vue实现滑动面板

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