Last active
August 29, 2015 14:20
-
-
Save shiftyp/8cdfce8cf0f382ebd0e6 to your computer and use it in GitHub Desktop.
Simple Paint Program
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
.canvas { | |
width: 400px; | |
height: 400px; | |
border: 1px solid #f00; | |
user-select: none; | |
-webkit-user-select: none; | |
} | |
.pixel { | |
width: 5px; | |
height: 5px; | |
background-color: #fff; | |
float: left; | |
} |
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Simple Paint Program</title> | |
<link rel="stylesheet" href="index.css"> | |
</head> | |
<body> | |
<div id="container" class="canvas"></div> | |
<script type="text/javascript" src="index.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
var selectedColor = '#000'; | |
var container = document.getElementById('container'); | |
var pixelCount = (container.clientWidth / 5) * (container.clientHeight / 5); | |
function draw(event) { | |
var pixel = event.target; | |
if (pixel === container) return; | |
pixel.style.backgroundColor = selectedColor; | |
} | |
function startDraw(event) { | |
container.addEventListener('mouseover', draw); | |
draw(event); | |
} | |
function stopDraw(event) { | |
container.removeEventListener('mouseover', draw); | |
} | |
container.addEventListener('mousedown', startDraw); | |
container.addEventListener('mouseup', stopDraw); | |
for (var i = 0; i < pixelCount; i++) { | |
var pixel = document.createElement('div'); | |
pixel.className = 'pixel'; | |
container.appendChild(pixel); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment