iwmlib/lib/pixi/volatile.js

159 lines
4.5 KiB
JavaScript
Raw Permalink Normal View History

2019-03-21 09:57:27 +01:00
/* global Quad */
import Theme from './theme.js'
/**
* Class that represents a PixiJS Volatile.
2019-07-18 12:26:39 +02:00
*
2019-03-21 09:57:27 +01:00
* @example
* const app = new PIXIApp({
* view: canvas,
* width: 900,
* height: 250
* }).setup().run()
2019-07-18 12:26:39 +02:00
*
2019-03-21 09:57:27 +01:00
* const button = new Button({
* label: 'Volatile!',
* action: () => {
* new Volatile({
* object: button,
* direction: 'right',
* destroyOnComplete: false
* })
* }
* })
2019-07-18 12:26:39 +02:00
*
2019-03-21 09:57:27 +01:00
* app.scene.addChild(button)
*
* @class
* @see {@link https://www.iwm-tuebingen.de/iwmbrowser/lib/pixi/volatile.html|DocTest}
*/
export default class Volatile {
/**
* Creates an instance of a Volatile.
2019-07-18 12:26:39 +02:00
*
2019-03-21 09:57:27 +01:00
* @constructor
* @param {object} [opts] - An options object to specify to style and behaviour of the modal.
* @param {number} [opts.id=auto generated] - The id of the tooltip.
* @param {PIXI.DisplayObject|PIXI.DisplayObject[]} opts.object - The object where the volatile should be applied to.
* @param {string} [opts.direction=top] - The animation direction. Possible values: top, right, bottom, left.
* @param {function} [opts.onStart] - A function which is executed when the volatile animation starts.
* @param {function} [opts.onComplete] - A function which is executed when the volatile animation finishes.
* @param {number} [opts.distance=160] - The animation distance.
* @param {number} [opts.duration=1.5] - The duration of the animation in seconds.
* @param {object} [opts.ease=Quad.easeOut] - The easing of the animation, see {@link https://greensock.com/docs/Easing}
* @param {boolean} [opts.destroyOnComplete=true] - Should the object be destroyed after the volatile animation?
*/
constructor(opts = {}) {
const theme = Theme.fromString(opts.theme)
this.theme = theme
2019-07-18 12:26:39 +02:00
this.opts = Object.assign(
{},
{
id: PIXI.utils.uid(),
object: null,
direction: 'top', // top, right, bottom, left
onStart: null,
onComplete: null,
distance: 160,
duration: 1.5,
ease: Quad.easeOut,
destroyOnComplete: true
},
opts
)
2019-03-21 09:57:27 +01:00
this.id = this.opts.id
if (!Array.isArray(this.opts.object)) {
this.opts.object = [this.opts.object]
}
this.objects = this.opts.object
// setup
//-----------------
this.setup()
// layout
//-----------------
this.layout()
// run
//-----------------
this.run()
}
2019-07-18 12:26:39 +02:00
2019-03-21 09:57:27 +01:00
/**
* Creates children and instantiates everything.
2019-07-18 12:26:39 +02:00
*
2019-03-21 09:57:27 +01:00
* @private
* @return {Volatile} A reference to the volatile for chaining.
*/
setup() {
return this
}
2019-07-18 12:26:39 +02:00
2019-03-21 09:57:27 +01:00
/**
* Should be called to refresh the layout of the volatile. Can be used after resizing.
2019-07-18 12:26:39 +02:00
*
2019-03-21 09:57:27 +01:00
* @return {Volatile} A reference to the volatile for chaining.
*/
layout() {
return this
}
2019-07-18 12:26:39 +02:00
2019-03-21 09:57:27 +01:00
/**
* Starts the volatile animation.
2019-07-18 12:26:39 +02:00
*
2019-03-21 09:57:27 +01:00
* @private
* @return {Volatile} A reference to the volatile for chaining.
*/
run() {
for (let object of this.objects) {
let x = object.x
let y = object.y
switch (this.opts.direction) {
case 'top':
y -= this.opts.distance
break
case 'right':
x += this.opts.distance
break
case 'bottom':
y += this.opts.distance
break
case 'left':
x -= this.opts.distance
break
}
TweenLite.to(object, this.opts.duration, {
x,
y,
alpha: 0,
ease: this.opts.ease,
overwrite: 'all',
onStart: () => {
if (this.opts.onStart) {
this.opts.onStart.call(object, object)
}
},
onComplete: () => {
if (this.opts.onComplete) {
this.opts.onComplete.call(object, object)
}
if (this.opts.destroyOnComplete) {
2019-07-18 12:26:39 +02:00
object.destroy({ children: true })
2019-03-21 09:57:27 +01:00
}
}
})
}
return this
}
}