-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsketchpad-touch.html
398 lines (337 loc) · 12.2 KB
/
sketchpad-touch.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<html>
<head>
<title>Coloful Vision Sketchpad</title>
<script src="tutorials.js"></script>
<script src="ttsLocal.js"></script>
<script type="text/javascript">
// Stack of last objects
var stack = [];
var arrayOfStack = [];
// Variables for referencing the canvas and 2dcanvas context
var canvas, ctx;
// Variables to keep track of the mouse position and left-button status
var mouseX, mouseY, mouseDown = 0, MOUSE_SIZE = 5, drawMode = true;
// Variables to keep track of the touch position
var touchX, touchY;
// Draws a dot at a specific position on the supplied canvas name
// Parameters are: A canvas context, the x position, the y position, the size of the dot
function drawDot(ctx, x, y, size, isUndo) {
// Let's use black by setting RGB values to 0, and 255 alpha (completely opaque)
r = 0; g = 0; b = 0; a = 255;
if (isUndo) {
r = 255; g = 255; b = 255; a = 255;
size++;
}
// Select a fill style
ctx.fillStyle = "rgba(" + r + "," + g + "," + b + "," + (a / 255) + ")";
// Draw a filled circle
ctx.beginPath();
ctx.arc(x, y, size, 0, Math.PI * 2, true);
ctx.closePath();
ctx.fill();
}
// Clear the canvas context using the canvas width and height
function clearCanvas(canvas, ctx, clear = true) {
if (tutorialMode) {
resetCounters();
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (clear) {
setInstruction('');
}
addRadioButtons();
}
// Undo the canvas context using the canvas width and height
function undo(canvas, ctx) {
if (arrayOfStack.length) {
console.dir(arrayOfStack, { depth: null });
var s = arrayOfStack.pop();
for (const point of s) {
drawDot(ctx, point.x, point.y, MOUSE_SIZE, true);
}
for (const i in s) {
smoothen(ctx, s, '#FFFFFF');
}
}
}
function smoothen(ctx, stack, color) {
ctx.beginPath();
ctx.strokeStyle = '#000000';
if (color) {
ctx.strokeStyle = color;
}
// var previousPoint = stack[0];
// ctx.moveTo(previousPoint.x, previousPoint.y);
for (var i = 1; i < stack.length - 2; i++) {
var c = (stack[i].x + stack[i + 1].x) / 2;
var d = (stack[i].y + stack[i + 1].y) / 2;
ctx.quadraticCurveTo(stack[i].x, stack[i].y, c, d);
}
// For the last 2 points
ctx.quadraticCurveTo(
stack[i].x,
stack[i].y,
stack[i + 1].x,
stack[i + 1].y
);
ctx.lineWidth = MOUSE_SIZE;
ctx.stroke();
}
// Keep track of the mouse button being pressed and draw a dot at current location
function sketchpad_mouseDown(e) {
getMousePos(e);
checkForContinuePointMode(mouseX, mouseY);
if (!drawMode) return;
mouseDown = 1;
stack = [];
stack.push({ x: mouseX, y: mouseY });
drawDot(ctx, mouseX, mouseY, MOUSE_SIZE, false);
setInstruction('');
}
function setInstruction(instruction) {
var x = document.getElementById("instructionVar");
x.innerText = instruction;
if (instruction != '') {
getAudioFromLocal(instruction);
}
}
function appendInstruction(instruction) {
var x = document.getElementById("instructionVar");
x.innerText += instruction;
}
// Keep track of the mouse button being released
function sketchpad_mouseUp(e) {
getMousePos(e);
if (!drawMode) return;
if (mouseDown != 0) {
for (const point of stack) {
drawDot(ctx, point.x, point.y, MOUSE_SIZE, true);
}
arrayOfStack.push(stack);
if (stack.length > 1) {
smoothen(ctx, stack);
mouseDown = 0;
checkForTutorialScore(stack);
}
}
}
// Keep track of the mouse position and draw a dot if mouse button is currently pressed
function sketchpad_mouseMove(e) {
if (!drawMode && mouseDown != 0 && tutorialModeCheck() && currentTutorial.continueFromLast
&& lastKnownCoord.x > -1 && lastKnownCoord.y > -1) {
checkIfCoordInRange(mouseX, mouseY, lastKnownCoord);
}
if (!drawMode) return;
// Update the mouse co-ordinates when moved
getMousePos(e);
// Draw a dot if the mouse button is currently being pressed
if (mouseDown == 1) {
move();
}
}
// Get the current mouse position relative to the top-left of the canvas
function getMousePos(e) {
if (!e)
var e = event;
if (e.offsetX) {
mouseX = e.offsetX;
mouseY = e.offsetY;
}
else if (e.layerX) {
mouseX = e.layerX;
mouseY = e.layerY;
}
}
// Draw something when a touch start is detected
// function sketchpad_touchStart() {
// // Update the touch co-ordinates
// getTouchPos();
// drawDot(ctx, touchX, touchY, MOUSE_SIZE, false);
// if (stack.length) {
// arrayOfStack.push(stack);
// stack = [];
// }
// stack.push({ x: touchX, y: touchY });
// // Prevents an additional mousedown event being triggered
// event.preventDefault();
// }
// Draw something and prevent the default scrolling when touch movement is detected
// function sketchpad_touchMove(e) {
// // Update the touch co-ordinates
// getTouchPos(e);
// During a touchmove event, unlike a mousemove event, we don't need to check if the touch is engaged, since there will always be contact with the screen by definition.
// move();
// drawDot(ctx, touchX, touchY, MOUSE_SIZE, false);
// stack.push({ x: mouseX, y: mouseY });
// // Prevent a scrolling action as a result of this touchmove triggering.
// event.preventDefault();
// }
// Get the touch position relative to the top-left of the canvas
// When we get the raw values of pageX and pageY below, they take into account the scrolling on the page
// but not the position relative to our target div. We'll adjust them using "target.offsetLeft" and
// "target.offsetTop" to get the correct values in relation to the top left of the canvas.
// function getTouchPos(e) {
// if (!e)
// var e = event;
// if (e.touches) {
// if (e.touches.length == 1) { // Only deal with one finger
// var touch = e.touches[0]; // Get the information for finger #1
// touchX = touch.pageX - touch.target.offsetLeft;
// touchY = touch.pageY - touch.target.offsetTop;
// }
// }
// }
function isDrawMode(e) {
if (e) {
console.dir(e, { depth: null });
if (e.code && e.code === "Space") {
drawMode = !drawMode;
var modeAsString = drawMode ? "Draw" : "Placement";
document.getElementById("mode").innerHTML = "<b>Current Mode</b>: " + modeAsString;
}
if (e.keyCode) {
if (e.keyCode >= 49 && e.keyCode <= 57) {
document.getElementById(`tutorial${e.keyCode - 48}`).click();
}
}
}
}
function move() {
drawDot(ctx, mouseX, mouseY, MOUSE_SIZE, false);
stack.push({ x: mouseX, y: mouseY });
checkForLineLength(stack);
}
// Set-up the canvas and add our event handlers after the page has loaded
function init() {
// // resize the canvas to fill browser window dynamically
window.addEventListener('resize', resizeCanvas, false);
// Get the specific canvas element from the HTML document
canvas = document.getElementById('sketchpad');
// If the browser supports the canvas tag, get the 2d drawing context for this canvas
if (canvas.getContext)
ctx = canvas.getContext('2d');
// Check that we have a valid context to draw on/with before adding event handlers
if (ctx) {
// React to mouse events on the canvas, and mouseup on the entire document
canvas.addEventListener('mousedown', sketchpad_mouseDown, false);
canvas.addEventListener('mousemove', sketchpad_mouseMove, false);
window.addEventListener('mouseup', sketchpad_mouseUp, false);
// React to touch events on the canvas
// canvas.addEventListener('touchstart', sketchpad_touchStart, false);
// canvas.addEventListener('touchmove', sketchpad_touchMove, false);
}
document.addEventListener('keypress', isDrawMode, false);
addRadioButtons();
resizeCanvas();
}
function resizeCanvas() {
canvas.width = window.innerWidth * 0.75;
canvas.height = window.innerHeight;
}
function smoothenArc() {
line1Stack = arrayOfStack[arrayOfStack.length - 3];
line2Stack = arrayOfStack[arrayOfStack.length - 2];
line3Stack = arrayOfStack[arrayOfStack.length - 1];
// Call Undo Thrice and redraw the arc
undo(canvas, ctx);
undo(canvas, ctx);
undo(canvas, ctx);
// Re draw smoothened curve
ctx.beginPath();
ctx.strokeStyle = '#000000';
ctx.moveTo(line1Stack[0].x, line1Stack[0].y);
line2StackMidPoint = Math.ceil(line2Stack.length / 2);
MidX = 2 * line2Stack[line2StackMidPoint].x - line1Stack[0].x / 2 - line3Stack[line3Stack.length - 1].x / 2;
MidY = 2 * line2Stack[line2StackMidPoint].y - line1Stack[0].y / 2 - line3Stack[line3Stack.length - 1].y / 2;
ctx.quadraticCurveTo(MidX, MidY, line3Stack[line3Stack.length - 1].x, line3Stack[line3Stack.length - 1].y);
ctx.lineWidth = MOUSE_SIZE;
ctx.stroke();
// setInstruction("Arc Smoothened");
}
function executeTutorial(id) {
doTutorial(canvas, ctx, id);
}
function addRadioButtons() {
if (!tutorials || tutorials.length === 0) {
initializeTutorials();
}
var tutorialsDiv = document.getElementById("tutorials");
var innerHTML = '';
tutorialsDiv.innerHTML = `${innerHTML}`;
for (var i = 0; i < tutorials.length; i++) {
let tutorial = tutorials[i];
innerHTML = innerHTML.concat(`<input id="tutorial${tutorial.id}" type="radio" name="tutorial" value="${tutorial.id}" onclick="executeTutorial(${tutorial.id})"> ${tutorial.name} </input><br>`);
}
tutorialsDiv.innerHTML = `${innerHTML}`;
}
</script>
<style>
/* Some CSS styling */
#sketchpadapp {
/* Prevent nearby text being highlighted when accidentally dragging mouse outside confines of the canvas */
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.leftside {
float: left;
width: 20%;
height: 100%;
background-color: #def;
padding: 1%;
border-radius: 1%;
}
.rightside {
float: right;
margin-left: 1%;
}
#sketchpad {
float: left;
border: 2px solid #888;
border-radius: 1%;
/* position: relative; */
height: 100%;
width: 100%;
/* Necessary for correct mouse co-ords in Firefox */
}
#clearbutton {
font-size: 15px;
padding: 10px;
-webkit-appearance: none;
background: #eee;
border: 1px solid #888;
}
</style>
</head>
<body>
<div id="sketchpadapp">
<div class="leftside">
<h3>Colorful Vision SketchPad</h3>
<p>
- Empowering a blind person to draw
</p><br/>
<input type="submit" value="Clear Sketchpad" id="clearbutton" onclick="clearCanvas(canvas,ctx);">
<input type="submit" value="Undo" id="clearbutton" onclick="undo(canvas,ctx);"><br/><br/>
<label for="mode" id="mode"><b>Current Mode</b>: Draw</label><br/><br/>
<!-- <input type="submit" value="Start Tutorial" id="clearbutton" onclick="startTutorial(canvas,ctx);">
<input type="submit" value="Reset Tutorials" id="clearbutton" onclick="resetTutorials();">
<input type="submit" value="Do Tutorial" id="clearbutton" onclick="doTutorial(canvas,ctx,6);"> -->
<p><b>Instructions</b>: <var id="instructionVar"></var></p>
<div>
<label for="tutorials"> <b>Tutorials Set</b> </lable>
<div id="tutorials">
</div>
</div>
</div>
<div class="rightside">
<canvas id="sketchpad"> </canvas>
</div>
</div>
<script>
init();
</script>
</body>
</html>