|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 6 | + <title>Pixel Art Maker</title> |
| 7 | + <style> |
| 8 | + body { |
| 9 | + font-family: Arial, sans-serif; |
| 10 | + background-color: #f4f4f4; |
| 11 | + margin: 0; |
| 12 | + padding: 20px; |
| 13 | + } |
| 14 | + |
| 15 | + .container { |
| 16 | + max-width: 600px; |
| 17 | + margin: auto; |
| 18 | + text-align: center; |
| 19 | + background: #fff; |
| 20 | + padding: 20px; |
| 21 | + border-radius: 5px; |
| 22 | + box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); |
| 23 | + } |
| 24 | + |
| 25 | + h1 { |
| 26 | + margin-bottom: 20px; |
| 27 | + } |
| 28 | + |
| 29 | + .controls { |
| 30 | + margin-bottom: 20px; |
| 31 | + } |
| 32 | + |
| 33 | + canvas { |
| 34 | + border: 1px solid #ccc; |
| 35 | + cursor: crosshair; |
| 36 | + } |
| 37 | + </style> |
| 38 | +</head> |
| 39 | +<body> |
| 40 | + <div class="container"> |
| 41 | + <h1>Pixel Art Maker</h1> |
| 42 | + <div class="controls"> |
| 43 | + <input type="color" id="colorPicker" value="#000000"> |
| 44 | + <button id="clearBtn">Clear Canvas</button> |
| 45 | + <button id="downloadBtn">Download as PNG</button> |
| 46 | + </div> |
| 47 | + <canvas id="canvas" width="400" height="400"></canvas> |
| 48 | + </div> |
| 49 | + |
| 50 | + <script> |
| 51 | + const canvas = document.getElementById('canvas'); |
| 52 | + const ctx = canvas.getContext('2d'); |
| 53 | + const colorPicker = document.getElementById('colorPicker'); |
| 54 | + const clearBtn = document.getElementById('clearBtn'); |
| 55 | + const downloadBtn = document.getElementById('downloadBtn'); |
| 56 | + |
| 57 | + let painting = false; |
| 58 | + let currentColor = colorPicker.value; |
| 59 | + |
| 60 | + const pixelSize = 20; |
| 61 | + canvas.width = 400; |
| 62 | + canvas.height = 400; |
| 63 | + |
| 64 | + canvas.addEventListener('mousedown', startPainting); |
| 65 | + canvas.addEventListener('mouseup', stopPainting); |
| 66 | + canvas.addEventListener('mousemove', paint); |
| 67 | + colorPicker.addEventListener('input', (e) => currentColor = e.target.value); |
| 68 | + clearBtn.addEventListener('click', clearCanvas); |
| 69 | + downloadBtn.addEventListener('click', downloadImage); |
| 70 | + |
| 71 | + function startPainting(e) { |
| 72 | + painting = true; |
| 73 | + paint(e); |
| 74 | + } |
| 75 | + |
| 76 | + function stopPainting() { |
| 77 | + painting = false; |
| 78 | + ctx.beginPath(); |
| 79 | + } |
| 80 | + |
| 81 | + function paint(e) { |
| 82 | + if (!painting) return; |
| 83 | + const rect = canvas.getBoundingClientRect(); |
| 84 | + const x = Math.floor((e.clientX - rect.left) / pixelSize) * pixelSize; |
| 85 | + const y = Math.floor((e.clientY - rect.top) / pixelSize) * pixelSize; |
| 86 | + ctx.fillStyle = currentColor; |
| 87 | + ctx.fillRect(x, y, pixelSize, pixelSize); |
| 88 | + } |
| 89 | + |
| 90 | + function clearCanvas() { |
| 91 | + ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 92 | + } |
| 93 | + |
| 94 | + function downloadImage() { |
| 95 | + const link = document.createElement('a'); |
| 96 | + link.download = 'pixel-art.png'; |
| 97 | + link.href = canvas.toDataURL(); |
| 98 | + link.click(); |
| 99 | + } |
| 100 | + </script> |
| 101 | +</body> |
| 102 | +</html> |
0 commit comments