Skip to content

Javascript API

Thomas Handorf edited this page May 3, 2018 · 6 revisions

While layerJS can be controlled completely from HTML (through attributes and links) it is still possible to interact through a JavaScript API. This can be helpful if the UI should change based on an event in the app (instead of a user interaction) or if the app needs to react to changes in the UI.

layerJS Methods

select

layerJS.select(selector: string)

get a specific layerJS object (a stage, layer or frame)

Parameter Description
selector a css selector for any layerJS object
Example:
layerJS.select('#layerid');

getState

layerJS.getState(document)

get the state object for a document. See below for what you can do with it.

Parameter Description
document optional document for which you would like to get the state. defaults to window.document
Example:
layerJS.getState();

State Methods

transitionTo

State.transitionTo(state: array, transitionOptions: object|array)

can be used to change the state of one or more layers. This will be animated according to the transitionOptions.

Parameter Description
state an array of paths. A path is a string representing the active frame in a layer. e.g. "stage1.contentlayer.frame2" will make frame2 the active frame in contentlayer which is in stage1. The begining of the path can be omitted if the path is unique e.g. "contentlayer.frame2" or even "frame2"
transitionOptions array of transitons object or a single object:
{
   type: transition type, e.g. "left",
   duration: time in milliseconds,
   startPosition: initial scroll position, e.g. "top"
}
if only one object is given it will be used for all layers that are changed. otherwiese provide one transitionObject for each path your specify.
Example:
layerJS.getState().transitionTo(['menulayer.!none','backdrop.!none','contentlayer.frame2'],{type:'left', duration: '500'});

exportState

State.exportState():array

returns an array of active paths in the current document

Parameter Description
returns: array returns an array of paths. This has the same syntax as the parameter to State.transitionsTo(), eg. ["stage1.contentlayer.frame2","stage1.menulayer.menu"]. You can feed this (or a subset) back into transitionTo.
Example:
layerJS.getState().exportState();

Router Methods

navigate

Router.navigate(url_fragment: string)

can be used to change the state using the link syntax of the hashrouter.

Parameter Description
url_fragment the "hash" part of the url in the format of the hashrouter. e.g. "#contentlayer.frame2;menulayer.!none"
Example:
layerJS.router.navigate('#contentlayer.frame2;menulayer.!none');

Layer Methods

transitionTo

Layer.transitionTo(framename: string, transitionOptions: object)

can be used to change the active frame of a layer. This will be animated according to the transitionOptions.

Parameter Description
framename the name of the frame to which it should transition
transitionOptions an object:
{
   type: transition type, e.g. "left",
   duration: time in milliseconds,
   startPosition: initial scroll position, e.g. "top"
}
Example:
layerJS.select('#layerid').transitionTo('frame1',{type:'left', duration: '500'});

scrollTo

Layer.scrollTo(x: number, y: number, transitionOptions: object)

scroll to a specific position in the current frame

Parameter Description
x horizontal scroll position
y vertical scroll position
transitionOptions an object:
{
   duration: time in milliseconds,
}
Example:
layerJS.select('#layerid').scrollTo(0, 200,{duration: '500'});

Events

stateChanged

State.on('stateChanged', callback: function())

will trigger the callback function whenever thes state is changed.

Example:

layerJS.getState().on('stateChanged', function(){ ... });

transitionPrepared

Layer.on('transitionPrepared', callback: function())

will trigger right before the transition animation actually starts. if more than one transition will be run at the same time those will actually start at the same time.

layerJS.select('menulayer').on('transitionPrepared', function(){ ... });

transitionFinished

Layer.on('transitionFinished', callback: function(framename))

will trigger after the transition animations ends. Will provide the new active framename as parameter.

layerJS.select('menulayer').on('transitionFinished', function(framename){ ... });
Clone this wiki locally
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy