-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstimWithSliders.html
84 lines (81 loc) · 2.86 KB
/
stimWithSliders.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
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input id="dot_frequency" type="range" min='0' max='1' step='0.01'></input>
<input id="line_frequency" type="range" min='0' max='1' step='0.01'></input>
<button id="new">Generate new exemplar</button>
<div id="stim_target">
</div>
</body>
<script>
var min_dot_density = 100;
var max_dot_density = 2000;
var min_line_density = 30;
var max_line_density = 550;
var dim_a = 0.5;
var dim_b = 0.5;
window.onload = function() {
draw();
};
document.querySelector('#dot_frequency').addEventListener('input', function(e, i) {
dim_a = document.querySelector('#dot_frequency').value;
draw();
});
document.querySelector('#line_frequency').addEventListener('input', function(e, i) {
dim_b = document.querySelector('#line_frequency').value;
draw();
});
document.querySelector('#new').addEventListener('click', function(){
draw();
})
function generate_stim(dot_dim, line_dim){
// params
var w = 400;
var r = 100;
var x = w / 2;
var y = w / 2;
var circle_r = 2;
var minLineLength = 10;
dot_density = min_dot_density + (max_dot_density-min_dot_density)*dot_dim;
line_density = min_line_density + (max_line_density-min_line_density)*line_dim;
// svg string
var str = '<svg xmlns="http://www.w3.org/2000/svg" width="' + w + '" height="' + w + '">'
//Code for drawing the lines that come out of the circle.
//Each line starts in the center of the circle and is drawn outward
for (var i = 0; i < line_density; i++) {
var angle = Math.random() * Math.PI / 2;
var lineLength = r + minLineLength + (r - minLineLength) * Math.random();
var x2 = Math.cos(angle) * lineLength;
var y2 = Math.sin(angle) * lineLength;
if (Math.random() < 0.5) {
x2 = -x2;
}
if (Math.random() < 0.5) {
y2 = -y2;
}
x2 = x + x2;
y2 = y + y2;
str += '<line x1="' + x + '" y1="' + y + '" x2="' + x2 + '" y2="' + y2 + '" style="stroke:rgb(65,105,225); stroke-width:1;" />';
}
// draw circle
str += '<circle cx="' + x + '" cy="' + y + '" r="' + r + '" style="stroke:rgb(65,105,225); stroke-width:2; fill: rgb(255,255,255);"/>'
//Code for creating and dispersing the dots
for (var j = 0; j < dot_density; j++) {
do {
var x2 = Math.random() * 2 * r + x - r;
var y2 = Math.random() * 2 * r + y - r;
} while ((Math.pow(x - x2, 2) + Math.pow(y - y2, 2)) > Math.pow(r - circle_r - 2, 2));
str += '<circle cx="' + x2 + '" cy="' + y2 + '" r="' + circle_r + '" style="fill:rgb(65,105,225);" />';
}
str += "</svg>";
return str;
}
function draw() {
var str = generate_stim(dim_a, dim_b);
var params = '<div><p>' + dot_density + ' dots and ' + line_density + ' lines </p></div>'
document.querySelector('#stim_target').innerHTML = str + params;
}
</script>
</html>