This repository has been archived by the owner on Feb 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
a-full-life.js
495 lines (466 loc) · 14.7 KB
/
a-full-life.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
var aFullLife = (function() {
var game = {};
/*
* Levels
*
* Each level in the game has a name, a description, an inner diameter
* (used to represent a less-than-full life), a type, and various things
* that can be used to fill a life.
*/
game.levels = [
{
name: 'Normal',
description: 'A well-balanced life',
innerDiameter: 1, // Percentage of outer diameter
type: '',
fillers: [
{ name: 'Physical health', value: 0.30, selected: false },
{ name: 'Mental and emotional wellbeing', value: 0.30, selected: false },
{ name: 'An established sense of identity', value: 0.20, selected: false },
{ name: 'A drive to move forward and improve', value: 0.20, selected: false}
]
},
{
name: 'Scheduling',
description: 'Making time for everything',
innerDiameter: 1,
type: '',
fillers: [
{ name: 'Work', value: 0.24, selected: false },
{ name: 'Sleep', value: 0.33, selected: false },
{ name: 'Meals', value: 0.13, selected: false },
{ name: 'Social life', value: 0.20, selected: false },
{ name: 'Hobbies', value: 0.10, selected: false }
]
},
{
name: 'Clinical Depression',
description: 'You have been diagnosed with clinical depression.',
innerDiameter: 0.72,
type: 'health',
fillers: [
{ name: 'Feeling good about the work you do', value: 0.24, selected: false },
{ name: 'Sleeping well', value: 0.33, selected: false },
{ name: 'Enjoying meals', value: 0.13, selected: false },
{ name: 'Looking happy in your social life', value: 0.20, selected: false },
{ name: 'Maintaining interest in hobbies', value: 0.10, selected: false }
]
},
{
name: 'Generalized Anxiety Disorder',
description: 'You have been diagnosed with an anxiety disorder.',
innerDiameter: 0.82,
type: 'health',
fillers: [
{ name: 'Obsessing over accomplishing tasks well', value: 0.44, selected: false },
{ name: 'Sleeping through the whole night', value: 0.33, selected: false },
{ name: 'Not getting addicted to benzodiazapines', value: 0.13, selected: false },
{ name: 'Being able to be around others comfortably', value: 0.20, selected: false },
{ name: 'Worrying whether your hobbies reflect well on you', value: 0.10, selected: false }
]
},
{
name: 'Homosexuality',
description: 'You realize at an early age that you identify as homosexual',
innerDiameter: 0.92,
type: 'identity',
fillers: [
{ name: 'Feeling comfortable with your sexuality', value: 0.20, selected: false},
{ name: 'The acceptance of your parents', value: 0.30, selected: false },
{ name: 'The respect of your coworkers/peers', value: 0.30, selected: false },
{ name: 'Getting married', value: 0.60, selected: false },
{ name: 'Expectation of personal safety', value: 0.40, selected: false}
]
},
{
name: 'Gender Dysphoria',
description: 'You realize early on that you do not feel comfortable with' +
' your birth sex.',
innerDiameter: 0.72,
type: 'identity',
fillers: [
{ name: 'Passing', value: 0.70, selected: false },
{ name: 'Affording Hormone Replacement Therapy', value: 0.4, selected: false },
{ name: 'Deciding on surgery', value: 0.3, selected: false },
{ name: 'Explaining the difference between gender and sex', value: 0.15, selected: false },
],
},
{
name: 'Reality',
description: 'This is your life. Live it to the fullest.',
innerDiameter: 1,
type: 'final',
fillers: [
{ name: 'What it takes to make you happy', value: 0.30, selected: false },
{ name: 'What it takes to keep you healthy', value: 0.30, selected: false },
{ name: 'Everything you need to feel fulfilled', value: 0.40, selected: false }
],
}
];
// We can start on a level by using the hash portion of the URL, but only if we want.
game.useHash = false;
game.currentLevel = window.location.hash && game.useHash ?
parseInt(window.location.hash.split('#')[1], 10) : 0;
// Flags to say whether or not we've seen certain note/instruction screens.
game.instructionsSeen = false;
game.healthNoteSeen = false;
game.identityNoteSeen = false;
/*
* Initialize
*
* This method builds up the SVG element in the correct dimensions, creating
* some grouping elements along the way that will be used in the drawing
* portion of the game.
*/
game.initialize = function() {
// Get the game element and build an SVG inside it.
this.el = document.getElementById('game');
this.dimensions = [
Math.max(1125, this.el.clientWidth) - 20,
Math.max(600, document.documentElement.clientHeight) - 240
];
this.vis = d3.select('#game')
.append('svg')
.attr({
width: this.dimensions[0] + 20,
height: this.dimensions[1] + 20
})
.append('g')
.attr('transform', 'translate(10,10)');
// Panels
this.fillerPanel = this.vis.append('g')
.attr({
'class': 'filler',
width: Math.max(550, this.dimensions[0] - (this.dimensions[0] * 0.618)),
height: this.dimensions[1] - 10,
transform: 'translate(0,10)'
});
this.lifePanel = this.vis.append('g')
.attr({
'class': 'life',
width: Math.max(575, this.dimensions[0] * 0.618),
height: this.dimensions[1] - 10,
transform: 'translate(' +
(this.dimensions[0] - (this.dimensions[0] * 0.618)) + ',10)'
});
this.levelPanel = this.vis.append('g')
.attr({
'class': 'level',
width: 100,
height: 60,
transform: 'translate(' + (this.dimensions[0] - 100) + ',10)'
});
// Headers
this.fillerPanel.append('text')
.text('Fill Your Life With:')
.attr({
'class': 'header',
x: 0,
y: 26
});
this.lifePanel.append('text')
.text('Your Life: ')
.attr({
'class': 'header',
x: 0,
y: 26
}).append('tspan')
.attr('class', 'levelTitle');
this.lifePanel.append('text')
.attr({
'class': 'levelDescription',
x: 0,
y: 54
});
this.levelPanel.append('text')
.text('Level ')
.attr({
'class': 'header',
x: 0,
y: 26
}).append('tspan')
.attr('class', 'currLevel');
this.levelPanel.append('text')
.text('<')
.attr({
'class': 'header prevLevel clickable',
x: 0,
y: 54
});
this.levelPanel.append('text')
.text('>')
.attr({
'class': 'header nextLevel clickable',
x: 87,
y: 54
});
this.draw();
};
/*
* draw
*
* Draws one level of the game and attaches interactions.
*/
game.draw = function() {
var self = this;
var level = this.levels[this.currentLevel];
if (this.useHash) {
window.location.hash = this.currentLevel;
}
// Show notes if we need to.
if (!this.healthNoteSeen && level.type === 'health') {
this.showNote('health');
this.healthNoteSeen = true;
}
if (!this.identityNoteSeen && level.type === 'identity') {
this.showNote('identity');
this.identityNoteSeen = true;
}
if (level.type === 'final') {
this.showNote('final');
}
if (!this.instructionsSeen) {
this.showNote('instructions');
this.instructionsSeen = true;
}
// Draw the life circle.
// First, calculate points and radii.
var centerPoint = [
this.lifePanel.attr('width') / 2,
this.lifePanel.attr('height') / 2 + 26
];
var outerRadius = Math.min(
this.lifePanel.attr('width') / 2,
this.lifePanel.attr('height') / 2 - 26) * 0.7;
var strokeWidth = outerRadius - outerRadius * level.innerDiameter + 1;
var innerRadius = outerRadius - strokeWidth;
// Attach these to the object for use in other scopes.
this.lifeCircleProperties = {
outerRadius: outerRadius,
strokeWidth: strokeWidth,
innerRadius: innerRadius,
centerPoint: centerPoint
};
// Set the title and description.
this.lifePanel.select('.levelTitle')
.text(level.name);
this.lifePanel.select('.levelDescription')
.text(level.description);
// Add the life-circle.
this.life = this.lifePanel.append('circle')
.attr({
'class': 'lifeCircle clearable',
cx: centerPoint[0],
cy: centerPoint[1],
r: outerRadius - (strokeWidth / 2),
'stroke-width': strokeWidth
});
// Draw the fillers
var fillerHeight = (this.fillerPanel.attr('height') - 26) /
level.fillers.length * 0.9;
var fillerItem = this.fillerPanel.selectAll('.fillerItem')
.data(level.fillers)
.enter()
.append('g')
.attr('class', 'fillerItem clearable clickable')
.attr('transform', function(d, i) {
return 'translate(' + [10, 28 + fillerHeight * i] + ')';
});
fillerItem.append('text')
.text(function(d) { return d.name; })
.attr({
x: fillerHeight,
y: fillerHeight / 2,
'dominant-baseline': 'middle'
});
fillerItem.append('circle')
.attr({
'class': 'fillerCircle',
cx: fillerHeight / 2,
cy: fillerHeight / 2,
strokeWidth: 1
})
.attr('r', function(d) {
return (d.value * fillerHeight) / 2;
});
// This rect is what actually listens to the click events.
fillerItem.append('rect')
.attr({
x: 0,
y: 0,
width: this.fillerPanel.attr('width') - 20,
height: fillerHeight
})
.on('click', function(d) {
self.step(this, d);
});
// Draw the current value indicator
this.currentValue = 0;
this.currentValueIndicator = this.lifePanel.append('g')
.attr('class', 'currentValueIndicator clearable');
this.currentValueIndicator.append('circle')
.attr({
cx: centerPoint[0],
cy: centerPoint[1],
r: 1,
'stroke-width': 1
});
this.currentValueIndicator.append('text')
.text('0% full')
.attr({
x: centerPoint[0] + 10,
y: centerPoint[1],
'dominant-baseline': 'middle'
});
// Display the level selection controls and add actions if need be.
this.levelPanel.select('.currLevel').text(this.currentLevel);
if (this.currentLevel === 0) {
this.levelPanel.select('.prevLevel')
.style('display', 'none');
} else {
this.levelPanel.select('.prevLevel')
.style('display', 'block')
.on('click', function() {
self.currentLevel--;
self.clear();
self.draw();
});
}
if (this.currentLevel === this.levels.length - 1) {
this.levelPanel.select('.nextLevel')
.style('display', 'none');
} else {
this.levelPanel.select('.nextLevel')
.style('display', 'block')
.on('click', function() {
self.currentLevel++;
self.clear();
self.draw();
});
}
};
/*
* step
*
* Draw one step in the process of adding fillers to a life.
*/
game.step = function(el, datum) {
var mustTransition = false;
// If we've already selected the item, deslect it. Otherwise, make sure
// we can add the item (warn the user if not).
if (datum.selected) {
d3.select(el)
.style('opacity', 0.0);
this.currentValue -= datum.value;
mustTransition = true;
datum.selected = false;
} else {
if (Math.floor((datum.value + this.currentValue) * 100) >
this.levels[this.currentLevel].innerDiameter * 100) {
this.tooFullError();
return;
} else {
d3.select(el)
.style('opacity', 0.75);
this.currentValue += datum.value;
mustTransition = true;
datum.selected = true;
}
}
// If we need to change our current value indicator, do so with transitions.
if (mustTransition) {
this.currentValueIndicator.select('circle')
.transition()
.attr('r', this.currentValue *
this.lifeCircleProperties.outerRadius)
.style('fill', Math.floor(this.currentValue * 100) === 100 ?
'lightblue' : 'white')
.duration(1000);
this.currentValueIndicator.select('text')
.transition()
.attr('x', this.lifeCircleProperties.centerPoint[0] + 10 +
(this.currentValue * this.lifeCircleProperties.outerRadius))
.duration(1000);
this.currentValueIndicator.select('text')
.text(Math.floor(this.currentValue * 100) + '% full')
}
};
/*
* showNote
*
* This displays an overlay on top of the SVG. It should contain divs
* classed 'left' and 'right' to fit over the filler and life panels
* respectively.
*/
game.showNote = function(noteName) {
var note = d3.select('#' + noteName);
note.style({
display: 'block',
top: noteName === 'instructions' ? 0 : (this.dimensions[1] / 2) + 'px',
left: 0
});
note.select('.left')
.attr('style', 'float:left;width:220px;height:' + (this.dimensions[1] / 2 - 10) + 'px');
note.select('.right')
.attr('style', 'float:right;width:' + (this.lifePanel.attr('width') - 10) +
'px;height:' + (this.dimensions[1] / 2 - 10) + 'px');
note.transition()
.style('opacity', 0.9)
.duration(1000);
note.on('click', function() {
note.transition()
.style('opacity', 0)
.duration(1000);
note.transition()
.style('display', 'none')
.delay(1001);
});
};
/*
* tooFullError
*
* Display an error if we cannot fit that filler into life.
*/
game.tooFullError = function() {
var error = this.vis.append('g')
.attr('class', 'clearable');
error.append('rect')
.attr({
'class': 'modalError',
x: this.dimensions[0] / 2 - 200,
y: this.dimensions[1] / 2 - 50,
rx: 20,
ry: 20,
width: 400,
height: 100
});
error.append('text')
.text('You cannot fit that in your life!')
.attr({
'class': 'header',
x: this.dimensions[0] / 2,
y: this.dimensions[1] / 2,
'text-anchor': 'middle',
'dominant-baseline': 'middle'
});
error.transition()
.style('opacity', 0)
.duration(2500)
.remove();
};
/*
* clear
*
* Clear the SVG of all clearable items.
*/
game.clear = function() {
this.levels.forEach(function(level) {
level.fillers.forEach(function(filler) {
filler.selected = false;
});
});
this.vis.selectAll('.clearable')
.remove();
};
return game;
})()