-
Notifications
You must be signed in to change notification settings - Fork 0
/
flowers.js
80 lines (71 loc) · 1.62 KB
/
flowers.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
(function () {
'use strict';
var stems = document.querySelectorAll( '.stem' );
var flowers = document.querySelectorAll( '.flower' );
var colours = [
'deeppink',
'black',
'purple',
'brown',
'skyblue',
'pink',
'cornflowerblue',
'orange',
'darkseagreen',
'gold',
'maroon',
'midnightblue',
'yellow',
'#003f07'
];
var currentColour = 0;
function toggleClass( element, className ) {
if ( element.classList.contains( className ) ) {
element.classList.remove( className );
} else {
element.classList.add( className );
}
}
function cycleColour( direction ) {
direction = direction || '+';
if ( direction === '-' ) {
currentColour += 1;
} else {
currentColour -= 1;
}
if ( currentColour === -1 ) {
currentColour = colours.length -1;
} else if ( currentColour === colours.length ) {
currentColour = 0;
}
return currentColour;
}
window.addEventListener( 'keyup', function ( event ) {
var colour;
var key = event.which;
var keys = {
z: 90,
x: 88,
c: 67,
left: 37,
right: 39
};
if ( key === keys.z ) {
toggleClass( stems[ 0 ], 'alive' );
} else if ( key === keys.x ) {
toggleClass( stems[ 1 ], 'alive' );
} else if ( key === keys.c ) {
toggleClass( stems[ 2 ], 'alive' );
} else if ( key === keys.left ) {
colour = cycleColour( '+' );
[].forEach.call( flowers, function ( flower ) {
flower.style.backgroundColor = colours[ colour ];
});
} else if ( key === keys.right ) {
colour = cycleColour( '-' );
[].forEach.call( flowers, function ( flower ) {
flower.style.backgroundColor = colours[ colour ];
});
}
});
})();