-
Notifications
You must be signed in to change notification settings - Fork 0
/
stampTool.js
322 lines (288 loc) · 11.4 KB
/
stampTool.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
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
function StampTool() {
// Sets the icon and name for the stamp tool
this.icon = "assets/stamp.png";
this.name = "stamp";
var stamps = [];
// Set up the sliders
var stampSizeSlider;
var stampNumberSlider;
var stampSpreadSlider;
var stampSize;
var stampNumber;
var stampSpread;
var stampSelect;
var defaultColors;
var self = this;
this.stamp;
// The draw method for the stamp tool
this.draw = function() {
//display the last save state of pixels
updatePixels();
// set the stamp origin to center
var stampX = mouseX - stampSize / 2;
var stampY = mouseY - stampSize / 2;
for(var i = 0; i < stampNumber; i++)
{
// Draw multiple stamps based on offset array
if(stampNumber > 1 && i < stamps.length)
{
drawStamp(this.stamp, mouseX + stamps[i][0],mouseY + stamps[i][1]);
}
// Preview the stamp on screen before clicking
drawStamp(this.stamp, mouseX,mouseY);
// Draw the stamp to the canvas
if(mouseIsPressed && helpers.mousePressOnCanvas()) {
// Save the drawn image to the canvas
loadPixels();
}
}
}
// Draws a specified stamp at the given x,y position
var drawStamp = function(stamp,xPos, yPos) {
// Set the scale based on selected stamp size
var scale = stampSize / 10;
// If default colors is not selected, use selected colors
var strokeColor = colourP.selectedStrokeColour;
var fillColor = colourP.selectedFillColour;
// Alien head stamp
if(stamp == "Alien")
{
// Use default colours if option checked
if(defaultColors.checked())
{
strokeColor = "rgb(0,225,100)";
fillColor = "rgb(0,255,100)";
}
push();
noStroke();
fill(fillColor);
ellipse(xPos,yPos,28 *scale,35 *scale);
//eyes
fill(255,255,255);
ellipse(xPos-5*scale,yPos,7*scale,12*scale);
fill(0,0,0);
ellipse(xPos-5*scale,yPos,5*scale,5*scale);
fill(255,255,255);
ellipse(xPos+5*scale,yPos,7*scale,12*scale);
fill(0,0,0);
ellipse(xPos+5*scale,yPos,5*scale,5*scale);
//antenna
strokeWeight(1);
stroke(strokeColor);
line(xPos-8*scale,yPos-10*scale,xPos-15*scale,yPos-20*scale);
line(xPos+8*scale,yPos-10*scale,xPos+15*scale,yPos-20*scale);
noStroke();
//mouth
fill(0,0,0);
ellipse(xPos, yPos+12*scale, 8*scale,3*scale);
fill(fillColor);
rect(xPos-6*scale, yPos+9*scale, 12*scale,3*scale, 0,0,14*scale,10*scale);
pop();
}
// Star stamp
if(stamp == "Star")
{
if(defaultColors.checked())
{
strokeColor = "rgb(0,0,0)";
fillColor = "rgb(255,255,0)";
}
push();
stroke(strokeColor);
fill(fillColor);
beginShape();
vertex(xPos,yPos-9 * scale);
vertex(xPos+2 * scale,yPos-3 * scale);
vertex(xPos+8 * scale,yPos-3 * scale);
vertex(xPos+3 * scale,yPos+1 * scale);
vertex(xPos+6 * scale,yPos+7 * scale);
vertex(xPos,yPos+3 * scale);
vertex(xPos-6 * scale,yPos+7 * scale);
vertex(xPos-3 * scale,yPos+1 * scale);
vertex(xPos-8 * scale,yPos-3 * scale);
vertex(xPos-2 * scale,yPos-3 * scale);
vertex(xPos,yPos-9 * scale);
endShape();
pop();
}
// Moon Stamp
if(stamp == "Moon")
{
if(defaultColors.checked())
{
strokeColor = "rgb(0,0,0)";
fillColor = "rgb(250,250,200)";
}
push();
stroke(strokeColor);
fill(fillColor);
beginShape();
vertex(xPos -15 *scale, yPos-25*scale);
bezierVertex(xPos+35 *scale, yPos-45 *scale, xPos+35 *scale, yPos+30 *scale, xPos - 15*scale, yPos+30 *scale);
bezierVertex(xPos+15 *scale, yPos+25 *scale, xPos+15 *scale, yPos-15 *scale, xPos -15*scale, yPos-25*scale);
endShape();
pop();
}
// Face stamp
if(stamp == "Face")
{
if(defaultColors.checked())
{
strokeColor = "rgb(0,0,0)";
fillColor = "rgb(255,255,0)";
}
push();
stroke(strokeColor);
fill(fillColor);
ellipse(xPos,yPos,25*scale);
fill(255,255,255);
ellipse(xPos-5*scale,yPos-5*scale, 5*scale);
ellipse(xPos+5*scale,yPos-5*scale, 5*scale);
fill(0,0,0);
ellipse(xPos-5*scale,yPos-5*scale, 1*scale);
ellipse(xPos+5*scale,yPos-5*scale, 1*scale);
noFill();
beginShape();
vertex(xPos - 6*scale, yPos);
curveVertex(xPos-4*scale, yPos +5*scale);
curveVertex(xPos+4*scale, yPos +5*scale);
vertex(xPos + 6*scale, yPos);
endShape();
pop();
}
// Cloud stamp
if(stamp == "Cloud")
{
if(defaultColors.checked())
{
strokeColor = "rgb(200,200,200)";
fillColor = "rgb(255,255,255)";
}
push();
noStroke();
fill(strokeColor);
ellipse(xPos,yPos +1 *scale, 28 *scale,10 *scale);
fill(fillColor);
ellipse(xPos,yPos, 20 *scale,10 *scale);
ellipse(xPos +8 *scale,yPos, 12 *scale,6 *scale);
ellipse(xPos -8 *scale,yPos, 12 *scale,6 *scale);
pop();
}
// Tree stamp
if(stamp == "Tree")
{
if(defaultColors.checked())
{
strokeColor = "rgb(150,75,0)";
fillColor = "rgb(10,200,0)";
}
push();
noStroke();
fill(strokeColor);
rect(xPos,yPos,5 *scale,20 *scale);
fill(fillColor);
ellipse(xPos +2 *scale,yPos +2 *scale,32 *scale,16 *scale);
ellipse(xPos +2 *scale,yPos - 9 *scale,22 *scale,16 *scale);
triangle(xPos - 5 *scale,yPos - 13 *scale,xPos + 10 *scale,yPos - 13 *scale,xPos + 2 *scale,yPos - 25 *scale);
pop();
}
// Grass Stamp
if(stamp == "Grass")
{
if(defaultColors.checked())
{
strokeColor = "rgb(60,176,17)";
fillColor = "rgb(46,148,71)";
}
push();
stroke(strokeColor);
fill(fillColor);
beginShape();
vertex(xPos -20 *scale, yPos-30*scale);
bezierVertex(xPos+10 *scale, yPos-20 *scale, xPos+10 *scale, yPos+20 *scale, xPos, yPos+15 *scale);
bezierVertex(xPos, yPos+15 *scale, xPos+10 *scale, yPos, xPos - 20*scale, yPos-30 *scale);
endShape();
// Highlight
fill(strokeColor);
beginShape();
vertex(xPos -20 *scale, yPos-30*scale);
bezierVertex(xPos+5 *scale, yPos-20 *scale, xPos+5 *scale, yPos+20 *scale, xPos, yPos+15 *scale);
bezierVertex(xPos, yPos+15 *scale, xPos+10 *scale, yPos, xPos - 20*scale, yPos-30 *scale);
endShape();
pop();
}
};
// Resets the stamp offset array
var updateStamps = function() {
stamps = [];
var stampXOffset;
var stampYOffset;
if(stampNumber > 1)
{
for(var i = 0; i < stampNumber; i++)
{
stampXOffset = random(-10 * stampSpread,10 * stampSpread);
stampYOffset = random(-10 * stampSpread,10 * stampSpread);
stamps.push([stampXOffset, stampYOffset]);
}
}
};
// Clear the options when tool is unselected
this.unselectTool = function() {
//clear options
select(".options").html("");
select(".secondaryOptions").html("");
};
// Creates the options menu for selecting the desired stamp and the size
this.populateOptions = function() {
stampSelect = createSelect();
defaultColors = createCheckbox('Default Colours', true);
stampSelect.option("Alien");
stampSelect.option("Star");
stampSelect.option("Moon");
stampSelect.option("Cloud");
stampSelect.option("Grass");
stampSelect.option("Tree");
stampSelect.option("Face");
this.stamp = stampSelect.value();
stampSelect.changed(function(){
self.stamp = stampSelect.value();
})
// Create the sliders
stampSizeSlider = createSlider(5, 50, 20);
stampNumberSlider = createSlider(1, 10, 1);
stampSpreadSlider = createSlider(0,20,0);
// Assign stamp values based on sliders
stampSize = stampSizeSlider.value();
stampNumber = stampNumberSlider.value();
stampSpread = stampSpreadSlider.value();
// Add event listeners.
stampNumberSlider.changed(function(){
stampNumber = stampNumberSlider.value();
updateStamps();
});
stampSpreadSlider.changed(function() {
stampSpread = stampSpreadSlider.value();
updateStamps();
});
stampSizeSlider.changed(function() {
stampSize = stampSizeSlider.value();
updateStamps();
})
// Add the dropdown to options
select(".options").html(
"<div id='stampSelect'>Select Stamp</div>");
stampSelect.parent("#stampSelect");
defaultColors.parent("#stampSelect",true);
// Add the sliders to Secondary Options
select(".secondaryOptions").html(
"<div id='sizeSlider'>Size of Stamp</div>");
select(".secondaryOptions").html(
"<div id='numberSlider'>Number of Stamps</div>",true);
select(".secondaryOptions").html(
"<div id='spreadSlider'>Stamp Spread</div>",true);
stampSizeSlider.parent("#sizeSlider");
stampNumberSlider.parent("#numberSlider");
stampSpreadSlider.parent("#spreadSlider");
}
}