Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ampproject/amphtml] ♿ [Story a11y] aria-label on amp-story-360 canvas patch #32918

Merged
merged 4 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/amp-story/amp-story-360.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<amp-story-page id="cover">
<amp-story-grid-layer template="fill">
<amp-story-360 layout="fill" heading-start="30" pitch-start="20" heading-end="-20" pitch-end="10" duration="5s" controls="gyroscope">
<amp-video poster="img/nasa_training_poster.jpg" autoplay layout="fill" loop alt-text="Astronaut space walk training">
<amp-video poster="img/nasa_training_poster.jpg" autoplay layout="fill" loop>
<!-- Credit: NASA -->
<!-- https://commons.wikimedia.org/wiki/File:NASA_VR-360_Astronaut_Training-_Space_Walk.webm -->
<source src="img/NASA_VR_360_Astronaut_Training_Space_Walk_Clip.mp4" type="video/mp4" />
Expand All @@ -35,14 +35,14 @@
scene-pitch="-5">
<!-- Curiosity self-portrat, Sol 1462 (September 2016) Credit: NASA / JPL / MSSS / Seán Doran -->
<!-- https://informal.jpl.nasa.gov/museum/360-video -->
<amp-img src="img/SeanDoran-Quela-sol1462-edited_ver2-sm.jpg" width="1998" height="999" alt-text="Curiosity self-portrait"></amp-img>
<amp-img src="img/SeanDoran-Quela-sol1462-edited_ver2-sm.jpg" width="1998" height="999" title="Curiosity self-portrait"></amp-img>
</amp-story-360>
</amp-story-grid-layer>
</amp-story-page>
<amp-story-page id="page2">
<amp-story-grid-layer template="fill">
<amp-story-360 layout="fill" heading-start="95" pitch-start="-10" heading-end="-60" pitch-end="-20" duration="3s" controls="gyroscope">
<amp-img src="img/Equirectangular_projection_SW.jpg" width="2058" height="1036" alt-text="globe"></amp-img>
<amp-img src="img/Equirectangular_projection_SW.jpg" width="2058" height="1036" aria-label="globe"></amp-img>
</amp-story-360>
</amp-story-grid-layer>
</amp-story-page>
Expand Down
28 changes: 20 additions & 8 deletions extensions/amp-story-360/0.1/amp-story-360.js
Original file line number Diff line number Diff line change
Expand Up @@ -674,17 +674,13 @@ export class AmpStory360 extends AMP.BaseElement {
// Used to update the video in animate_.
this.ampVideoEl_ = this.element.querySelector('amp-video');

const mediaEl = ampImgEl || this.ampVideoEl_;
userAssert(
ampImgEl || this.ampVideoEl_,
mediaEl,
'amp-story-360 must contain an amp-img or amp-video element.'
);

// Puts alt-text on canvas element so it can be read by screen readers.
// The media element is hidden with CSS it no longer can read it.
const altText = (ampImgEl || this.ampVideoEl_).getAttribute('alt-text');
if (altText) {
this.canvas_.setAttribute('role', 'img');
this.canvas_.setAttribute('aria-label', altText);
if (mediaEl) {
this.setAccessibleText_(mediaEl);
}

if (ampImgEl) {
Expand All @@ -695,6 +691,22 @@ export class AmpStory360 extends AMP.BaseElement {
}
}

/**
* Puts a11y text on canvas element so it can be read by screen readers.
* The media element is hidden by CSS it no longer can read it.
* @param {!Element} mediaEl Either an amp-img or amp-video
* @private
*/
setAccessibleText_(mediaEl) {
const altTags = ['alt', 'title', 'aria-label']; /** In order of priority. */
const altTag = altTags.find((attr) => mediaEl.getAttribute(attr));
if (altTag) {
const altText = mediaEl.getAttribute(altTag);
this.canvas_.setAttribute('role', 'img');
this.canvas_.setAttribute('aria-label', altText);
}
}

/**
* @param {!Element} ampImgEl
* @return {!Promise}
Expand Down