Skip to content

Instantly share code, notes, and snippets.

@Pearyman
Created January 6, 2015 11:14
Show Gist options
  • Save Pearyman/a23a4008b83a682d87c1 to your computer and use it in GitHub Desktop.
Save Pearyman/a23a4008b83a682d87c1 to your computer and use it in GitHub Desktop.
<!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