1.0.5 • Published 1 month ago

@a11y-ngx/responsive-image-maps v1.0.5

Weekly downloads
-
License
MIT
Repository
-
Last release
1 month ago

Responsive Image Maps

A responsive image map directive that updates the areas' coordinates according to the image size.

⚠️ IMPORTANT: Currently rect and circle shapes are supported, poly will be supported in future releases.

This library was generated with Angular CLI version 12.2.0.

Installation

  1. Install npm package:

    npm install @a11y-ngx/responsive-image-maps --save

  2. Import A11yResponsiveImageMapsModule into your module:

import { A11yResponsiveImageMapsModule } from '@a11y-ngx/responsive-image-maps';

@NgModule({
    declarations: [...],
    imports: [
        ...
        A11yResponsiveImageMapsModule
    ]
})
export class AppModule { }

Use

Create an image map.

The Map Directive

  • The Map directive will pick any <map> tag with [name] attribute in it and search for the <img> with [usemap] matching that name.
  • When the main <img> resize, all <area> elements will update their coordinates based on the new image size.
  • The directive can be accessed using a template variable with responsiveImageMap.
  • The directive will auto update sizes when the page resize (using Window Resize Service).
  • If the image changes size (through window resize), (sizeChanged) output will emit the new MapSize.
  • If the image changes size (programmatically), sizes can be manually updated using the update() method from the exported directive.
  • The HTML <map> element can be reached through the nativeElement property.

Map Directive public Properties, Getters and Methods

PropertyTypeReturnsDescription
loadedBehaviorSubjectbooleanWill return true when the image has loaded
sizeChangedEventEmitterMapSizeWill emit the new image's size & position when page resize and it changes the width and height values
areasQueryListResponsiveImageAreaDirectiveTo access all <area> children directives
scaleFactorgetter{ width: number, height: number }The scale factor from the image's original full size and the resized size
imageSizeMapSizeobjectTo access all the values (size & position) from the main image
nativeElementgetterHTMLMapElementTo access the HTML <map> element
imageElementgetterHTMLImageElementTo access the HTML <img> element that the <map> is attached to
update()methodvoidWill update the image new values and all the <area>'s new coordinates accordingly

The MapSize Interface

PropertyTypeDescription
topnumberThe image DOMRect's top value
leftnumberThe image DOMRect's left value
widthnumberThe image DOMRect's width value
heightnumberThe image DOMRect's height value
fullWidthnumberThe image's original full width size (resized or not)
fullHeightnumberThe image's original full height size (resized or not)

The Area Directive

  • The Area directive will pick any <area> tag with [shape] attribute of value "rect" or "circle".
  • This directive will manage the area size (either relative to the image or to the viewport).
  • These directives can be accessed from the Map directive through the areas property, which will return a QueryList<ResponsiveImageAreaDirective>.
  • The HTML <area> element can be reached through the nativeElement property.

Area Directive public Properties, Getters and Methods

PropertyTypeReturnsDescription
areaSizeAreaSizeobjectTo access all the values (size & position) from the area (relative to the image)
nativeElementgetterHTMLAreaElementTo access the HTML <area> element
getBoundingClientRect()methodDOMRectTo get the calculated position of the <area> element (relative to the viewport)
updateCoords()methodvoidWill update the [coords] attribute based on the image size. (NOTE: there's no need to trigger this manually, the map directive will update when needed)

The AreaSize Interface

PropertyTypeDescription
topnumberThe area's (relative to image) top value
leftnumberThe area's (relative to image) left value
widthnumberThe area's (relative to image) width value
heightnumberThe area's (relative to image) height value

Working Example

TypeScript

import { ResponsiveImageMapDirective, MapSize, AreaSize } from '@a11y-ngx/responsive-image-maps';

@Component({ ... })
export class MyComponent {
    @ViewChild('imageMap') imageMap: ResponsiveImageMapDirective;

    updateOtherElements(newSize: MapSize): void {
        const width = newSize.width;
        const height = newSize.height;
        ...

        this.imageMap.areas.forEach(area => {
            const areaElement = area.nativeElement;
            const areaSize: AreaSize = area.AreaSize;
            ...
        });
    }

    updateMapSizes(): void {
        this.imageMap.update();
    }
}

HTML

<img src="https://www.w3schools.com/html/workplace.jpg" alt="Workplace" usemap="#workmap">

<map name="workmap" #imageMap="responsiveImageMap" (sizeChanged)="updateOtherElements($event)">
    <area shape="rect" coords="34,44,270,350" alt="Computer" href="...">
    <area shape="rect" coords="290,172,333,250" alt="Phone" href="...">
    <area shape="circle" coords="337,300,44" alt="Coffee" href="...">
</map>
1.0.2

1 month ago

1.0.5

1 month ago

1.0.4

1 month ago

1.0.3

1 month ago

1.0.1

2 years ago

1.0.0

2 years ago