html5教程制作简单画板代码分享
程序员文章站
2023-11-17 15:01:04
html5制作简单画板代码分享,大家参考使用吧... 13-12-04...
html5制作简单画板
复制代码
代码如下:<!doctype html>
<html lang="en-us">
<head>
<title></title>
<meta charset="utf-8">
<script src="<a href="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script">http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script</a>>
</head>
<style>
canvas{ border:2px solid #000;}
</style>
<body>
<canvas id="draw" width="500" height="500"></canvas>
<script type="text/javascript">
$(document).ready(function(){
var canvas=document.getelementbyid("draw");
var draw=canvas.getcontext("2d");
draw.linewidth=5; //线条粗细
draw.strokestyle="red"; //颜色
var godraw=false;
//按下鼠标
$("#draw").mousedown(function(e){
//准确坐标
var mousex=e.pagex-this.offsetleft;
var mousey=e.pagey-this.offsettop;
draw.moveto(mousex,mousey);
godraw=true;</p> <p> })
//放开鼠标
$("#draw").mouseup(function(e){
godraw=false;
})
//移动鼠标
$("#draw").mousemove(function(e){
if(godraw){
var mousex=e.pagex-this.offsetleft;
var mousey=e.pagey-this.offsettop;
draw.lineto(mousex+4,mousey+4);
draw.stroke();
}</p> <p> })
})
</script>
</body>
</html>
效果图