-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
117 lines (104 loc) · 3.49 KB
/
script.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
var mode;
function drawGrid(squaresPerSide) {
//Add rows
for (var i = 0; i < squaresPerSide; i++) {
$('#sketchpad').append('<div class="row"></div>');
}
//Fill rows
for (var i = 0; i < squaresPerSide; i++) {
$('.row').append('<div class="square"></div>');
}
//Set square sizes
var squareDimension = $('#sketchpad').width() / squaresPerSide;
$('.square').css({
'height': squareDimension,
'width': squareDimension
});
}
function setMode(newMode) {
mode = newMode;
$('#' + newMode).addClass('selectedMode').siblings().removeClass('selectedMode');
}
$(document).ready(function() {
//Set up
var squaresPerSide = 16;
drawGrid(squaresPerSide);
setMode('pen');
//Draw!
$('#sketchpad').on('mouseenter', '.square', function() {
var opacity = +$(this).css('opacity');
switch (mode) {
case 'pen':
$(this).css({
'opacity': 1,
'background-color': 'black'
});
break;
case 'pencil':
if (opacity + .1 < 1) {
$(this).css({
'opacity': opacity + .1
});
}
else {
$(this).css({
'opacity': 1
});
}
break;
case 'rainbow':
var r = Math.floor(Math.random() * 256);
var g = Math.floor(Math.random() * 256);
var b = Math.floor(Math.random() * 256);
$(this).css({
'opacity': 1,
'background-color': 'rgb(' + r + ',' + g + ',' + b + ')'
});
break;
case 'eraser':
if (opacity > 0) {
$(this).css({
'opacity': opacity - .2
});
}
//Double check for browser weirdness like Safari not wanting to
//set it all the way to 0, and adjust for erasing too far past
//0, since it erases faster than the pencil draws
var newOpacity = $(this).css('opacity')
if (opacity - newOpacity < .015 || newOpacity < 0) {
$(this).css({
'opacity': 0,
'background-color': 'black'
});
}
break;
}
});
//Reset
$('#reset').on('click', function() {
//Get and validate user input
var newSquaresPerSide;
var invalidInput;
do {
newSquaresPerSide = prompt('How many squares per side would you like?',
squaresPerSide);
var isNull = newSquaresPerSide === null;
var isInteger = $.isNumeric(newSquaresPerSide) &&
Number.isInteger(+newSquaresPerSide);
var isPositive = newSquaresPerSide > 0;
invalidInput = !isNull && !(isInteger && isPositive);
if (invalidInput) {
alert('Only positive integers (1, 2, 3, etc.)');
}
} while (invalidInput);
if (newSquaresPerSide !== null) {
squaresPerSide = newSquaresPerSide;
$('#sketchpad').empty();
drawGrid(squaresPerSide);
}
});
//Change mode
$('#modes').on('click', 'button', function() {
setMode($(this).attr('id'));
});
});