手写板源码

sxb.wxml

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
<view class="contain">
<view>绘制你的图案</view>
<canvas canvas-id="canvas" class="canvas"
bindtouchstart="start" bindtouchmove="move" bindtouchend="end"></canvas>
<text>坐 标: ({{x1}},{{y1}})</text>
</view>
<view class="menu">
<button bindtap="clear">清除画板</button>
<button bindtap="changecolor">画笔颜色</button>
<view class="view2" style="{{animate}}" hidden="{{yincang}}">
<view class="red" bindtap="red"></view>
<view class="yellow" bindtap="yellow"></view>
<view class="blue" bindtap="blue"></view>
<view class="eee" bindtap="eee"></view>
<view class="black" bindtap="black"></view>
<view class="water" bindtap="water"></view>
<view class="zi" bindtap="zi"></view>
</view>
<button bindtap="changewidth">粗细</button>
<view class="view3" style="{{animate2}}" hidden="{{yincang2}}">
<view class="line-width" bindtap="xi">细</view>
<view class="line-width" bindtap="zx">中细</view>
<view class="line-width" bindtap="pt">普通</view>
<view class="line-width" bindtap="zc">中粗</view>
<view class="line-width" bindtap="c">粗</view>
</view>
</view>

sxb.wxss

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
@import "../../animate.wxss";
page{
height: 100%;
background-color: #fffbf0;
}
.contain{

display: flex;
flex-direction: column;
align-items: center;
}
.canvas{
background-color: white;
border: 1px solid black;
width: 96%;
height: 600rpx;
box-sizing: border-box;
box-shadow:11rpx 11rpx 11rpx gray;
}
text{
margin-top: 15rpx;
font-size: 35rpx;
}
button{
box-shadow: 11rpx 11rpx 11rpx gray;
margin-bottom: 20rpx;
width: 200rpx;
line-height: 60rpx;
}
.view2{
width: 80%;
height: 60rpx;
display: flex;
flex-direction: row;
margin: 0 auto;
text-align: center;
/* animation: zoomIn 2.5s; */
}
.view2 view{
/* border: 1rpx solid black; */
flex: 1;
margin-left: 30rpx;
border-radius: 50%;
margin-bottom: 10rpx;
}
.red{
background-color: red;
}
.yellow{
background-color: yellow;
}
.blue{
background-color: #3AC2F1;
}
.eee{
background-color: green;
}
.black{
background-color: black;
}
.water{
background-color: #88ada6;
}
.zi{
background-color: #725e82;
}
.menu{
margin-top: 20rpx;
/* background-color: burlywood; */

}
.view3{
display: flex;
width: 90%;
margin: 0 auto;

}
.line-width{
flex: 1;
text-align: center;
}

sxb.js

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
var cvs = null;
var touchs=[];
Page({
data: {
x1:0,
y1:0,
linecolor: "black",
linewidth:3,
animate:"",
animate2:"",
yincang:true,
yincang2:true
},
start:function(e){
this.setData({
x1:Math.floor(e.touches[0].x),
y1:Math.floor(e.touches[0].y)
})
let point={ x:e.touches[0].x,y:e.touches[0].y}
touchs.push(point)
},
move:function(e){
this.setData({
x1:Math.floor(e.touches[0].x),
y1:Math.floor(e.touches[0].y)
})
console.log(touchs.length)
let point={ x:e.touches[0].x,y:e.touches[0].y}
touchs.push(point)
this.draw(touchs)
},
end:function(e){
// touchs.pop()
touchs=[]
},
draw:function(touchs){
var point1=touchs[0];
var point2=touchs[1];
cvs.moveTo(point1.x,point1.y);
cvs.lineTo(point2.x,point2.y);
cvs.stroke();
cvs.draw(true);
touchs.shift();
},
onLoad: function () {
cvs=wx.createCanvasContext('canvas');
// cvs.beginPath();
cvs.setStrokeStyle(this.data.linecolor);
cvs.setLineWidth(this.data.linewidth);
cvs.setLineCap('round');
cvs.setLineJoin('round');
},
clear:function(){
cvs.clearRect(0,0,750,600);
cvs.draw(true);
this.setData({
x1:0,
y1:0
})
},
red:function(){
this.setData({
linecolor:"red",
yincang:true
})
cvs.setStrokeStyle(this.data.linecolor);
},
yellow:function(){
this.setData({
linecolor:"yellow",
yincang:true
})
cvs.setStrokeStyle(this.data.linecolor);
},
blue:function(){
this.setData({
linecolor:"#3AC2F1",
yincang:true
})
cvs.setStrokeStyle(this.data.linecolor);
},
eee:function(){
this.setData({
linecolor:"green",
yincang:true
})
cvs.setStrokeStyle(this.data.linecolor);
},
black:function(){
this.setData({
linecolor:"black",
yincang:true
})
cvs.setStrokeStyle(this.data.linecolor)
},
water:function(){
this.setData({
linecolor:"#88ada6",
yincang:true
})
cvs.setStrokeStyle(this.data.linecolor)
},
zi:function(){
this.setData({
linecolor:"#725e82",
yincang:true
})
cvs.setStrokeStyle(this.data.linecolor)
},
changecolor:function(){
this.setData({
yincang:!this.data.yincang,
animate: "animation:zoomIn 1s"
})
},
changewidth:function(){
this.setData({
yincang2:!this.data.yincang2,
animate2: "animation:zoomIn 1s"
})
},
xi:function(){
this.setData({
linewidth:1,
yincang2:true
})
cvs.setLineWidth(this.data.linewidth)
},
zx:function(){
this.setData({
linewidth:2,
yincang2:true
})
cvs.setLineWidth(this.data.linewidth)
},
pt:function(){
this.setData({
linewidth:3,
yincang2:true
})
cvs.setLineWidth(this.data.linewidth)
},
zc:function(){
this.setData({
linewidth:4,
yincang2:true
})
cvs.setLineWidth(this.data.linewidth)
},
c:function(){
this.setData({
linewidth:5,
yincang2:true
})
cvs.setLineWidth(this.data.linewidth)
}
})

请我喝杯咖啡吧~

支付宝
微信