js实现画板
使用Canvas实现基础画板
HTML中需要添加Canvas元素并设置尺寸
<canvas id="drawing-board" width="800" height="600"></canvas>
获取Canvas上下文并设置初始绘图状态
const canvas = document.getElementById('drawing-board');
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.stroke();
[lastX, lastY] = [e.offsetX, e.offsetY];
}
function stopDrawing() {
isDrawing = false;
}
添加绘图样式控制
设置线条样式
ctx.strokeStyle = '#000000';
ctx.lineWidth = 5;
ctx.lineCap = 'round';
ctx.lineJoin = 'round';
添加颜色选择功能

<input type="color" id="color-picker">
document.getElementById('color-picker').addEventListener('change', (e) => {
ctx.strokeStyle = e.target.value;
});
实现橡皮擦功能
切换绘图模式
let isErasing = false;
function toggleEraser() {
isErasing = !isErasing;
if (isErasing) {
ctx.globalCompositeOperation = 'destination-out';
} else {
ctx.globalCompositeOperation = 'source-over';
}
}
添加清除画布功能
实现清空画布
function clearCanvas() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
支持触摸设备
添加触摸事件支持

canvas.addEventListener('touchstart', handleTouchStart);
canvas.addEventListener('touchmove', handleTouchMove);
canvas.addEventListener('touchend', handleTouchEnd);
function handleTouchStart(e) {
e.preventDefault();
const touch = e.touches[0];
const mouseEvent = new MouseEvent('mousedown', {
clientX: touch.clientX,
clientY: touch.clientY
});
canvas.dispatchEvent(mouseEvent);
}
function handleTouchMove(e) {
e.preventDefault();
const touch = e.touches[0];
const mouseEvent = new MouseEvent('mousemove', {
clientX: touch.clientX,
clientY: touch.clientY
});
canvas.dispatchEvent(mouseEvent);
}
function handleTouchEnd(e) {
e.preventDefault();
const mouseEvent = new MouseEvent('mouseup', {});
canvas.dispatchEvent(mouseEvent);
}
保存绘图结果
实现图像保存功能
function saveDrawing() {
const dataURL = canvas.toDataURL('image/png');
const link = document.createElement('a');
link.download = 'drawing.png';
link.href = dataURL;
link.click();
}
进阶功能:撤销/重做
实现简单历史记录
const history = [];
let historyIndex = -1;
function saveState() {
historyIndex++;
if (historyIndex < history.length) {
history.length = historyIndex;
}
history.push(canvas.toDataURL());
}
function undo() {
if (historyIndex <= 0) return;
historyIndex--;
const img = new Image();
img.onload = () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0);
};
img.src = history[historyIndex];
}
function redo() {
if (historyIndex >= history.length - 1) return;
historyIndex++;
const img = new Image();
img.onload = () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0);
};
img.src = history[historyIndex];
}
在绘图事件中添加状态保存
function stopDrawing() {
isDrawing = false;
saveState();
}






