質問編集履歴

1

修正

2018/08/13 18:00

投稿

aiueoaiueoaiue
aiueoaiueoaiue

スコア94

test CHANGED
File without changes
test CHANGED
@@ -17,196 +17,6 @@
17
17
  ###サンプルコード
18
18
 
19
19
  ```JavaScript
20
-
21
- var canvas = document.getElementById('canvassample'),
22
-
23
- ctx = canvas.getContext('2d'),
24
-
25
- moveflg = 0,
26
-
27
- Xpoint,
28
-
29
- Ypoint,
30
-
31
- currentCanvas,
32
-
33
- temp = [];
34
-
35
-
36
-
37
- //初期値(サイズ、色、アルファ値)の決定
38
-
39
- var defSize = 7,
40
-
41
- defColor = "#555";
42
-
43
-
44
-
45
-
46
-
47
- // ストレージの初期化
48
-
49
- var myStorage = localStorage;
50
-
51
- window.onload = initLocalStorage();
52
-
53
-
54
-
55
-
56
-
57
- // PC対応
58
-
59
- canvas.addEventListener('mousedown', startPoint, false);
60
-
61
- canvas.addEventListener('mousemove', movePoint, false);
62
-
63
- canvas.addEventListener('mouseup', endPoint, false);
64
-
65
- // スマホ対応
66
-
67
- canvas.addEventListener('touchstart', startPoint, false);
68
-
69
- canvas.addEventListener('touchmove', movePoint, false);
70
-
71
- canvas.addEventListener('touchend', endPoint, false);
72
-
73
-
74
-
75
- function startPoint(e){
76
-
77
- e.preventDefault();
78
-
79
- ctx.beginPath();
80
-
81
- console.log(e.clientX);
82
-
83
-
84
-
85
-
86
-
87
- // 矢印の先っぽから始まるように調整
88
-
89
- Xpoint = e.clientX-8;
90
-
91
- Ypoint = e.clientY-8;
92
-
93
-
94
-
95
- ctx.moveTo(Xpoint, Ypoint);
96
-
97
- }
98
-
99
-
100
-
101
- function movePoint(e){
102
-
103
- if(e.buttons === 1 || e.witch === 1 || e.type == 'touchmove')
104
-
105
- {
106
-
107
- Xpoint = e.pageX-8;
108
-
109
-  Ypoint = e.pageY-8;
110
-
111
- moveflg = 1;
112
-
113
-
114
-
115
- ctx.lineTo(Xpoint, Ypoint);
116
-
117
- ctx.lineCap = "round";
118
-
119
- ctx.lineWidth = defSize * 2;
120
-
121
- ctx.strokeStyle = defColor;
122
-
123
- ctx.stroke();
124
-
125
-
126
-
127
- }
128
-
129
- }
130
-
131
-
132
-
133
- function endPoint(e)
134
-
135
- {
136
-
137
- if(moveflg === 0)
138
-
139
- {
140
-
141
- ctx.lineTo(Xpoint-1, Ypoint-1);
142
-
143
- ctx.lineCap = "round";
144
-
145
- ctx.lineWidth = defSize * 2;
146
-
147
- ctx.strokeStyle = defColor;
148
-
149
- ctx.stroke();
150
-
151
-
152
-
153
- }
154
-
155
- moveflg = 0;
156
-
157
-
158
-
159
- setLocalStoreage();
160
-
161
- }
162
-
163
-
164
-
165
- function resetCanvas() {
166
-
167
- ctx.clearRect(0, 0, ctx.canvas.clientWidth, ctx.canvas.clientHeight);
168
-
169
- }
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
- function initLocalStorage(){
178
-
179
- myStorage.setItem("__log", JSON.stringify([]));
180
-
181
- }
182
-
183
- function setLocalStoreage(){
184
-
185
- var png = canvas.toDataURL();
186
-
187
- var logs = JSON.parse(myStorage.getItem("__log"));
188
-
189
-
190
-
191
- setTimeout(function(){
192
-
193
- logs.unshift({png});
194
-
195
-
196
-
197
- myStorage.setItem("__log", JSON.stringify(logs));
198
-
199
-
200
-
201
- currentCanvas = 0;
202
-
203
- temp = [];
204
-
205
-
206
-
207
- }, 0);
208
-
209
- }
210
20
 
211
21
 
212
22