-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlampen-anknipsen.js
122 lines (106 loc) · 3.33 KB
/
lampen-anknipsen.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
var s = function(sketch) {
let numberOfLamps = 30;
let lampPositions = [];
let isOn = [];
let buttons = [];
let buttonPressed = [];
let canvas;
const lampSize = 50;
const container = 'lampen-anknipsen-container';
const buttonColor1 = sketch.color(25, 25, 200, 100);
const buttonColor2 = sketch.color(200, 25, 25, 100);
sketch.setup = function() {
canvas = sketch.createCanvas(windowWidth-100, 300);
canvas.parent(container);
for(let i = 0; i < numberOfLamps; i++) {
createLamp(i);
}
let incrementButton = sketch.createButton('+1');
incrementButton.position(0, 0);
incrementButton.size(50, 50);
incrementButton.style('font-weight', 'bold');
incrementButton.parent(container);
incrementButton.mousePressed(incrementNumberOfLamps);
let decrementButton = sketch.createButton('-1');
decrementButton.position(75, 0);
decrementButton.size(50, 50);
decrementButton.style('font-weight', 'bold');
decrementButton.parent(container);
decrementButton.mousePressed(decrementNumberOfLamps);
let resetButton = sketch.createButton('reset');
resetButton.position(150, 0);
resetButton.size(50, 50);
resetButton.style('font-weight', 'bold');
resetButton.parent(container);
resetButton.mousePressed(reset);
sketch.noLoop();
}
sketch.draw = function() {
sketch.background(255);
sketch.stroke(0);
for(let i = 0; i < numberOfLamps; i++) {
let lampPos = lampPositions[i];
if (isOn[i]) {
sketch.fill(255, 255, 0);
} else {
sketch.fill(100);
}
sketch.circle(lampPos.x, lampPos.y, lampSize);
}
}
function createLamp(index) {
let x = 5 + lampSize/2 + 1.1 * index * lampSize;
let y = sketch.height/2;
lampPositions[index] = new p5.Vector(x, y);
isOn[index] = false;
let button = createButton(str(index+1));
button.position(x - lampSize/2, y + lampSize/2 + 5);
button.size(lampSize, lampSize);
button.style('background-color', buttonColor1);
button.style('font-weight', 'bold');
button.parent(container);
button.mousePressed(() => onButtonPressed(index+1));
buttons[index] = button;
buttonPressed[index] = false;
}
function onButtonPressed(offset) {
let index = offset-1;
buttonPressed[index] = !buttonPressed[index];
for(let i = index; i < numberOfLamps; i+=offset) {
isOn[i] = !isOn[i];
}
if (buttonPressed[index]) {
buttons[index].style('background-color', buttonColor2);
} else {
buttons[index].style('background-color', buttonColor1);
}
sketch.redraw();
}
function reset() {
for(let i = 0; i < numberOfLamps; i++) {
isOn[i] = false;
buttons[i].style('background-color', buttonColor1);
}
sketch.redraw();
}
function incrementNumberOfLamps() {
if (numberOfLamps >= 33) {
return;
}
createLamp(numberOfLamps);
numberOfLamps++;
sketch.redraw();
}
function decrementNumberOfLamps() {
if (numberOfLamps <= 1) {
return;
}
numberOfLamps--;
lampPositions.pop();
isOn.pop();
buttons.pop().remove();
buttonPressed.pop();
sketch.redraw();
}
}
var lampenAnknipsenSktech = new p5(s);