-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1fe308f
commit 21ac8d0
Showing
7 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Pixel Painter</title> | ||
<link rel="stylesheet" href="styles.css"> | ||
</head> | ||
<body> | ||
<div id="toolbar"> | ||
<input type="color" id="colorPicker" value="#000000"> | ||
<button id="clearButton">Clear</button> | ||
<button id="undoButton">Undo</button> | ||
</div> | ||
<div id="canvasContainer" style="position: relative;"> | ||
<canvas id="pixelCanvas" width="600" height="600"></canvas> | ||
<div id="grid"></div> | ||
</div> | ||
<script src="script.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// script.js | ||
document.addEventListener('DOMContentLoaded', () => { | ||
const canvas = document.getElementById('pixelCanvas'); | ||
const ctx = canvas.getContext('2d'); | ||
const colorPicker = document.getElementById('colorPicker'); | ||
const clearButton = document.getElementById('clearButton'); | ||
const undoButton = document.getElementById('undoButton'); | ||
let drawing = false; | ||
let color = colorPicker.value; | ||
let undoStack = []; | ||
|
||
// Initialize canvas | ||
ctx.fillStyle = '#fff'; | ||
ctx.fillRect(0, 0, canvas.width, canvas.height); | ||
|
||
canvas.addEventListener('mousedown', () => drawing = true); | ||
canvas.addEventListener('mouseup', () => drawing = false); | ||
canvas.addEventListener('mousemove', draw); | ||
colorPicker.addEventListener('input', (e) => { | ||
color = e.target.value; | ||
colorPicker.style.borderColor = color; | ||
}); | ||
clearButton.addEventListener('click', clearCanvas); | ||
undoButton.addEventListener('click', undo); | ||
|
||
canvas.addEventListener('mousedown', saveState); | ||
|
||
function draw(event) { | ||
if (!drawing) return; | ||
const rect = canvas.getBoundingClientRect(); | ||
const x = event.clientX - rect.left; | ||
const y = event.clientY - rect.top; | ||
ctx.fillStyle = color; | ||
ctx.fillRect(Math.floor(x / 10) * 10, Math.floor(y / 10) * 10, 10, 10); | ||
} | ||
|
||
function clearCanvas() { | ||
ctx.fillStyle = '#fff'; | ||
ctx.fillRect(0, 0, canvas.width, canvas.height); | ||
} | ||
|
||
function saveState() { | ||
undoStack.push(ctx.getImageData(0, 0, canvas.width, canvas.height)); | ||
if (undoStack.length > 10) undoStack.shift(); // Limit stack size | ||
} | ||
|
||
function undo() { | ||
if (undoStack.length > 0) { | ||
const imgData = undoStack.pop(); | ||
ctx.putImageData(imgData, 0, 0); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* styles.css */ | ||
body { | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
background-color: #f0f0f0; | ||
font-family: Arial, sans-serif; | ||
} | ||
|
||
#toolbar { | ||
margin: 20px; | ||
} | ||
|
||
button { | ||
transition: background-color 0.3s, transform 0.3s; | ||
} | ||
|
||
button:hover { | ||
background-color: #ddd; | ||
transform: scale(1.1); | ||
} | ||
|
||
#colorPicker { | ||
transition: box-shadow 0.3s; | ||
} | ||
|
||
#colorPicker:focus { | ||
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); | ||
} | ||
|
||
#canvasContainer { | ||
position: relative; | ||
} | ||
|
||
#pixelCanvas { | ||
border: 1px solid #000; | ||
background-color: #fff; | ||
cursor: crosshair; | ||
position: relative; | ||
} | ||
|
||
#grid { | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
width: 100%; | ||
height: 100%; | ||
pointer-events: none; | ||
background: linear-gradient(90deg, rgba(0,0,0,0.1) 1px, transparent 1px), | ||
linear-gradient(rgba(0,0,0,0.1) 1px, transparent 1px); | ||
background-size: 10px 10px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.