highlighting and arbitrary element or area in a chosen viewport, while shading over the rest of the content
MIT License
spotlight
let's you to visually highlight a chosen element; this is done by shading over the surrounding content
Below is an example of spotlight
used in a callout
framework:
1.5.1
1.4.0
getBoundingClientRect
method to return useful dimensions of the spotted area1.3.2
spotlight
library consists of a single entry-level API,
allowing to create a spotlight-scene
with a given parameters,
applied to the DOM.
Additionally, some constant enumerators provided for convenience.
This component may then be further interacted via component's own APIs as described below, to change its appearance, flavor or spot target, and to be removed at the end of usage.
Each spotlight-scene
is self-contained and isolated,
therefore it is possible to create as many 'spotlights' as needed,
even if in the real use-cases one would rarely need more than a single instance.
Import the library and it's constants as in example below:
import { spotlight, SHAPES } from './dist/spotlight.min.js';
Imported spotlight
is a function syntaxed as below:
const slsElement = function spotlight(target[, container[, options]]) { ... }
target
[optional]
document.body
container
[optional]
target
container
MUST be an ancestor of the target
container
is document.body
options
[optional]
shape
- see shape
property definition below,shadowColor
- see shadowColor
property definition belowtransitionDuration
- see transitionDuration
property definition belowspotlight-scene
component APIsThe base API outlined above serves as an entry point for the interop with the library.
The result of that function is the spotlight-scene
component instance.
It is already applied to the DOM, unless explicitly opted out via the options
above.
This component may by further interacted via it's own APIs. Common use-case for this is to move smoothly the spotlight from one element to another, given that all of them are children of the same parent.
Another obvious need is to remove the spotlight-scene
from the DOM
when not needed anymore.
In all of the further APIs I'll use
sls
term to represent the concretespotlight-scene
instance that the properties and methods belong to.
sls.container
[DOM element] [read only]
container
element that the component was initialized with (see base API above)container
MAY NOT be changedsls.target
[DOM element] - 'spotted' element
target
container
sls.shape
[enum] - shape of the spotlight, defaults to circle
circle
oval
box
SHAPES
enum, like SHAPES.circle
sls.shadowColor
[Array for rgba
(CSS) function] - valid Array for rgba
CSS function; defaults to [0, 0, 0, 0.8]
sls.transitionDuration
[number] - duration in millis of spotlight's transitions (move from target to target, shape change, etc); defaults to 333
sls.close()
Promise
, resolved when all donespotlight-scene
component and performs all relevant cleanupssls.moveTo(targetElement)
Promise
, resolved when move it finishedtargetElement
subject to the same constraints target
property abovesls.getBoundingClientRect()
The flow below exemplifies typical usage of the library:
const t1 = <... the element to be spotted>;
const t2 = <... another one>;
const t3 = <... another one>;
const sl = spotlight(t1); // the spotlight is shown now
...
sl.target = t2; // spotlight moved to a new target
sl.style.color = '#110'; // color of the shade is adjusted...
sl.shape = SHAPES.oval; // ... and spot's shape too
...
sl.transitionDuration = 500; // slow it down a bit
sl.moveTo(t3)
.then(() => console.log('spotlight moved, do something...'));
sl.close();