Created
June 6, 2011 20:32
-
-
Save rjrodger/1011032 to your computer and use it in GitHub Desktop.
Little HTML5 mobile web app for drawing on a canvas
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> | |
<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> |
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
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
This is my code currently. Its working for desktop just fine but for mobile I can't draw anything.
window.addEventListener("load", () => {
const canvas = document.querySelector('#canvas');
const ctx = canvas.getContext('2d');
});