-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw-dry.js
90 lines (67 loc) · 1.81 KB
/
draw-dry.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
window.onload = function() {
document.ontouchmove = function(e){ e.preventDefault(); }
var draw = {
fill: "#000000",
stroke: "#000000",
clear: "#ffffff",
size: 5,
cap: 'round',
join: 'round',
width: 300,
height: 300
}
var canvas = document.getElementById('main');
var canvastop = canvas.offsetTop
var context = canvas.getContext("2d");
var lastx;
var lasty;
function clear() {
context.fillStyle = draw.clear;
context.rect(0, 0, draw.width, draw.height);
context.fill();
}
function path( moves ) {
context.beginPath();
context.strokeStyle = draw.stroke;
context.fillStyle = draw.fill;
context.lineCap = draw.cap;
context.lineJoin = draw.join;
context.lineWidth = draw.size;
moves()
context.fill();
context.stroke();
context.closePath();
}
function dot(x,y) {
path(function(){
context.arc(x,y,1,0,Math.PI*2,true);
});
}
function line(fromx,fromy, tox,toy) {
path(function(){
context.moveTo(fromx, fromy);
context.lineTo(tox, toy);
});
}
function position(event,action) {
event.preventDefault();
var newx = event.touches[0].clientX;
var newy = event.touches[0].clientY - canvastop;
action(lastx,lasty, newx,newy)
lastx = newx;
lasty = newy;
}
canvas.ontouchstart = function(event){
position(event,function(lastx,lasty, newx,newy){
dot(newx,newy);
})
}
canvas.ontouchmove = function(event){
position(event,function(lastx,lasty, newx,newy){
line(lastx,lasty, newx,newy);
})
}
var clearButton = document.getElementById('clear');
clearButton.onclick = clear;
clear();
}