Created
November 5, 2009 01:35
-
-
Save jrk/226604 to your computer and use it in GitHub Desktop.
Minimalist painting in HTML5 canvas + Javascript.
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> | |
<head> | |
<script type="text/javascript"> | |
function draw() { | |
var canvas = document.getElementById("canvas"); | |
var ctx = canvas.getContext("2d"); | |
ctx.fillStyle = "black"; | |
ctx.beginPath(); | |
var x; | |
var y; | |
canvas.onmousedown = function(e) { | |
x = e.clientX; | |
y = e.clientY; | |
ctx.moveTo(x, y); | |
} | |
canvas.onmouseup = function(e) { | |
x = null; | |
y = null; | |
} | |
canvas.onmousemove = function(e) { | |
if (x == null || y == null) { | |
return; | |
} | |
x = e.clientX; | |
y = e.clientY; | |
x -= canvas.offsetLeft; | |
y -= canvas.offsetTop; | |
ctx.lineTo(x, y); | |
ctx.stroke(); | |
ctx.moveTo(x, y); | |
} | |
}; | |
</script> | |
<title></title> | |
</head> | |
<body onload="draw();"> | |
<canvas id="canvas" width="300" height="300" style="border: 1px solid black;"></canvas> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment