Created
July 12, 2012 12:11
-
-
Save tiye/3097767 to your computer and use it in GitHub Desktop.
几行代码网页变成画布
This file contains hidden or 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
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){}}; |
This file contains hidden or 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
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