Socket
Socket
Sign inDemoInstall

@pixi/accessibility

Package Overview
Dependencies
Maintainers
2
Versions
122
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@pixi/accessibility - npm Package Compare versions

Comparing version 7.2.4 to 7.3.0-rc

342

lib/AccessibilityManager.js

@@ -1,174 +0,99 @@

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var core = require('@pixi/core');
var display = require('@pixi/display');
var events = require('@pixi/events');
var accessibleTarget = require('./accessibleTarget.js');
"use strict";
var core = require("@pixi/core"), display = require("@pixi/display"), events = require("@pixi/events"), accessibleTarget = require("./accessibleTarget.js");
display.DisplayObject.mixin(accessibleTarget.accessibleTarget);
const KEY_CODE_TAB = 9;
const DIV_TOUCH_SIZE = 100;
const DIV_TOUCH_POS_X = 0;
const DIV_TOUCH_POS_Y = 0;
const DIV_TOUCH_ZINDEX = 2;
const DIV_HOOK_SIZE = 1;
const DIV_HOOK_POS_X = -1e3;
const DIV_HOOK_POS_Y = -1e3;
const DIV_HOOK_ZINDEX = 2;
const KEY_CODE_TAB = 9, DIV_TOUCH_SIZE = 100, DIV_TOUCH_POS_X = 0, DIV_TOUCH_POS_Y = 0, DIV_TOUCH_ZINDEX = 2, DIV_HOOK_SIZE = 1, DIV_HOOK_POS_X = -1e3, DIV_HOOK_POS_Y = -1e3, DIV_HOOK_ZINDEX = 2;
class AccessibilityManager {
// 2fps
/**
* @param {PIXI.CanvasRenderer|PIXI.Renderer} renderer - A reference to the current renderer
*/
constructor(renderer) {
this.debug = false;
this._isActive = false;
this._isMobileAccessibility = false;
this.pool = [];
this.renderId = 0;
this.children = [];
this.androidUpdateCount = 0;
this.androidUpdateFrequency = 500;
this._hookDiv = null;
if (core.utils.isMobile.tablet || core.utils.isMobile.phone) {
this.createTouchHook();
}
this.debug = !1, this._isActive = !1, this._isMobileAccessibility = !1, this.pool = [], this.renderId = 0, this.children = [], this.androidUpdateCount = 0, this.androidUpdateFrequency = 500, this._hookDiv = null, (core.utils.isMobile.tablet || core.utils.isMobile.phone) && this.createTouchHook();
const div = document.createElement("div");
div.style.width = `${DIV_TOUCH_SIZE}px`;
div.style.height = `${DIV_TOUCH_SIZE}px`;
div.style.position = "absolute";
div.style.top = `${DIV_TOUCH_POS_X}px`;
div.style.left = `${DIV_TOUCH_POS_Y}px`;
div.style.zIndex = DIV_TOUCH_ZINDEX.toString();
this.div = div;
this.renderer = renderer;
this._onKeyDown = this._onKeyDown.bind(this);
this._onMouseMove = this._onMouseMove.bind(this);
globalThis.addEventListener("keydown", this._onKeyDown, false);
div.style.width = `${DIV_TOUCH_SIZE}px`, div.style.height = `${DIV_TOUCH_SIZE}px`, div.style.position = "absolute", div.style.top = `${DIV_TOUCH_POS_X}px`, div.style.left = `${DIV_TOUCH_POS_Y}px`, div.style.zIndex = DIV_TOUCH_ZINDEX.toString(), this.div = div, this.renderer = renderer, this._onKeyDown = this._onKeyDown.bind(this), this._onMouseMove = this._onMouseMove.bind(this), globalThis.addEventListener("keydown", this._onKeyDown, !1);
}
/**
* Value of `true` if accessibility is currently active and accessibility layers are showing.
* @member {boolean}
* @readonly
*/
get isActive() {
return this._isActive;
}
/**
* Value of `true` if accessibility is enabled for touch devices.
* @member {boolean}
* @readonly
*/
get isMobileAccessibility() {
return this._isMobileAccessibility;
}
/**
* Creates the touch hooks.
* @private
*/
createTouchHook() {
const hookDiv = document.createElement("button");
hookDiv.style.width = `${DIV_HOOK_SIZE}px`;
hookDiv.style.height = `${DIV_HOOK_SIZE}px`;
hookDiv.style.position = "absolute";
hookDiv.style.top = `${DIV_HOOK_POS_X}px`;
hookDiv.style.left = `${DIV_HOOK_POS_Y}px`;
hookDiv.style.zIndex = DIV_HOOK_ZINDEX.toString();
hookDiv.style.backgroundColor = "#FF0000";
hookDiv.title = "select to enable accessibility for this content";
hookDiv.addEventListener("focus", () => {
this._isMobileAccessibility = true;
this.activate();
this.destroyTouchHook();
});
document.body.appendChild(hookDiv);
this._hookDiv = hookDiv;
hookDiv.style.width = `${DIV_HOOK_SIZE}px`, hookDiv.style.height = `${DIV_HOOK_SIZE}px`, hookDiv.style.position = "absolute", hookDiv.style.top = `${DIV_HOOK_POS_X}px`, hookDiv.style.left = `${DIV_HOOK_POS_Y}px`, hookDiv.style.zIndex = DIV_HOOK_ZINDEX.toString(), hookDiv.style.backgroundColor = "#FF0000", hookDiv.title = "select to enable accessibility for this content", hookDiv.addEventListener("focus", () => {
this._isMobileAccessibility = !0, this.activate(), this.destroyTouchHook();
}), document.body.appendChild(hookDiv), this._hookDiv = hookDiv;
}
/**
* Destroys the touch hooks.
* @private
*/
destroyTouchHook() {
if (!this._hookDiv) {
return;
}
document.body.removeChild(this._hookDiv);
this._hookDiv = null;
this._hookDiv && (document.body.removeChild(this._hookDiv), this._hookDiv = null);
}
/**
* Activating will cause the Accessibility layer to be shown.
* This is called when a user presses the tab key.
* @private
*/
activate() {
if (this._isActive) {
return;
}
this._isActive = true;
globalThis.document.addEventListener("mousemove", this._onMouseMove, true);
globalThis.removeEventListener("keydown", this._onKeyDown, false);
this.renderer.on("postrender", this.update, this);
this.renderer.view.parentNode?.appendChild(this.div);
this._isActive || (this._isActive = !0, globalThis.document.addEventListener("mousemove", this._onMouseMove, !0), globalThis.removeEventListener("keydown", this._onKeyDown, !1), this.renderer.on("postrender", this.update, this), this.renderer.view.parentNode?.appendChild(this.div));
}
/**
* Deactivating will cause the Accessibility layer to be hidden.
* This is called when a user moves the mouse.
* @private
*/
deactivate() {
if (!this._isActive || this._isMobileAccessibility) {
return;
}
this._isActive = false;
globalThis.document.removeEventListener("mousemove", this._onMouseMove, true);
globalThis.addEventListener("keydown", this._onKeyDown, false);
this.renderer.off("postrender", this.update);
this.div.parentNode?.removeChild(this.div);
!this._isActive || this._isMobileAccessibility || (this._isActive = !1, globalThis.document.removeEventListener("mousemove", this._onMouseMove, !0), globalThis.addEventListener("keydown", this._onKeyDown, !1), this.renderer.off("postrender", this.update), this.div.parentNode?.removeChild(this.div));
}
/**
* This recursive function will run through the scene graph and add any new accessible objects to the DOM layer.
* @private
* @param {PIXI.Container} displayObject - The DisplayObject to check.
*/
updateAccessibleObjects(displayObject) {
if (!displayObject.visible || !displayObject.accessibleChildren) {
if (!displayObject.visible || !displayObject.accessibleChildren)
return;
}
if (displayObject.accessible && displayObject.isInteractive()) {
if (!displayObject._accessibleActive) {
this.addChild(displayObject);
}
displayObject.renderId = this.renderId;
}
displayObject.accessible && displayObject.isInteractive() && (displayObject._accessibleActive || this.addChild(displayObject), displayObject.renderId = this.renderId);
const children = displayObject.children;
if (children) {
for (let i = 0; i < children.length; i++) {
if (children)
for (let i = 0; i < children.length; i++)
this.updateAccessibleObjects(children[i]);
}
}
}
/**
* Before each render this function will ensure that all divs are mapped correctly to their DisplayObjects.
* @private
*/
update() {
const now = performance.now();
if (core.utils.isMobile.android.device && now < this.androidUpdateCount) {
if (core.utils.isMobile.android.device && now < this.androidUpdateCount || (this.androidUpdateCount = now + this.androidUpdateFrequency, !this.renderer.renderingToScreen))
return;
}
this.androidUpdateCount = now + this.androidUpdateFrequency;
if (!this.renderer.renderingToScreen) {
return;
}
if (this.renderer.lastObjectRendered) {
this.updateAccessibleObjects(this.renderer.lastObjectRendered);
}
const { x, y, width, height } = this.renderer.view.getBoundingClientRect();
const { width: viewWidth, height: viewHeight, resolution } = this.renderer;
const sx = width / viewWidth * resolution;
const sy = height / viewHeight * resolution;
this.renderer.lastObjectRendered && this.updateAccessibleObjects(this.renderer.lastObjectRendered);
const { x, y, width, height } = this.renderer.view.getBoundingClientRect(), { width: viewWidth, height: viewHeight, resolution } = this.renderer, sx = width / viewWidth * resolution, sy = height / viewHeight * resolution;
let div = this.div;
div.style.left = `${x}px`;
div.style.top = `${y}px`;
div.style.width = `${viewWidth}px`;
div.style.height = `${viewHeight}px`;
div.style.left = `${x}px`, div.style.top = `${y}px`, div.style.width = `${viewWidth}px`, div.style.height = `${viewHeight}px`;
for (let i = 0; i < this.children.length; i++) {
const child = this.children[i];
if (child.renderId !== this.renderId) {
child._accessibleActive = false;
core.utils.removeItems(this.children, i, 1);
this.div.removeChild(child._accessibleDiv);
this.pool.push(child._accessibleDiv);
child._accessibleDiv = null;
i--;
} else {
if (child.renderId !== this.renderId)
child._accessibleActive = !1, core.utils.removeItems(this.children, i, 1), this.div.removeChild(child._accessibleDiv), this.pool.push(child._accessibleDiv), child._accessibleDiv = null, i--;
else {
div = child._accessibleDiv;
let hitArea = child.hitArea;
const wt = child.worldTransform;
if (child.hitArea) {
div.style.left = `${(wt.tx + hitArea.x * wt.a) * sx}px`;
div.style.top = `${(wt.ty + hitArea.y * wt.d) * sy}px`;
div.style.width = `${hitArea.width * wt.a * sx}px`;
div.style.height = `${hitArea.height * wt.d * sy}px`;
} else {
hitArea = child.getBounds();
this.capHitArea(hitArea);
div.style.left = `${hitArea.x * sx}px`;
div.style.top = `${hitArea.y * sy}px`;
div.style.width = `${hitArea.width * sx}px`;
div.style.height = `${hitArea.height * sy}px`;
if (div.title !== child.accessibleTitle && child.accessibleTitle !== null) {
div.title = child.accessibleTitle;
}
if (div.getAttribute("aria-label") !== child.accessibleHint && child.accessibleHint !== null) {
div.setAttribute("aria-label", child.accessibleHint);
}
}
if (child.accessibleTitle !== div.title || child.tabIndex !== div.tabIndex) {
div.title = child.accessibleTitle;
div.tabIndex = child.tabIndex;
if (this.debug)
this.updateDebugHTML(div);
}
child.hitArea ? (div.style.left = `${(wt.tx + hitArea.x * wt.a) * sx}px`, div.style.top = `${(wt.ty + hitArea.y * wt.d) * sy}px`, div.style.width = `${hitArea.width * wt.a * sx}px`, div.style.height = `${hitArea.height * wt.d * sy}px`) : (hitArea = child.getBounds(), this.capHitArea(hitArea), div.style.left = `${hitArea.x * sx}px`, div.style.top = `${hitArea.y * sy}px`, div.style.width = `${hitArea.width * sx}px`, div.style.height = `${hitArea.height * sy}px`, div.title !== child.accessibleTitle && child.accessibleTitle !== null && (div.title = child.accessibleTitle), div.getAttribute("aria-label") !== child.accessibleHint && child.accessibleHint !== null && div.setAttribute("aria-label", child.accessibleHint)), (child.accessibleTitle !== div.title || child.tabIndex !== div.tabIndex) && (div.title = child.accessibleTitle, div.tabIndex = child.tabIndex, this.debug && this.updateDebugHTML(div));
}

@@ -178,107 +103,81 @@ }

}
/**
* private function that will visually add the information to the
* accessability div
* @param {HTMLElement} div -
*/
updateDebugHTML(div) {
div.innerHTML = `type: ${div.type}</br> title : ${div.title}</br> tabIndex: ${div.tabIndex}`;
}
/**
* Adjust the hit area based on the bounds of a display object
* @param {PIXI.Rectangle} hitArea - Bounds of the child
*/
capHitArea(hitArea) {
if (hitArea.x < 0) {
hitArea.width += hitArea.x;
hitArea.x = 0;
}
if (hitArea.y < 0) {
hitArea.height += hitArea.y;
hitArea.y = 0;
}
hitArea.x < 0 && (hitArea.width += hitArea.x, hitArea.x = 0), hitArea.y < 0 && (hitArea.height += hitArea.y, hitArea.y = 0);
const { width: viewWidth, height: viewHeight } = this.renderer;
if (hitArea.x + hitArea.width > viewWidth) {
hitArea.width = viewWidth - hitArea.x;
}
if (hitArea.y + hitArea.height > viewHeight) {
hitArea.height = viewHeight - hitArea.y;
}
hitArea.x + hitArea.width > viewWidth && (hitArea.width = viewWidth - hitArea.x), hitArea.y + hitArea.height > viewHeight && (hitArea.height = viewHeight - hitArea.y);
}
/**
* Adds a DisplayObject to the accessibility manager
* @private
* @param {PIXI.DisplayObject} displayObject - The child to make accessible.
*/
addChild(displayObject) {
let div = this.pool.pop();
if (!div) {
div = document.createElement("button");
div.style.width = `${DIV_TOUCH_SIZE}px`;
div.style.height = `${DIV_TOUCH_SIZE}px`;
div.style.backgroundColor = this.debug ? "rgba(255,255,255,0.5)" : "transparent";
div.style.position = "absolute";
div.style.zIndex = DIV_TOUCH_ZINDEX.toString();
div.style.borderStyle = "none";
if (navigator.userAgent.toLowerCase().includes("chrome")) {
div.setAttribute("aria-live", "off");
} else {
div.setAttribute("aria-live", "polite");
}
if (navigator.userAgent.match(/rv:.*Gecko\//)) {
div.setAttribute("aria-relevant", "additions");
} else {
div.setAttribute("aria-relevant", "text");
}
div.addEventListener("click", this._onClick.bind(this));
div.addEventListener("focus", this._onFocus.bind(this));
div.addEventListener("focusout", this._onFocusOut.bind(this));
}
div.style.pointerEvents = displayObject.accessiblePointerEvents;
div.type = displayObject.accessibleType;
if (displayObject.accessibleTitle && displayObject.accessibleTitle !== null) {
div.title = displayObject.accessibleTitle;
} else if (!displayObject.accessibleHint || displayObject.accessibleHint === null) {
div.title = `displayObject ${displayObject.tabIndex}`;
}
if (displayObject.accessibleHint && displayObject.accessibleHint !== null) {
div.setAttribute("aria-label", displayObject.accessibleHint);
}
if (this.debug)
this.updateDebugHTML(div);
displayObject._accessibleActive = true;
displayObject._accessibleDiv = div;
div.displayObject = displayObject;
this.children.push(displayObject);
this.div.appendChild(displayObject._accessibleDiv);
displayObject._accessibleDiv.tabIndex = displayObject.tabIndex;
div || (div = document.createElement("button"), div.style.width = `${DIV_TOUCH_SIZE}px`, div.style.height = `${DIV_TOUCH_SIZE}px`, div.style.backgroundColor = this.debug ? "rgba(255,255,255,0.5)" : "transparent", div.style.position = "absolute", div.style.zIndex = DIV_TOUCH_ZINDEX.toString(), div.style.borderStyle = "none", navigator.userAgent.toLowerCase().includes("chrome") ? div.setAttribute("aria-live", "off") : div.setAttribute("aria-live", "polite"), navigator.userAgent.match(/rv:.*Gecko\//) ? div.setAttribute("aria-relevant", "additions") : div.setAttribute("aria-relevant", "text"), div.addEventListener("click", this._onClick.bind(this)), div.addEventListener("focus", this._onFocus.bind(this)), div.addEventListener("focusout", this._onFocusOut.bind(this))), div.style.pointerEvents = displayObject.accessiblePointerEvents, div.type = displayObject.accessibleType, displayObject.accessibleTitle && displayObject.accessibleTitle !== null ? div.title = displayObject.accessibleTitle : (!displayObject.accessibleHint || displayObject.accessibleHint === null) && (div.title = `displayObject ${displayObject.tabIndex}`), displayObject.accessibleHint && displayObject.accessibleHint !== null && div.setAttribute("aria-label", displayObject.accessibleHint), this.debug && this.updateDebugHTML(div), displayObject._accessibleActive = !0, displayObject._accessibleDiv = div, div.displayObject = displayObject, this.children.push(displayObject), this.div.appendChild(displayObject._accessibleDiv), displayObject._accessibleDiv.tabIndex = displayObject.tabIndex;
}
/**
* Dispatch events with the EventSystem.
* @param e
* @param type
* @private
*/
_dispatchEvent(e, type) {
const { displayObject: target } = e.target;
const boundry = this.renderer.events.rootBoundary;
const event = Object.assign(new events.FederatedEvent(boundry), { target });
boundry.rootTarget = this.renderer.lastObjectRendered;
type.forEach((type2) => boundry.dispatchEvent(event, type2));
const { displayObject: target } = e.target, boundry = this.renderer.events.rootBoundary, event = Object.assign(new events.FederatedEvent(boundry), { target });
boundry.rootTarget = this.renderer.lastObjectRendered, type.forEach((type2) => boundry.dispatchEvent(event, type2));
}
/**
* Maps the div button press to pixi's EventSystem (click)
* @private
* @param {MouseEvent} e - The click event.
*/
_onClick(e) {
this._dispatchEvent(e, ["click", "pointertap", "tap"]);
}
/**
* Maps the div focus events to pixi's EventSystem (mouseover)
* @private
* @param {FocusEvent} e - The focus event.
*/
_onFocus(e) {
if (!e.target.getAttribute("aria-live")) {
e.target.setAttribute("aria-live", "assertive");
}
this._dispatchEvent(e, ["mouseover"]);
e.target.getAttribute("aria-live") || e.target.setAttribute("aria-live", "assertive"), this._dispatchEvent(e, ["mouseover"]);
}
/**
* Maps the div focus events to pixi's EventSystem (mouseout)
* @private
* @param {FocusEvent} e - The focusout event.
*/
_onFocusOut(e) {
if (!e.target.getAttribute("aria-live")) {
e.target.setAttribute("aria-live", "polite");
}
this._dispatchEvent(e, ["mouseout"]);
e.target.getAttribute("aria-live") || e.target.setAttribute("aria-live", "polite"), this._dispatchEvent(e, ["mouseout"]);
}
/**
* Is called when a key is pressed
* @private
* @param {KeyboardEvent} e - The keydown event.
*/
_onKeyDown(e) {
if (e.keyCode !== KEY_CODE_TAB) {
return;
}
this.activate();
e.keyCode === KEY_CODE_TAB && this.activate();
}
/**
* Is called when the mouse moves across the renderer element
* @private
* @param {MouseEvent} e - The mouse event.
*/
_onMouseMove(e) {
if (e.movementX === 0 && e.movementY === 0) {
return;
}
this.deactivate();
e.movementX === 0 && e.movementY === 0 || this.deactivate();
}
/** Destroys the accessibility manager */
destroy() {
this.destroyTouchHook();
this.div = null;
globalThis.document.removeEventListener("mousemove", this._onMouseMove, true);
globalThis.removeEventListener("keydown", this._onKeyDown);
this.pool = null;
this.children = null;
this.renderer = null;
this.destroyTouchHook(), this.div = null, globalThis.document.removeEventListener("mousemove", this._onMouseMove, !0), globalThis.removeEventListener("keydown", this._onKeyDown), this.pool = null, this.children = null, this.renderer = null;
}

@@ -294,4 +193,3 @@ }

core.extensions.add(AccessibilityManager);
exports.AccessibilityManager = AccessibilityManager;
//# sourceMappingURL=AccessibilityManager.js.map

@@ -1,19 +0,69 @@

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
"use strict";
const accessibleTarget = {
accessible: false,
/**
* Flag for if the object is accessible. If true AccessibilityManager will overlay a
* shadow div with attributes set
* @member {boolean}
* @memberof PIXI.DisplayObject#
*/
accessible: !1,
/**
* Sets the title attribute of the shadow div
* If accessibleTitle AND accessibleHint has not been this will default to 'displayObject [tabIndex]'
* @member {?string}
* @memberof PIXI.DisplayObject#
*/
accessibleTitle: null,
/**
* Sets the aria-label attribute of the shadow div
* @member {string}
* @memberof PIXI.DisplayObject#
*/
accessibleHint: null,
/**
* @member {number}
* @memberof PIXI.DisplayObject#
* @private
* @todo Needs docs.
*/
tabIndex: 0,
_accessibleActive: false,
/**
* @member {boolean}
* @memberof PIXI.DisplayObject#
* @todo Needs docs.
*/
_accessibleActive: !1,
/**
* @member {boolean}
* @memberof PIXI.DisplayObject#
* @todo Needs docs.
*/
_accessibleDiv: null,
/**
* Specify the type of div the accessible layer is. Screen readers treat the element differently
* depending on this type. Defaults to button.
* @member {string}
* @memberof PIXI.DisplayObject#
* @default 'button'
*/
accessibleType: "button",
/**
* Specify the pointer-events the accessible div will use
* Defaults to auto.
* @member {string}
* @memberof PIXI.DisplayObject#
* @default 'auto'
*/
accessiblePointerEvents: "auto",
accessibleChildren: true,
/**
* Setting to false will prevent any children inside this container to
* be accessible. Defaults to true.
* @member {boolean}
* @memberof PIXI.DisplayObject#
* @default true
*/
accessibleChildren: !0,
renderId: -1
};
exports.accessibleTarget = accessibleTarget;
//# sourceMappingURL=accessibleTarget.js.map

@@ -1,12 +0,5 @@

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var AccessibilityManager = require('./AccessibilityManager.js');
var accessibleTarget = require('./accessibleTarget.js');
"use strict";
var AccessibilityManager = require("./AccessibilityManager.js"), accessibleTarget = require("./accessibleTarget.js");
exports.AccessibilityManager = AccessibilityManager.AccessibilityManager;
exports.accessibleTarget = accessibleTarget.accessibleTarget;
//# sourceMappingURL=index.js.map
{
"name": "@pixi/accessibility",
"version": "7.2.4",
"version": "7.3.0-rc",
"main": "lib/index.js",

@@ -39,6 +39,6 @@ "module": "lib/index.mjs",

"peerDependencies": {
"@pixi/core": "7.2.4",
"@pixi/display": "7.2.4",
"@pixi/events": "7.2.4"
"@pixi/core": "7.3.0-rc",
"@pixi/display": "7.3.0-rc",
"@pixi/events": "7.3.0-rc"
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc