Created
January 6, 2015 11:14
-
-
Save Pearyman/a23a4008b83a682d87c1 to your computer and use it in GitHub Desktop.
// source http://jsbin.com/dunahe
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 charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<canvas id="myCanvas" width="600" height="600"></canvas> | |
<script type="text/javascript"> | |
var c=document.getElementById("myCanvas"); | |
var ctx=c.getContext("2d"); | |
ctx.fillStyle="#000"; | |
ctx.fillRect(0,0,600,600); | |
var onoff=false; | |
var oldx=-10; | |
var oldy=-10; | |
var lineColor="#fff"; | |
var linw="4"; | |
c.addEventListener("mousemove",draw,true); | |
c.addEventListener("mousedown",down,false); | |
c.addEventListener("mouseup",up,false); | |
function down(event){ | |
onoff=true; | |
oldx=event.pageX-10; | |
oldy=event.pageY-10; | |
} | |
function up(){ | |
onoff=false; | |
} | |
function draw(event){ | |
if(onoff==true){ | |
var newx=event.pageX-10; | |
var newy=event.pageY-10; | |
ctx.beginPath(); | |
ctx.moveTo(oldx,oldy); | |
ctx.lineTo(newx,newy); | |
ctx.strokeStyle=lineColor; | |
ctx.lineWidth=linw; | |
ctx.lineCap="round"; | |
ctx.stroke(); | |
oldx=newx; | |
oldy=newy; | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment