本人开发小白,在学习前端知识编写一个绘图小工具的时候,突发灵感,一个的想象力有限,我做的一个雏形,在此基础上拜谢CSDN中的开发大佬一起完善这个绘图工具,此工具未来不管做成什么样,都不会用于商业,供大家免费使用,在此,我牵个头,有想法的大佬,都可以在下面更新换代代码,并附带界面效果!谢谢
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>绘图工具</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f7f7f7;
text-align: center;
margin: 0;
padding: 0;
}
h1 {
color: #333;
}
#toolbar {
background-color: #fff;
padding: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
#canvas {
border: 1px solid #ccc;
}
#actions {
margin-top: 10px;
}
#actions button {
margin: 5px;
}
.animated {
animation-duration: 2s;
animation-timing-function: ease;
animation-iteration-count: infinite;
}
@keyframes move {
0% { transform: translateX(0); }
50% { transform: translateX(200px); }
100% { transform: translateX(0); }
}
</style>
</head>
<body>
<h1>绘图工具</h1>
<div id="toolbar">
<button id="penTool">钢笔</button>
<button id="brushTool">毛笔</button>
<button id="rectangleTool">矩形</button>
<button id="circleTool">圆形</button>
<input type="color" id="colorSelect">
<input type="number" id="lineWidthSelect" min="1" max="20" value="2">
</div>
<canvas id="canvas" width="800" height="600"></canvas>
<div id="actions">
<button id="undoButton">撤销</button>
<button id="redoButton">重做</button>
<button id="saveButton">保存</button>
<input type="file" id="imageInput" accept="image/*">
</div>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const penTool = document.getElementById('penTool');
const brushTool = document.getElementById('brushTool');
const rectangleTool = document.getElementById('rectangleTool');
const circleTool = document.getElementById('circleTool');
const colorSelect = document.getElementById('colorSelect');
const lineWidthSelect = document.getElementById('lineWidthSelect');
const undoButton = document.getElementById('undoButton');
const redoButton = document.getElementById('redoButton');
const saveButton = document.getElementById('saveButton');
const imageInput = document.getElementById('imageInput');
const actions = [];
let currentAction = -1;
let drawing = false;
let isPenTool = true;
let animatedElement = null;
canvas.addEventListener('mousedown', (e) => {
drawing = true;
ctx.beginPath();
ctx.moveTo(e.clientX - canvas.getBoundingClientRect().left, e.clientY - canvas.getBoundingClientRect().top);
});
canvas.addEventListener('mousemove', (e) => {
if (!drawing) return;
if (isPenTool) {
ctx.lineTo(e.clientX - canvas.getBoundingClientRect().left, e.clientY - canvas.getBoundingClientRect().top);
ctx.stroke();
canvas.classList.add('animated');
canvas.style.animationName = 'move';
}
});
canvas.addEventListener('mouseup', () => {
drawing = false;
if (isPenTool) {
ctx.closePath();
saveAction();
canvas.classList.remove('animated');
}
});
penTool.addEventListener('click', () => {
isPenTool = true;
});
brushTool.addEventListener('click', () => {
isPenTool = false;
ctx.lineCap = 'round';
ctx.lineJoin = 'round';
});
rectangleTool.addEventListener('click', () => {
isPenTool = false;
});
circleTool.addEventListener('click', () => {
isPenTool = false;
});
colorSelect.addEventListener('input', () => {
ctx.strokeStyle = colorSelect.value;
});
lineWidthSelect.addEventListener('input', () => {
ctx.lineWidth = lineWidthSelect.value;
});
undoButton.addEventListener('click', undo);
redoButton.addEventListener('click', redo);
saveButton.addEventListener('click', saveCanvas);
imageInput.addEventListener('change', (e) => {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (e) {
const image = new Image();
image.src = e.target.result;
image.onload = function () {
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
};
};
reader.readAsDataURL(file);
}
});
function saveAction() {
actions.splice(currentAction + 1);
currentAction++;
actions.push(canvas.toDataURL());
}
function undo() {
if (currentAction > 0) {
currentAction--;
const img = new Image();
img.src = actions[currentAction];
img.onload = () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0);
}
}
}
function redo() {
if (currentAction < actions.length - 1) {
currentAction++;
const img = new Image();
img.src = actions[currentAction];
img.onload = () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0);
}
}
}
function saveCanvas() {
const img = new Image();
img.src = canvas.toDataURL();
const link = document.createElement('a');
link.href = img.src;
link.download = 'drawing.png';
link.click();
}
</script>
</body>
</html>
效果图: