Untitled
unknown
plain_text
a year ago
2.6 kB
6
Indexable
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Painting Game</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
canvas {
border: 2px solid #000;
cursor: crosshair;
}
.controls {
display: flex;
gap: 10px;
margin-top: 20px;
}
.controls button {
padding: 10px 20px;
cursor: pointer;
font-size: 16px;
}
</style>
</head>
<body>
<canvas id="paintCanvas" width="800" height="600"></canvas>
<div class="controls">
<button id="clearButton">Clear Canvas</button>
<input type="color" id="colorPicker" value="#000000">
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const canvas = document.getElementById('paintCanvas');
const ctx = canvas.getContext('2d');
const clearButton = document.getElementById('clearButton');
const colorPicker = document.getElementById('colorPicker');
let painting = false;
function startPosition(e) {
painting = true;
draw(e);
}
function endPosition() {
painting = false;
ctx.beginPath();
}
function draw(e) {
if (!painting) return;
ctx.lineWidth = 5;
ctx.lineCap = 'round';
ctx.strokeStyle = colorPicker.value;
ctx.lineTo(e.clientX - canvas.offsetLeft, e.clientY - canvas.offsetTop);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(e.clientX - canvas.offsetLeft, e.clientY - canvas.offsetTop);
}
canvas.addEventListener('mousedown', startPosition);
canvas.addEventListener('mouseup', endPosition);
canvas.addEventListener('mousemove', draw);
canvas.addEventListener('mouseleave', endPosition);
clearButton.addEventListener('click', () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
});
});
</script>
</body>
</html>
Editor is loading...
Leave a Comment