-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
179 lines (146 loc) · 3.39 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
var bgcolor = "white";
var strokeinp = "black";
var strokeWeightinp = 5;
var database;
var drawing = [];
var currentPath = [];
var isDrawing = false;
askPrompt();
function setup() {
canvas = createCanvas(windowWidth, windowHeight);
canvas.mousePressed(startPath);
canvas.parent('canvascontainer');
canvas.mouseReleased(endPath);
var saveButton = select('#saveButton');
saveButton.mousePressed(saveDrawing);
var changeSettings = createButton('change Settings');
changeSettings.mousePressed(askPrompt);
var clearButton = select('#clearButton');
clearButton.mousePressed(clearDrawing);
database = firebase.database();
var params = getURLParams();
console.log(params);
if (params.id) {
console.log(params.id);
showDrawing(params.id);
}
var ref = database.ref('drawings');
ref.on('value', gotData, errData);
}
function startPath() {
isDrawing = true;
currentPath = [];
drawing.push(currentPath);
}
function endPath() {
isDrawing = false;
}
//FunctionDraw
//FunctionDraw
//FunctionDraw
//FunctionDraw
//FunctionDraw
//FunctionDraw
//FunctionDraw
//FunctionDraw
//FunctionDraw
//FunctionDraw
//FunctionDraw
//FunctionDraw
//FunctionDraw
//FunctionDraw
//FunctionDraw
function draw() {
background(bgcolor);
if (isDrawing) {
var point = {
x: mouseX,
y: mouseY
};
currentPath.push(point);
}
stroke(strokeinp);
strokeWeight(strokeWeightinp);
noFill();
for (var i = 0; i < drawing.length; i++) {
var path = drawing[i];
beginShape();
for (var j = 0; j < path.length; j++) {
vertex(path[j].x, path[j].y);
}
endShape();
}
}
//FunctionDraw
//FunctionDraw
//FunctionDraw
//FunctionDraw
//FunctionDraw
//FunctionDraw
//FunctionDraw
//FunctionDraw
//FunctionDraw
//FunctionDraw
//FunctionDraw
//FunctionDraw
//FunctionDraw
//FunctionDraw
//FunctionDraw
function saveDrawing() {
var ref = database.ref('drawings');
var data = {
name: 'Pranav',
drawing: drawing
};
var result = ref.push(data, dataSent);
console.log(result.key);
function dataSent(err, status) {
console.log(status);
}
}
function gotData(data) {
// clear the listing
var elts = selectAll('.listing');
for (var i = 0; i < elts.length; i++) {
elts[i].remove();
}
var drawings = data.val();
var keys = Object.keys(drawings);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
//console.log(key);
var li = createElement('li', '');
li.class('listing');
var ahref = createA('#', key);
ahref.mousePressed(showDrawing);
ahref.parent(li);
var perma = createA('?id=' + key, 'permalink');
perma.parent(li);
perma.style('padding', '4px');
li.parent('drawinglist');
}
}
function errData(err) {
console.log(err);
}
function showDrawing(key) {
//console.log(arguments);
if (key instanceof MouseEvent) {
key = this.html();
}
var ref = database.ref('drawings/' + key);
ref.once('value', oneDrawing, errData);
function oneDrawing(data) {
var dbdrawing = data.val();
drawing = dbdrawing.drawing;
//console.log(drawing);
}
}
function clearDrawing() {
drawing = [];
}
function askPrompt(){
bgcolor = prompt("What do you want the background color to be as(example red, blue, green, black, etc)", "white");
strokeinp = prompt("What do you want the stroke color to be", "black");
strokeWeightinp = prompt("What to you want the thickness oif the stroke to be", 5)
}