当前位置:首页 > JavaScript

js实现画板

2026-04-06 22:30:42JavaScript

实现基础画板功能

创建一个基础的HTML文件,包含canvas元素和必要的JavaScript代码。以下是一个简单的画板实现:

<!DOCTYPE html>
<html>
<head>
    <title>简易画板</title>
    <style>
        canvas {
            border: 1px solid black;
            cursor: crosshair;
        }
    </style>
</head>
<body>
    <canvas id="drawingCanvas" width="800" height="600"></canvas>

    <script>
        const canvas = document.getElementById('drawingCanvas');
        const ctx = canvas.getContext('2d');
        let isDrawing = false;

        // 设置初始画笔样式
        ctx.strokeStyle = '#000000';
        ctx.lineWidth = 5;
        ctx.lineCap = 'round';

        // 鼠标按下事件
        canvas.addEventListener('mousedown', startDrawing);

        // 鼠标移动事件
        canvas.addEventListener('mousemove', draw);

        // 鼠标释放事件
        canvas.addEventListener('mouseup', stopDrawing);

        // 鼠标离开画布事件
        canvas.addEventListener('mouseout', stopDrawing);

        function startDrawing(e) {
            isDrawing = true;
            ctx.beginPath();
            ctx.moveTo(e.offsetX, e.offsetY);
        }

        function draw(e) {
            if (!isDrawing) return;
            ctx.lineTo(e.offsetX, e.offsetY);
            ctx.stroke();
        }

        function stopDrawing() {
            isDrawing = false;
        }
    </script>
</body>
</html>

添加更多功能

扩展基础画板功能,添加颜色选择、画笔大小调整和清除画布功能:

<div>
    <label>画笔颜色: <input type="color" id="penColor" value="#000000"></label>
    <label>画笔大小: <input type="range" id="penSize" min="1" max="50" value="5"></label>
    <button id="clearBtn">清除画布</button>
</div>

<script>
    // 获取UI元素
    const penColor = document.getElementById('penColor');
    const penSize = document.getElementById('penSize');
    const clearBtn = document.getElementById('clearBtn');

    // 颜色变化事件
    penColor.addEventListener('input', () => {
        ctx.strokeStyle = penColor.value;
    });

    // 画笔大小变化事件
    penSize.addEventListener('input', () => {
        ctx.lineWidth = penSize.value;
    });

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

支持触摸设备

为移动设备添加触摸支持:

// 触摸开始事件
canvas.addEventListener('touchstart', (e) => {
    e.preventDefault();
    const touch = e.touches[0];
    const mouseEvent = {
        offsetX: touch.clientX - canvas.offsetLeft,
        offsetY: touch.clientY - canvas.offsetTop
    };
    startDrawing(mouseEvent);
});

// 触摸移动事件
canvas.addEventListener('touchmove', (e) => {
    e.preventDefault();
    const touch = e.touches[0];
    const mouseEvent = {
        offsetX: touch.clientX - canvas.offsetLeft,
        offsetY: touch.clientY - canvas.offsetTop
    };
    draw(mouseEvent);
});

// 触摸结束事件
canvas.addEventListener('touchend', stopDrawing);

保存画布内容

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

<button id="saveBtn">保存图片</button>

<script>
    document.getElementById('saveBtn').addEventListener('click', () => {
        const link = document.createElement('a');
        link.download = 'drawing.png';
        link.href = canvas.toDataURL('image/png');
        link.click();
    });
</script>

实现橡皮擦功能

添加橡皮擦功能,通过切换绘图模式实现:

js实现画板

<button id="eraserBtn">橡皮擦</button>

<script>
    const eraserBtn = document.getElementById('eraserBtn');
    let isEraser = false;

    eraserBtn.addEventListener('click', () => {
        isEraser = !isEraser;
        if (isEraser) {
            ctx.globalCompositeOperation = 'destination-out';
            eraserBtn.textContent = '画笔';
        } else {
            ctx.globalCompositeOperation = 'source-over';
            eraserBtn.textContent = '橡皮擦';
        }
    });
</script>

这些代码组合在一起可以创建一个功能丰富的画板应用,支持鼠标和触摸输入,具有颜色选择、画笔大小调整、橡皮擦和保存功能。可以根据需要进一步扩展功能,如添加撤销/重做、形状绘制或滤镜效果等。

标签: 画板js
分享给朋友:

相关文章

js实现vue路由

js实现vue路由

Vue 路由的基本实现 在 Vue.js 中实现路由功能通常使用 Vue Router 库。Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。 安装 Vue R…

js实现按钮点击

js实现按钮点击

实现按钮点击的JavaScript方法 HTML按钮元素 在HTML中创建按钮元素,为其添加id或class以便JavaScript选择: <button id="myButton">点…

js实现上传图片

js实现上传图片

使用HTML5的File API实现图片上传 HTML5的File API允许通过JavaScript访问用户选择的文件。需要创建一个文件输入元素,并监听其change事件。 <input t…

js实现图片放大

js实现图片放大

使用 CSS transform 实现图片放大 通过 CSS 的 transform: scale() 属性实现图片放大效果。当鼠标悬停时,图片会平滑放大。 const img = document…

js实现导航菜单

js实现导航菜单

使用HTML和CSS创建基础结构 导航菜单的基础结构通常由HTML的无序列表(<ul>)和列表项(<li>)组成,每个列表项包含一个链接(<a>)。CSS用于样式化…

js实现复制功能实现

js实现复制功能实现

使用document.execCommand方法(传统方式,已逐渐被弃用) 传统方式通过document.execCommand('copy')实现复制功能,需先选中文本。以下是一个完整示例: fu…