-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
164 lines (146 loc) · 5.63 KB
/
sketch.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
158
159
160
161
162
163
164
/////////////////////////////////////////////
// 4cm^2 canvas for drawing
// From professor Xiang Chen's isketch example: https://github.com/ucla-hci/isketch.git
/////////////////////////////////////////////
var ISKETCH = ISKETCH || {}
ISKETCH.WIDTH = 50;
ISKETCH.HEIGHT = 50;
$(document).ready(() => {
console.log('Welcome to iSketch!')
// initialize the canvas
$('#cvsMain')[0].width = ISKETCH.WIDTH;
$('#cvsMain')[0].height = ISKETCH.HEIGHT;
$('#cvsMain').css('background-color', '#eeeeee');
ISKETCH.context = $('#cvsMain')[0].getContext('2d');
ISKETCH.context.strokeStyle = "#df4b26";
ISKETCH.context.lineJoin = "round";
ISKETCH.context.lineWidth = 3;
ISKETCH.context.font = "12px Arial";
ISKETCH.drawSections();
show_table();
// add input event handlers
$('#cvsMain').on('mousedown', ISKETCH.canvasMouseDown);
$('#cvsMain').on('mousemove', ISKETCH.canvasMouseMove);
$('#cvsMain').on('mouseup', ISKETCH.canvasMouseUp);
//Initialize gesture recognizer
ISKETCH.recognizer = new DollarRecognizer();
})
ISKETCH.drawSections = function(){
//divide canvas into 4 sections by drawing 2 lines
ISKETCH.context.strokeStyle = "#9C9C9C";
ISKETCH.context.lineWidth = 1;
ISKETCH.context.beginPath();
ISKETCH.context.moveTo(ISKETCH.WIDTH/2,0);
ISKETCH.context.lineTo(ISKETCH.WIDTH/2,ISKETCH.HEIGHT);
ISKETCH.context.stroke();
ISKETCH.context.moveTo(0,ISKETCH.HEIGHT/2);
ISKETCH.context.lineTo(ISKETCH.WIDTH,ISKETCH.HEIGHT/2);
ISKETCH.context.stroke();
ISKETCH.context.closePath();
ISKETCH.context.fillText("1", (ISKETCH.WIDTH/4 - 4), (ISKETCH.HEIGHT/4 + 4));
ISKETCH.context.fillText("2", (3*ISKETCH.WIDTH/4 - 4), (ISKETCH.HEIGHT/4 + 4));
ISKETCH.context.fillText("3", (ISKETCH.WIDTH/4 - 4), (3*ISKETCH.HEIGHT/4 + 4));
ISKETCH.context.fillText("4", (3*ISKETCH.WIDTH/4 - 4), (3*ISKETCH.HEIGHT/4 + 4));
ISKETCH.context.strokeStyle = "#df4b26";
ISKETCH.context.lineWidth = 5;
}
ISKETCH.canvasMouseDown = function (e) {
ISKETCH.context.clearRect(0, 0, $('#cvsMain').width(), $('#cvsMain').height());
ISKETCH.drawSections();
ISKETCH.context.beginPath();
let rect = $('#cvsMain')[0].getBoundingClientRect();
let x = e.clientX - rect.left, y = e.clientY - rect.top;
ISKETCH.context.moveTo(x, y);
ISKETCH.context.stroke();
ISKETCH.isDragging = true;
// create an empty array to store the user's mouse coordinates as Point objects
// add the mouse down coordinates to this array
ISKETCH.coords = [];
ISKETCH.coords.push(new Point(x,y));
}
ISKETCH.canvasMouseMove = function (e) {
if (!ISKETCH.isDragging) return;
let rect = $('#cvsMain')[0].getBoundingClientRect();
let x = e.clientX - rect.left, y = e.clientY - rect.top
ISKETCH.context.lineTo(x, y);
ISKETCH.context.moveTo(x, y);
ISKETCH.context.stroke();
// Add the mouse move coordinates to the array
ISKETCH.coords.push(new Point(x,y));
}
ISKETCH.canvasMouseUp = function (e) {
ISKETCH.isDragging = false;
ISKETCH.context.closePath();
// Add the mouse up coordinates to the array
let rect = $('#cvsMain')[0].getBoundingClientRect();
let x = e.clientX - rect.left, y = e.clientY - rect.top;
ISKETCH.coords.push(new Point(x,y));
// Print the array
for(let i = 0; i < ISKETCH.coords.length; i++) {
console.log(ISKETCH.coords[i].X + ", " + ISKETCH.coords[i].Y);
}
// Recognize the gesture
var gesture = ISKETCH.recognizer.Recognize(ISKETCH.coords, true);
//Write the gesture below the canvas
document.getElementById('gesture').innerHTML = gesture.Name;
//Act based on the recognized gesture:
if(gesture.Name === "caret") {
//speed up the board
set_scroll_update_interval(UPDATE_INTERVAL-200);
}
else if(gesture.Name === "v") {
//slow down the board
set_scroll_update_interval(UPDATE_INTERVAL+200);
}
else if(gesture.Name === "circle") {
//switch between emojis and text
change_table();
}
else if(gesture.Name === "z") {
//type sleepy face
document.getElementById('message').innerHTML = document.getElementById('message').innerHTML + '😴';
}
else if(gesture.Name === "space") {
//type space
document.getElementById('message').append(' ');
}
else if(gesture.Name === "backspace") {
//type backspace
type_backspace();
}
else if(gesture.Name === "zigzag") {
//type sad squiggly face
document.getElementById('message').innerHTML = document.getElementById('message').innerHTML + '😖';
}
else if(gesture.Name == "smile") {
//type happy face
document.getElementById('message').innerHTML = document.getElementById('message').innerHTML + '😀';
}
else if(gesture.Name == "frown") {
//type happy face
document.getElementById('message').innerHTML = document.getElementById('message').innerHTML + '😞';
}
else if(gesture.Name === "No match.") {
//If they drew a point input
if(ISKETCH.coords.length <= 8) {
document.getElementById('gesture').innerHTML = "Point";
//check the location of the first point and type corresponding letter
if(ISKETCH.coords[0].X < ISKETCH.WIDTH/2 && ISKETCH.coords[0].Y < ISKETCH.HEIGHT/2) {
//bottom right
typeLetter(0);
}
else if(ISKETCH.coords[0].X >= ISKETCH.WIDTH/2 && ISKETCH.coords[0].Y < ISKETCH.HEIGHT/2) {
//top left
typeLetter(1);
}
else if(ISKETCH.coords[0].X < ISKETCH.WIDTH/2 && ISKETCH.coords[0].Y >= ISKETCH.HEIGHT/2) {
//top right
typeLetter(2);
}
else if(ISKETCH.coords[0].X >= ISKETCH.WIDTH/2 && ISKETCH.coords[0].Y >= ISKETCH.HEIGHT/2) {
//bottom left
typeLetter(3);
}
}
}
}