Skip to content

Instantly share code, notes, and snippets.

@henrikakselsen
Forked from rjrodger/draw.html
Created July 3, 2012 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henrikakselsen/3038705 to your computer and use it in GitHub Desktop.
Save henrikakselsen/3038705 to your computer and use it in GitHub Desktop.
Little HTML5 mobile web app for drawing on a canvas
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="user-scalable=no,initial-scale=1.0,maximum-scale=1.0" />
<style>
body { padding:10px; margin:0px; background-color: #ccc; }
#main { margin: 10px auto 0px auto; }
</style>
<script src="draw.js"></script>
</head>
<body>
<button id="clear">clear</button><br>
<canvas id="main" width="300" height="300"></canvas>
</body>
</html>
window.onload = function() {
document.ontouchmove = function(e){ e.preventDefault(); }
var canvas = document.getElementById('main');
var canvastop = canvas.offsetTop
var context = canvas.getContext("2d");
var lastx;
var lasty;
context.strokeStyle = "#000000";
context.lineCap = 'round';
context.lineJoin = 'round';
context.lineWidth = 5;
function clear() {
context.fillStyle = "#ffffff";
context.rect(0, 0, 300, 300);
context.fill();
}
function dot(x,y) {
context.beginPath();
context.fillStyle = "#000000";
context.arc(x,y,1,0,Math.PI*2,true);
context.fill();
context.stroke();
context.closePath();
}
function line(fromx,fromy, tox,toy) {
context.beginPath();
context.moveTo(fromx, fromy);
context.lineTo(tox, toy);
context.stroke();
context.closePath();
}
canvas.ontouchstart = function(event){
event.preventDefault();
lastx = event.touches[0].clientX;
lasty = event.touches[0].clientY - canvastop;
dot(lastx,lasty);
}
canvas.ontouchmove = function(event){
event.preventDefault();
var newx = event.touches[0].clientX;
var newy = event.touches[0].clientY - canvastop;
line(lastx,lasty, newx,newy);
lastx = newx;
lasty = newy;
}
var clearButton = document.getElementById('clear')
clearButton.onclick = clear
clear()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment