-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
411 lines (359 loc) · 13.1 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
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
<html>
<head>
<title>The Liturgical Year</title>
<style>
BODY {
font-family: 'Gill Sans', helvetica;
}
P.copyright {
font-size: 75%
}
</style>
<script>
try {
e = () => 5
if (e() !== 5) {
throw "ugh";
}
} catch (e) {
alert('Unfortunately, this page requires a newer browser than you have...')
}
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/snap.svg/0.5.1/snap.svg-min.js"></script>
<script>
lc = null;
function runIt() {
lc = new LiturgicalCycle('#mainCycle');
}
class PieChart {
// adapted for ES6 from https://github.com/artemdemo/pie-chart-snapsvg
constructor(dataArray) {
this.xCenter = 300;
this.yCenter = 300;
this.outerRadius = 200;
this.innerRadius = 120;
this.makeDonut = true;
this.data = dataArray;
this.totalDataAmount = undefined;
this.getAngleData();
this.updatePositionData();
this.sectors = [];
this.innerDonut = undefined;
}
/**
* Converts a list of [{amount, title, color}] objects into
* a richer list that knows its angle informati
*/
getAngleData() {
const d = this.data;
const ad = [];
let totalAmount = 0;
for (const o of d) {
totalAmount += o.amount;
}
for (const o of d) {
const newO = Object.assign({
angle: 360 * o.amount/totalAmount
}, o);
ad.push(newO);
}
this.angleData = ad;
this.totalDataAmount = totalAmount;
return ad;
}
/**
* Call whenever the shape of the piechart changes.
* Gets x and y data from the angle data
*/
updatePositionData() {
const ad = this.angleData;
const pd = [];
let currentStartAngle = -90;
let currentEndAngle = -90;
// use Snap.sin, etc. which works on angles
const totalSlices = ad.length;
const xC = this.xCenter;
const yC = this.yCenter;
const r = this.outerRadius;
let firstX1 = 0;
let firstY1 = 0;
for (let i = 0; i < totalSlices; i++) {
const thisAd = ad[i];
currentEndAngle = currentStartAngle + thisAd.angle;
let x1 = Math.round(xC + r * Snap.cos(currentStartAngle));
let x2 = Math.round(xC + r * Snap.cos(currentEndAngle));
let y1 = Math.round(yC + r * Snap.sin(currentStartAngle));
let y2 = Math.round(yC + r * Snap.sin(currentEndAngle));
// these correct for rounding to make sure that
// the first and last sections intersect.
if (i == 0) {
firstX1 = x1;
firstY1 = y1;
} else if (i + 1 == totalSlices) {
x2 = firstX1;
y2 = firstY1;
}
const negAngle = currentStartAngle + thisAd.angle / 2;
const nx = Math.round(xC + r * Snap.cos(negAngle));
const ny = Math.round(yC + r * Snap.sin(negAngle));
const bigAdjust = ` A${r},${r} 0 0,1 ${nx},${ny}`;
const pathStr = `M${xC},${yC} L${x1},${y1} ${bigAdjust} A${r},${r} 0 0,1 ${x2},${y2} z`;
const newData = Object.assign({'x1': x1, 'y1': y1,
'x2': x2, 'y2': y2,
'pathStr': pathStr,
}, thisAd);
pd.push(newData)
currentStartAngle = currentEndAngle;
}
this.positionData = pd;
return pd;
}
drawSvg(s) {
const sectors = [];
for (const o of this.positionData) {
const sector = s.path(o.pathStr);
sector.attr('fill', o.color);
sectors.push(sector);
o.sector = sector;
}
if (this.makeDonut) {
this.innerDonut = s.circle(this.xCenter, this.yCenter, this.innerRadius);
this.innerDonut.attr('fill', '#fff'); // should be clip;
}
this.sectors = sectors;
return this.positionData;
}
clearSvg(s) {
for (const sector of this.sectors) {
sector.remove();
}
if (this.innerDonut !== undefined) {
this.innerDonut.remove();
this.innerDonut = undefined;
}
this.sectors = [];
}
}
sampleData = [
{amount: 20, title: 'hi', color: 'red'},
{amount: 40, title: 'bye', color: 'blue'},
{amount: 10, title: 'howdy', color: 'yellow'},
{amount: 5, title: 'heya', color: 'green'},
];
class LiturgicalCycle {
constructor(where) {
this.s = Snap(where);
this.titleText = this.createText(300, 20, 'Liturgical Year').attr('text-anchor', 'middle');
this.defaultFill = {
stroke: '#ff00ff',
strokeWidth: 20,
fill: 'goldenrod',
opacity: 0.2
};
// const r = this.s.rect(100, 100, 100, 100, 20, 20).attr(this.defaultFill);
// r.click( () => {
// this.removedText.remove();
// this.createText(100, 20, 'this is the Text!');
// const top = 50;
// const left = 60;
// const radius = 20;
// const c = this.s.circle(top, left, radius);
// c.attr(this.defaultFill);
// r.attr('opacity', 0.4);
// });
// this.r = r;
this.lentColor = '#34495e'; // material ui, Wetasphalt
this.celebrationColor = '#27ae60'; // material ui, Nephritis
this.normalColor = '#2980b9';
this.cycleYear = [
{amount: 22, title: 'Advent', color: this.lentColor},
{amount: 12, title: 'Christmastide', color: this.celebrationColor, left: 'Christmas Day'},
{amount: 54, title: 'after Epiphany', color: this.normalColor, left: 'Epiphany'},
{amount: 40, title: 'Lent', color: this.lentColor, left: 'Ash Wednesday'},
{amount: 49, title: 'Eastertide', color: this.celebrationColor, left: 'Easter'},
{amount: 188, title: 'after Pentecost', color: this.normalColor, left: 'Pentecost'},
];
this.ordinaryI = this.cycleYear[2];
this.ordinaryII = this.cycleYear[5];
this.totalOrdinaryLength = 54 + 188;
this.positionData = undefined;
this.saintDayPositions = [];
this.saints = undefined;
this.drawPieChart();
this.easterPosition = this.positionData[4];
this.dragOrdinaryIAmount = undefined;
}
createText(x, y, text) {
const fontSize = 40;
const t = this.s.text(x, y + fontSize, text);
t.attr('font-size', fontSize);
t.attr('font-family', 'Gill Sans, Helvetica');
t.attr('font-weight', '300');
t.attr('text-rendering', 'optimizeLegibility');
return t;
}
drawPieChart() {
this.pieChart = new PieChart(this.cycleYear)
const positionData = this.pieChart.drawSvg(this.s);
this.positionData = positionData;
const lc = this;
for (const pd of positionData) {
const sec = pd.sector;
sec.click( () => this.sectorClick(pd) );
sec.hover( () => this.sectorClick(pd),
() => this.setTitle('Liturgical Year')
);
if (pd.left !== undefined) {
const c = this.s.circle(pd.x1, pd.y1, 10).attr({
'stroke': 'black',
'strokeWidth': '2px',
'fill': 'gray',
}).hover( () => this.setTitle(pd.left) );
if (pd.left == 'Easter') {
c.attr('fill', 'red');
c.drag( (dx, dy) => this.easterDrag(dx, dy),
undefined,
() => this.endEasterDrag()
);
}
pd.point = c;
}
}
this.addProperSaints();
return positionData;
}
getSaintsDaysPositions() {
// should be only called once, so that
// these are random but fixed.
for (let i = 0; i < 20; i++) {
const iDeg = parseInt(Math.random() * 360);
this.saintDayPositions.push(iDeg);
}
return this.saintDayPositions;
}
//
// Adds the proper of the saints as a ring.
//
addProperSaints() {
const pc = this.pieChart
const saintDistance = pc.innerRadius + (pc.outerRadius - pc.innerRadius) / 2;
//const c = this.s.circle(pc.xCenter, pc.yCenter, saintDistance);
// c.attr({
// stroke: '#ffffdd',
// strokeWidth: 20,
// fill: 'rgba(0,0,0,0)',
// filter: this.s.filter(Snap.filter.shadow(0, 2, 3))
// });
const xC = pc.xCenter;
const yC = pc.yCenter;
const saintWidth = 20;
const saintIn = saintDistance - saintWidth/2;
const saintOut = saintDistance + saintWidth/2;
const c = this.s.path(`M ${xC}, ${yC} m 0, -${saintOut} ` +
`a ${saintOut}, ${saintOut}, 0, 1, 0, 1, 0 Z ` +
`m 0 ${saintWidth} a ${saintIn}, ${saintIn}, 0, 1, 1, -1, 0 Z`)
c.attr({
fill: '#ffffdd',
filter: this.s.filter(Snap.filter.shadow(0, 2, 3))
});
c.hover( () => this.setTitle('Proper of the Saints'),
() => this.setTitle('Liturgical Year')
);
const saintDays = [];
if (this.saintDayPositions.length == 0) {
this.getSaintsDaysPositions()
}
for (let i = 0; i < this.saintDayPositions.length; i++) {
const iDeg = this.saintDayPositions[i];
const iX = Math.round(pc.xCenter + saintDistance * Snap.cos(iDeg));
const iY = Math.round(pc.xCenter + saintDistance * Snap.sin(iDeg));
const sc = this.s.circle(iX, iY, 3);
sc.hover( () => this.setTitle("A hypothetical Saint's Day"),
() => this.setTitle('Liturgical Year')
);
saintDays.push(sc);
}
this.saints = this.s.group(c, ...saintDays);
return this.saints;
}
easterDrag(dx, dy) {
const easterX = this.easterPosition.x1 + dx - this.pieChart.xCenter;
const easterY = -1 * (this.easterPosition.y1 + dy - this.pieChart.yCenter);
const arcT = Snap.deg(Math.atan2(easterY, easterX));
// bug in Snap.atan2!
const easterXOld = this.easterPosition.x1 - this.pieChart.xCenter;
const easterYOld = -1 * (this.easterPosition.y1 - this.pieChart.yCenter);
const arcTOld = Snap.deg(Math.atan2(easterYOld, easterXOld));
// bug in Snap.atan2!
const angleDifference = arcTOld - arcT;
const dayPerAngle = 365/360;
const dayChange = angleDifference * dayPerAngle;
const storedOrdinaryIAmount = this.ordinaryI.amount;
const storedOrdinaryIIAmount = this.ordinaryII.amount;
let tempOrdinaryI = Math.round(storedOrdinaryIAmount + dayChange);
if (tempOrdinaryI < 35) { // March 22
tempOrdinaryI = 35;
} else if (tempOrdinaryI > 69) { // April 25
tempOrdinaryI = 69;
}
this.changeOrdinary(tempOrdinaryI);
this.dragOrdinaryIAmount = tempOrdinaryI;
this.ordinaryI.amount = storedOrdinaryIAmount;
this.ordinaryII.amount = storedOrdinaryIIAmount;
}
endEasterDrag() {
this.changeOrdinary(this.dragOrdinaryIAmount);
}
clearPieChart() {
if (this.pieChart !== undefined) {
this.pieChart.clearSvg();
}
for (const pd of this.positionData) {
if (pd.point !== undefined) {
pd.point.remove();
}
}
if (this.saints !== undefined) {
this.saints.remove();
this.saints = undefined;
}
}
setTitle(txt) {
this.titleText.attr('text', txt);
}
sectorClick(pd) {
this.setTitle(pd.title);
}
animateChangeOrdinary(newOrdinaryI) {
Snap.animate(
this.ordinaryI.amount,
newOrdinaryI,
(v) => this.changeOrdinary(v),
100 + 20 * Math.abs(this.ordinaryI.amount, newOrdinaryI)
);
}
changeOrdinary(newOrdinaryI) {
this.ordinaryI.amount = newOrdinaryI
this.ordinaryII.amount = this.totalOrdinaryLength - newOrdinaryI;
this.clearPieChart();
this.drawPieChart();
}
}
</script>
</head>
<body onLoad='runIt()'>
<div style='text-align: center'>
<svg version="1.1" id="mainCycle" height="600" width="600"></svg>
<br />
<p>
A representation of the relationship between the mobile Proper of the Time
(drag the red dot for Easter) and the fixed Proper of the Saints.
</p>
<p class="copyright">
Copyright © 2017, <a href="http://www.trecento.com/">Michael Scott Cuthbert</a>. Very few rights reserved.
Use it however you'd like. <a href="https://github.com/cuthbertLab/liturgicalCycles">Source Code</a>.
Narration by Dr. <a href="http://www.elinahamilton.com/">Elina Hamilton</a>.
</p>
</div>
</body>
</html>