使用HTML5制作一个可以保存的在线画板

algorain

使用HTML5制作一个可以保存的在线画板;使用HTML5制作一个可以保存的在线画板;使用HTML5制作一个可以保存的在线画板

下面是代码,效果如下图,画好以后右键就可以保存为图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<style type="text/css">
#php100{ border:1px solid #ccc;}
</style>
</head>
<body>
<canvas id="php100" width="500" height="400"></canvas>
<script type="text/javascript">
var canvas = document.getElementById('php100');
var p100=canvas.getContext("2d");
p100.lineWidth=5
p100.strokeStyle="red";
var paint=0;
$("#php100").mousedown(function(e){
var mouseX = e.pageX - this.offsetLeft;
var mouseY = e.pageY - this.offsetTop;
paint=1;
p100.moveTo(mouseX,mouseY); //起始位置
});
$("#php100").mouseup(function(e){
paint=0;
});
$("#php100").mousemove(function(e){
var mouseX = e.pageX - this.offsetLeft;
var mouseY = e.pageY - this.offsetTop;
if(paint){
p100.lineTo(mouseX,mouseY); //终止位置
p100.stroke(); //结束图形
}
});
</script>
</body>
</html>

  • Title: 使用HTML5制作一个可以保存的在线画板
  • Author: algorain
  • Created at: 2017-02-13 19:03:06
  • Updated at: 2023-05-14 21:39:50
  • Link: http://www.rain1024.com/2017/02/13/html-article54/
  • License: This work is licensed under CC BY-NC-SA 4.0.
 Comments