-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathdescribe.js
501 lines (487 loc) · 17 KB
/
describe.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
496
497
498
499
500
501
/**
* @module Environment
* @submodule Environment
* @for p5
* @requires core
*/
import p5 from '../core/main';
const descContainer = '_Description'; //Fallback container
const fallbackDescId = '_fallbackDesc'; //Fallback description
const fallbackTableId = '_fallbackTable'; //Fallback Table
const fallbackTableElId = '_fte_'; //Fallback Table Element
const labelContainer = '_Label'; //Label container
const labelDescId = '_labelDesc'; //Label description
const labelTableId = '_labelTable'; //Label Table
const labelTableElId = '_lte_'; //Label Table Element
/**
* Creates a screen reader-accessible description of the canvas.
*
* The first parameter, `text`, is the description of the canvas.
*
* The second parameter, `display`, is optional. It determines how the
* description is displayed. If `LABEL` is passed, as in
* `describe('A description.', LABEL)`, the description will be visible in
* a div element next to the canvas. If `FALLBACK` is passed, as in
* `describe('A description.', FALLBACK)`, the description will only be
* visible to screen readers. This is the default mode.
*
* Read
* <a href="https://p5js.org/tutorials/writing-accessible-canvas-descriptions/">Writing accessible canvas descriptions</a>
* to learn more about making sketches accessible.
*
* @method describe
* @param {String} text description of the canvas.
* @param {Constant} [display] either LABEL or FALLBACK.
*
* @example
* <div>
* <code>
* function setup() {
* background('pink');
*
* // Draw a heart.
* fill('red');
* noStroke();
* circle(67, 67, 20);
* circle(83, 67, 20);
* triangle(91, 73, 75, 95, 59, 73);
*
* // Add a general description of the canvas.
* describe('A pink square with a red heart in the bottom-right corner.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* background('pink');
*
* // Draw a heart.
* fill('red');
* noStroke();
* circle(67, 67, 20);
* circle(83, 67, 20);
* triangle(91, 73, 75, 95, 59, 73);
*
* // Add a general description of the canvas
* // and display it for debugging.
* describe('A pink square with a red heart in the bottom-right corner.', LABEL);
* }
* </code>
* </div>
*
* <div>
* <code>
* function draw() {
* background(200);
*
* // The expression
* // frameCount % 100
* // causes x to increase from 0
* // to 99, then restart from 0.
* let x = frameCount % 100;
*
* // Draw the circle.
* fill(0, 255, 0);
* circle(x, 50, 40);
*
* // Add a general description of the canvas.
* describe(`A green circle at (${x}, 50) moves from left to right on a gray square.`);
* }
* </code>
* </div>
*
* <div>
* <code>
* function draw() {
* background(200);
*
* // The expression
* // frameCount % 100
* // causes x to increase from 0
* // to 99, then restart from 0.
* let x = frameCount % 100;
*
* // Draw the circle.
* fill(0, 255, 0);
* circle(x, 50, 40);
*
* // Add a general description of the canvas
* // and display it for debugging.
* describe(`A green circle at (${x}, 50) moves from left to right on a gray square.`, LABEL);
* }
* </code>
* </div>
*/
p5.prototype.describe = function(text, display) {
p5._validateParameters('describe', arguments);
if (typeof text !== 'string') {
return;
}
const cnvId = this.canvas.id;
//calls function that adds punctuation for better screen reading
text = _descriptionText(text);
//if there is no dummyDOM
if (!this.dummyDOM) {
this.dummyDOM = document.getElementById(cnvId).parentNode;
}
if (!this.descriptions) {
this.descriptions = {};
}
//check if html structure for description is ready
if (this.descriptions.fallback) {
//check if text is different from current description
if (this.descriptions.fallback.innerHTML !== text) {
//update description
this.descriptions.fallback.innerHTML = text;
}
} else {
//create fallback html structure
this._describeHTML('fallback', text);
}
//if display is LABEL
if (display === this.LABEL) {
//check if html structure for label is ready
if (this.descriptions.label) {
//check if text is different from current label
if (this.descriptions.label.innerHTML !== text) {
//update label description
this.descriptions.label.innerHTML = text;
}
} else {
//create label html structure
this._describeHTML('label', text);
}
}
};
/**
* Creates a screen reader-accessible description of elements in the canvas.
*
* Elements are shapes or groups of shapes that create meaning together. For
* example, a few overlapping circles could make an "eye" element.
*
* The first parameter, `name`, is the name of the element.
*
* The second parameter, `text`, is the description of the element.
*
* The third parameter, `display`, is optional. It determines how the
* description is displayed. If `LABEL` is passed, as in
* `describe('A description.', LABEL)`, the description will be visible in
* a div element next to the canvas. Using `LABEL` creates unhelpful
* duplicates for screen readers. Only use `LABEL` during development. If
* `FALLBACK` is passed, as in `describe('A description.', FALLBACK)`, the
* description will only be visible to screen readers. This is the default
* mode.
*
* Read
* <a href="https://p5js.org/tutorials/writing-accessible-canvas-descriptions/">Writing accessible canvas descriptions</a>
* to learn more about making sketches accessible.
*
* @method describeElement
* @param {String} name name of the element.
* @param {String} text description of the element.
* @param {Constant} [display] either LABEL or FALLBACK.
*
* @example
* <div>
* <code>
* function setup() {
* background('pink');
*
* // Describe the first element
* // and draw it.
* describeElement('Circle', 'A yellow circle in the top-left corner.');
* noStroke();
* fill('yellow');
* circle(25, 25, 40);
*
* // Describe the second element
* // and draw it.
* describeElement('Heart', 'A red heart in the bottom-right corner.');
* fill('red');
* circle(66.6, 66.6, 20);
* circle(83.2, 66.6, 20);
* triangle(91.2, 72.6, 75, 95, 58.6, 72.6);
*
* // Add a general description of the canvas.
* describe('A red heart and yellow circle over a pink background.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* background('pink');
*
* // Describe the first element
* // and draw it. Display the
* // description for debugging.
* describeElement('Circle', 'A yellow circle in the top-left corner.', LABEL);
* noStroke();
* fill('yellow');
* circle(25, 25, 40);
*
* // Describe the second element
* // and draw it. Display the
* // description for debugging.
* describeElement('Heart', 'A red heart in the bottom-right corner.', LABEL);
* fill('red');
* circle(66.6, 66.6, 20);
* circle(83.2, 66.6, 20);
* triangle(91.2, 72.6, 75, 95, 58.6, 72.6);
*
* // Add a general description of the canvas.
* describe('A red heart and yellow circle over a pink background.');
* }
* </code>
* </div>
*/
p5.prototype.describeElement = function(name, text, display) {
p5._validateParameters('describeElement', arguments);
if (typeof text !== 'string' || typeof name !== 'string') {
return;
}
const cnvId = this.canvas.id;
//calls function that adds punctuation for better screen reading
text = _descriptionText(text);
//calls function that adds punctuation for better screen reading
let elementName = _elementName(name);
//remove any special characters from name to use it as html id
name = name.replace(/[^a-zA-Z0-9]/g, '');
//store element description
let inner = `<th scope="row">${elementName}</th><td>${text}</td>`;
//if there is no dummyDOM
if (!this.dummyDOM) {
this.dummyDOM = document.getElementById(cnvId).parentNode;
}
if (!this.descriptions) {
this.descriptions = { fallbackElements: {} };
} else if (!this.descriptions.fallbackElements) {
this.descriptions.fallbackElements = {};
}
//check if html structure for element description is ready
if (this.descriptions.fallbackElements[name]) {
//if current element description is not the same as inner
if (this.descriptions.fallbackElements[name].innerHTML !== inner) {
//update element description
this.descriptions.fallbackElements[name].innerHTML = inner;
}
} else {
//create fallback html structure
this._describeElementHTML('fallback', name, inner);
}
//if display is LABEL
if (display === this.LABEL) {
if (!this.descriptions.labelElements) {
this.descriptions.labelElements = {};
}
//if html structure for label element description is ready
if (this.descriptions.labelElements[name]) {
//if label element description is different
if (this.descriptions.labelElements[name].innerHTML !== inner) {
//update label element description
this.descriptions.labelElements[name].innerHTML = inner;
}
} else {
//create label element html structure
this._describeElementHTML('label', name, inner);
}
}
};
/*
*
* Helper functions for describe() and describeElement().
*
*/
// check that text is not LABEL or FALLBACK and ensure text ends with punctuation mark
function _descriptionText(text) {
if (text === 'label' || text === 'fallback') {
throw new Error('description should not be LABEL or FALLBACK');
}
//if string does not end with '.'
if (
!text.endsWith('.') &&
!text.endsWith(';') &&
!text.endsWith(',') &&
!text.endsWith('?') &&
!text.endsWith('!')
) {
//add '.' to the end of string
text = text + '.';
}
return text;
}
/*
* Helper functions for describe()
*/
//creates HTML structure for canvas descriptions
p5.prototype._describeHTML = function(type, text) {
const cnvId = this.canvas.id;
if (type === 'fallback') {
//if there is no description container
if (!this.dummyDOM.querySelector(`#${cnvId + descContainer}`)) {
//if there are no accessible outputs (see textOutput() and gridOutput())
let html = `<div id="${cnvId}${descContainer}" role="region" aria-label="Canvas Description"><p id="${cnvId}${fallbackDescId}"></p></div>`;
if (!this.dummyDOM.querySelector(`#${cnvId}accessibleOutput`)) {
//create description container + <p> for fallback description
this.dummyDOM.querySelector(`#${cnvId}`).innerHTML = html;
} else {
//create description container + <p> for fallback description before outputs
this.dummyDOM
.querySelector(`#${cnvId}accessibleOutput`)
.insertAdjacentHTML('beforebegin', html);
}
} else {
//if describeElement() has already created the container and added a table of elements
//create fallback description <p> before the table
this.dummyDOM
.querySelector('#' + cnvId + fallbackTableId)
.insertAdjacentHTML(
'beforebegin',
`<p id="${cnvId + fallbackDescId}"></p>`
);
}
//if the container for the description exists
this.descriptions.fallback = this.dummyDOM.querySelector(
`#${cnvId}${fallbackDescId}`
);
this.descriptions.fallback.innerHTML = text;
return;
} else if (type === 'label') {
//if there is no label container
if (!this.dummyDOM.querySelector(`#${cnvId + labelContainer}`)) {
let html = `<div id="${cnvId}${labelContainer}" class="p5Label"><p id="${cnvId}${labelDescId}"></p></div>`;
//if there are no accessible outputs (see textOutput() and gridOutput())
if (!this.dummyDOM.querySelector(`#${cnvId}accessibleOutputLabel`)) {
//create label container + <p> for label description
this.dummyDOM
.querySelector('#' + cnvId)
.insertAdjacentHTML('afterend', html);
} else {
//create label container + <p> for label description before outputs
this.dummyDOM
.querySelector(`#${cnvId}accessibleOutputLabel`)
.insertAdjacentHTML('beforebegin', html);
}
} else if (this.dummyDOM.querySelector(`#${cnvId + labelTableId}`)) {
//if describeElement() has already created the container and added a table of elements
//create label description <p> before the table
this.dummyDOM
.querySelector(`#${cnvId + labelTableId}`)
.insertAdjacentHTML(
'beforebegin',
`<p id="${cnvId}${labelDescId}"></p>`
);
}
this.descriptions.label = this.dummyDOM.querySelector(
'#' + cnvId + labelDescId
);
this.descriptions.label.innerHTML = text;
return;
}
};
/*
* Helper functions for describeElement().
*/
//check that name is not LABEL or FALLBACK and ensure text ends with colon
function _elementName(name) {
if (name === 'label' || name === 'fallback') {
throw new Error('element name should not be LABEL or FALLBACK');
}
//check if last character of string n is '.', ';', or ','
if (name.endsWith('.') || name.endsWith(';') || name.endsWith(',')) {
//replace last character with ':'
name = name.replace(/.$/, ':');
} else if (!name.endsWith(':')) {
//if string n does not end with ':'
//add ':'' at the end of string
name = name + ':';
}
return name;
}
//creates HTML structure for element descriptions
p5.prototype._describeElementHTML = function(type, name, text) {
const cnvId = this.canvas.id;
if (type === 'fallback') {
//if there is no description container
if (!this.dummyDOM.querySelector(`#${cnvId + descContainer}`)) {
//if there are no accessible outputs (see textOutput() and gridOutput())
let html = `<div id="${cnvId}${descContainer}" role="region" aria-label="Canvas Description"><table id="${cnvId}${fallbackTableId}"><caption>Canvas elements and their descriptions</caption></table></div>`;
if (!this.dummyDOM.querySelector(`#${cnvId}accessibleOutput`)) {
//create container + table for element descriptions
this.dummyDOM.querySelector('#' + cnvId).innerHTML = html;
} else {
//create container + table for element descriptions before outputs
this.dummyDOM
.querySelector(`#${cnvId}accessibleOutput`)
.insertAdjacentHTML('beforebegin', html);
}
} else if (!this.dummyDOM.querySelector('#' + cnvId + fallbackTableId)) {
//if describe() has already created the container and added a description
//and there is no table create fallback table for element description after
//fallback description
this.dummyDOM
.querySelector('#' + cnvId + fallbackDescId)
.insertAdjacentHTML(
'afterend',
`<table id="${cnvId}${fallbackTableId}"><caption>Canvas elements and their descriptions</caption></table>`
);
}
//create a table row for the element
let tableRow = document.createElement('tr');
tableRow.id = cnvId + fallbackTableElId + name;
this.dummyDOM
.querySelector('#' + cnvId + fallbackTableId)
.appendChild(tableRow);
//update element description
this.descriptions.fallbackElements[name] = this.dummyDOM.querySelector(
`#${cnvId}${fallbackTableElId}${name}`
);
this.descriptions.fallbackElements[name].innerHTML = text;
return;
} else if (type === 'label') {
//If display is LABEL creates a div adjacent to the canvas element with
//a table, a row header cell with the name of the elements,
//and adds the description of the element in adjacent cell.
//if there is no label description container
if (!this.dummyDOM.querySelector(`#${cnvId + labelContainer}`)) {
//if there are no accessible outputs (see textOutput() and gridOutput())
let html = `<div id="${cnvId}${labelContainer}" class="p5Label"><table id="${cnvId}${labelTableId}"></table></div>`;
if (!this.dummyDOM.querySelector(`#${cnvId}accessibleOutputLabel`)) {
//create container + table for element descriptions
this.dummyDOM
.querySelector('#' + cnvId)
.insertAdjacentHTML('afterend', html);
} else {
//create container + table for element descriptions before outputs
this.dummyDOM
.querySelector(`#${cnvId}accessibleOutputLabel`)
.insertAdjacentHTML('beforebegin', html);
}
} else if (!this.dummyDOM.querySelector(`#${cnvId + labelTableId}`)) {
//if describe() has already created the label container and added a description
//and there is no table create label table for element description after
//label description
this.dummyDOM
.querySelector('#' + cnvId + labelDescId)
.insertAdjacentHTML(
'afterend',
`<table id="${cnvId + labelTableId}"></table>`
);
}
//create a table row for the element label description
let tableRow = document.createElement('tr');
tableRow.id = cnvId + labelTableElId + name;
this.dummyDOM
.querySelector('#' + cnvId + labelTableId)
.appendChild(tableRow);
//update element label description
this.descriptions.labelElements[name] = this.dummyDOM.querySelector(
`#${cnvId}${labelTableElId}${name}`
);
this.descriptions.labelElements[name].innerHTML = text;
}
};
export default p5;