-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
203 lines (188 loc) · 7.35 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interactive Ebbinghaus Illusion</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
min-height: 100vh;
margin: 0;
padding: 20px;
background-color: #f0f8ff;
font-family: Arial, sans-serif;
box-sizing: border-box;
}
.container {
text-align: center;
max-width: 800px;
}
h1 {
color: #333;
margin-bottom: 20px;
}
canvas {
border: 2px solid #4682b4;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.controls {
margin-top: 20px;
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 10px;
}
.control-group {
display: flex;
flex-direction: column;
align-items: center;
}
button, input {
margin: 5px;
}
#explanation {
margin-top: 20px;
text-align: left;
padding: 15px;
background-color: #fff;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
</style>
</head>
<body>
<div class="container">
<h1>Interactive Ebbinghaus Illusion</h1>
<canvas id="myCanvas" width="800" height="400"></canvas>
<div class="controls">
<div class="control-group">
<label for="leftSize">Left Petals Size:</label>
<input type="range" id="leftSize" min="50" max="200" value="140">
</div>
<div class="control-group">
<label for="rightSize">Right Petals Size:</label>
<input type="range" id="rightSize" min="20" max="100" value="50">
</div>
<div class="control-group">
<label for="petalColor">Petal Color:</label>
<input type="color" id="petalColor" value="#4169e1">
</div>
<div class="control-group">
<label for="centerColor">Center Color:</label>
<input type="color" id="centerColor" value="#ffa500">
</div>
<button id="freezeBtn">Freeze</button>
<button id="measureBtn">Compare Centers</button>
</div>
<div id="explanation">
<h3>About the Ebbinghaus Illusion</h3>
<p>The Ebbinghaus illusion, also known as Titchener circles, is an optical illusion of relative size perception. In the illusion, two circles of identical size are placed near to each other, and one is surrounded by large circles while the other is surrounded by small circles. As a result, the central circle surrounded by large circles appears smaller than the central circle surrounded by small circles.</p>
<p>This illusion demonstrates how our perception of an object's size can be influenced by its surroundings. It's named after German psychologist Hermann Ebbinghaus (1850-1909), who made significant contributions to the study of memory and perception.</p>
</div>
</div>
<script>
const ctx = myCanvas.getContext('2d');
let isAnimating = true;
let isMeasuring = false;
const leftSizeSlider = document.getElementById('leftSize');
const rightSizeSlider = document.getElementById('rightSize');
const petalColorPicker = document.getElementById('petalColor');
const centerColorPicker = document.getElementById('centerColor');
const freezeBtn = document.getElementById('freezeBtn');
const measureBtn = document.getElementById('measureBtn');
freezeBtn.addEventListener('click', () => {
isAnimating = !isAnimating;
freezeBtn.textContent = isAnimating ? 'Freeze' : 'Unfreeze';
});
measureBtn.addEventListener('click', () => {
isMeasuring = !isMeasuring;
measureBtn.textContent = isMeasuring ? 'Hide Comparison' : 'Compare Centers';
});
animate();
function animate(){
ctx.clearRect(0, 0, myCanvas.width, myCanvas.height);
const t = isAnimating ? Math.sin(new Date().getTime() / 1000) ** 2 : 0.5;
// left flower (larger petals)
drawFlower({
centerX: myCanvas.width / 4,
centerY: myCanvas.height / 2,
innerRad: 40,
outerRad: lerp(Number(leftSizeSlider.value), Number(leftSizeSlider.value) - 40, t),
petalRadius: lerp(Number(leftSizeSlider.value) / 3, Number(leftSizeSlider.value) / 4, t),
petalCount: 6,
color: petalColorPicker.value,
centerColor: centerColorPicker.value
});
//right flower (smaller petals)
drawFlower({
centerX: 3 * myCanvas.width / 4,
centerY: myCanvas.height / 2,
innerRad: 40,
outerRad: lerp(Number(rightSizeSlider.value), Number(rightSizeSlider.value) - 40, t),
petalRadius: lerp(Number(rightSizeSlider.value) / 3, Number(rightSizeSlider.value) / 4, t),
petalCount: 6,
color: petalColorPicker.value,
centerColor: centerColorPicker.value
});
if (isMeasuring) {
drawMeasurement();
}
requestAnimationFrame(animate);
}
function lerp(a, b, t){
return a + (b-a) * t;
}
function drawFlower({
centerX,
centerY,
innerRad,
outerRad,
petalRadius,
petalCount,
color,
centerColor
}) {
// Draw outer petals
ctx.beginPath();
ctx.arc(centerX, centerY, outerRad, 0, 2 * Math.PI);
ctx.lineWidth = petalRadius * 2;
ctx.strokeStyle = color;
ctx.lineCap = "round";
const spacing = 2 * Math.PI * outerRad / petalCount;
ctx.setLineDash([0, spacing]);
ctx.stroke();
// Draw center circle
ctx.beginPath();
ctx.fillStyle = centerColor;
ctx.arc(centerX, centerY, innerRad, 0, 2*Math.PI);
ctx.fill();
// Draw a black outline around the center circle
ctx.beginPath();
ctx.strokeStyle = "black";
ctx.lineWidth = 2;
ctx.arc(centerX, centerY, innerRad, 0, 2*Math.PI);
ctx.stroke();
}
function drawMeasurement() {
const leftCenterX = myCanvas.width / 4;
const rightCenterX = 3 * myCanvas.width / 4;
const centerY = myCanvas.height / 2;
ctx.beginPath();
ctx.moveTo(leftCenterX, centerY);
ctx.lineTo(rightCenterX, centerY);
ctx.strokeStyle = 'red';
ctx.lineWidth = 2;
ctx.setLineDash([5, 5]);
ctx.stroke();
ctx.font = '16px Arial';
ctx.fillStyle = 'red';
ctx.textAlign = 'center';
ctx.fillText('Same Size', myCanvas.width / 2, centerY - 10);
}
</script>
</body>
</html>