Skip to content

Instantly share code, notes, and snippets.

@tiye
Created July 12, 2012 12:11
Show Gist options
  • Save tiye/3097767 to your computer and use it in GitHub Desktop.
Save tiye/3097767 to your computer and use it in GitHub Desktop.
几行代码网页变成画布
document.body.innerHTML = "<canvas id='cvs' width='1200px' height='600px'></canvas>";
cvs = document.getElementById('cvs');
ctx = cvs.getContext('2d');
cvs.onmousedown = function(){
cvs.onmousemove = function(e){ctx.fillRect(e.offsetX,e.offsetY,2,2);console.log(e.clientX);}
};
cvs.onmouseup = function(){ cvs.onmousemove =function(e){}};
document.body.innerHTML = "<canvas id='cvs' width='1200px' height='600px'></canvas>";
x = 0;
y = 0;
cvs = document.getElementById('cvs');
ctx = cvs.getContext('2d');
cvs.onmousedown = function(e){
cvs.onmousemove = function(e){
m = x + y;
x = e.offsetX;
y = e.offsetY;
if (m > 0){
ctx.lineTo(x,y);
ctx.stroke();
} else {ctx.moveTo(x, y);};
ctx.moveTo(x, y);
};
};
cvs.onmouseup = function(){
cvs.onmousemove = null;
x = 0;
y = 0;
};
window.onblur = function(){
ctx.closePath();
ctx.beginPath();
ctx.clearRect(0,0,1200,900)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment