-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvasPaint.html
92 lines (91 loc) · 2.32 KB
/
canvasPaint.html
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="./js/jquery-2.1.4.js"></script>
<style>
body,div{
margin: 0px;
padding: 0px;
text-align: center;
}
#cav{
border:2px solid black;
border-radius: 4px;
box-shadow: 0px 0px 10px black;
-webkit-box-shadow:0px 0px 10px black;
-moz-box-shadow:0px 0px 10px black;
}
#bk{
width: 30px;
margin: 12px;
display: inline-block;
}
#save{
background-color: black;
color: white;
padding: 10px;
border-radius: 10px;
cursor: pointer;
display: inline-block;
font-size: 20px;
}
</style>
</head>
<body>
<div id="bk" ></div>
<canvas id="cav" width="700" height="700"></canvas>
<span id="save">save</span>
<script>
//定义画笔颜色
var bColor = ["#000000","#999999","#cc66ff","#ff0000","#ff9900","#ffff00","#008000","#00ccff","#009988"];
var col = "#000000";
function initBrush(){
console.log(bColor.length);
for(var i=0;i<bColor.length;i++){
var bk = $("<span></span>")
.css({"display": "inline-block", "width":"30px", "height":"20px", "background-color": bColor[i]})
.click(function(){
col = $(this).css("background-color");
});
$("#bk").append(bk);
console.log(i);
}
}
function initPainter(){
var self = this, x=0, y=0;
var can = $("#cav");
var ctx = can[0].getContext("2d");
ctx.lineWidth = 20;
can.on("mousedown",function(e){
e.preventDefault();
ctx.strokeStyle = col;
x = e.offsetX,
y = e.offsetY;
ctx.beginPath();
ctx.moveTo(x,y);
can.on("mousemove",function(e){
x = e.offsetX,
y = e.offsetY;
ctx.lineTo(x,y);
ctx.stroke();
});
can.on("mouseup",function(){
can.off("mousemove");
});
})
}
$("#save").click(function(){
var obj = $("#cav")[0];
var dataURL = obj.toDataURL("image/png");
var w = window.open();
w.document.write("<img src = '"+dataURL+"'/>");
});
$(document).ready(function(){
initBrush();
initPainter();
})
</script>
</body>
</html>