Skip to content

Instantly share code, notes, and snippets.

@jrk
Created November 5, 2009 01:35
Show Gist options
  • Save jrk/226604 to your computer and use it in GitHub Desktop.
Save jrk/226604 to your computer and use it in GitHub Desktop.
Minimalist painting in HTML5 canvas + Javascript.
<!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