/*! * scrollmagic v2.0.7 (2019-05-07) * the javascript library for magical scroll interactions. * (c) 2019 jan paepke (@janpaepke) * project website: http://scrollmagic.io * * @version 2.0.7 * @license dual licensed under mit license and gpl. * @author jan paepke - e-mail@janpaepke.de * * @file debug extension for scrollmagic. */ /** * this plugin was formerly known as the scrollmagic debug extension. * * it enables you to add visual indicators to your page, to be able to see exactly when a scene is triggered. * * to have access to this extension, please include `plugins/debug.addindicators.js`. * @mixin debug.addindicators */ (function (root, factory) { if (typeof define === 'function' && define.amd) { // amd. register as an anonymous module. define(['scrollmagic'], factory); } else if (typeof exports === 'object') { // commonjs factory(require('scrollmagic')); } else { // no browser global export needed, just execute factory(root.scrollmagic || (root.jquery && root.jquery.scrollmagic)); } }(this, function (scrollmagic) { "use strict"; var namespace = "debug.addindicators"; var console = window.console || {}, err = function.prototype.bind.call(console.error || console.log || function () {}, console); if (!scrollmagic) { err("(" + namespace + ") -> error: the scrollmagic main module could not be found. please make sure it's loaded before this plugin or use an asynchronous loader like requirejs."); } // plugin settings var font_size = "0.85em", zindex = "9999", edge_offset = 15; // minimum edge distance, added to indentation // overall vars var _util = scrollmagic._util, _autoindex = 0; scrollmagic.scene.extend(function () { var scene = this, _indicator; var log = function () { if (scene._log) { // not available, when main source minified array.prototype.splice.call(arguments, 1, 0, "(" + namespace + ")", "->"); scene._log.apply(this, arguments); } }; /** * add visual indicators for a scrollmagic.scene. * @memberof! debug.addindicators# * * @example * // add basic indicators * scene.addindicators() * * // passing options * scene.addindicators({name: "pin scene", colorend: "#ffffff"}); * * @param {object} [options] - an object containing one or more options for the indicators. * @param {(string|object)} [options.parent] - a selector, dom object or a jquery object that the indicators should be added to. if undefined, the controller's container will be used. * @param {number} [options.name=""] - this string will be displayed at the start and end indicators of the scene for identification purposes. if no name is supplied an automatic index will be used. * @param {number} [options.indent=0] - additional position offset for the indicators (useful, when having multiple scenes starting at the same position). * @param {string} [options.colorstart=green] - css color definition for the start indicator. * @param {string} [options.colorend=red] - css color definition for the end indicator. * @param {string} [options.colortrigger=blue] - css color definition for the trigger indicator. */ scene.addindicators = function (options) { if (!_indicator) { var default_options = { name: "", indent: 0, parent: undefined, colorstart: "green", colorend: "red", colortrigger: "blue", }; options = _util.extend({}, default_options, options); _autoindex++; _indicator = new indicator(scene, options); scene.on("add.plugin_addindicators", _indicator.add); scene.on("remove.plugin_addindicators", _indicator.remove); scene.on("destroy.plugin_addindicators", scene.removeindicators); // it the scene already has a controller we can start right away. if (scene.controller()) { _indicator.add(); } } return scene; }; /** * removes visual indicators from a scrollmagic.scene. * @memberof! debug.addindicators# * * @example * // remove previously added indicators * scene.removeindicators() * */ scene.removeindicators = function () { if (_indicator) { _indicator.remove(); this.off("*.plugin_addindicators"); _indicator = undefined; } return scene; }; }); /* * ---------------------------------------------------------------- * extension for controller to store and update related indicators * ---------------------------------------------------------------- */ // add option to globally auto-add indicators to scenes /** * every scrollmagic.controller instance now accepts an additional option. * see {@link scrollmagic.controller} for a complete list of the standard options. * @memberof! debug.addindicators# * @method new scrollmagic.controller(options) * @example * // make a controller and add indicators to all scenes attached * var controller = new scrollmagic.controller({addindicators: true}); * // this scene will automatically have indicators added to it * new scrollmagic.scene() * .addto(controller); * * @param {object} [options] - options for the controller. * @param {boolean} [options.addindicators=false] - if set to `true` every scene that is added to the controller will automatically get indicators added to it. */ scrollmagic.controller.addoption("addindicators", false); // extend controller scrollmagic.controller.extend(function () { var controller = this, _info = controller.info(), _container = _info.container, _isdocument = _info.isdocument, _vertical = _info.vertical, _indicators = { // container for all indicators and methods groups: [] }; var log = function () { if (controller._log) { // not available, when main source minified array.prototype.splice.call(arguments, 1, 0, "(" + namespace + ")", "->"); controller._log.apply(this, arguments); } }; if (controller._indicators) { log(2, "warning: scene already has a property '_indicators', which will be overwritten by plugin."); } // add indicators container this._indicators = _indicators; /* needed updates: +++++++++++++++ start/end position on scene shift (handled in indicator class) trigger parameters on triggerhook value change (handled in indicator class) bounds position on container scroll or resize (to keep alignment to bottom/right) trigger position on container resize, window resize (if container isn't document) and window scroll (if container isn't document) */ // event handler for when associated bounds markers need to be repositioned var handleboundspositionchange = function () { _indicators.updateboundspositions(); }; // event handler for when associated trigger groups need to be repositioned var handletriggerpositionchange = function () { _indicators.updatetriggergrouppositions(); }; _container.addeventlistener("resize", handletriggerpositionchange); if (!_isdocument) { window.addeventlistener("resize", handletriggerpositionchange); window.addeventlistener("scroll", handletriggerpositionchange); } // update all related bounds containers _container.addeventlistener("resize", handleboundspositionchange); _container.addeventlistener("scroll", handleboundspositionchange); // updates the position of the bounds container to aligned to the right for vertical containers and to the bottom for horizontal this._indicators.updateboundspositions = function (specificindicator) { var // constant for all bounds groups = specificindicator ? [_util.extend({}, specificindicator.triggergroup, { members: [specificindicator] })] : // create a group with only one element _indicators.groups, // use all g = groups.length, css = {}, parampos = _vertical ? "left" : "top", paramdimension = _vertical ? "width" : "height", edge = _vertical ? _util.get.scrollleft(_container) + _util.get.width(_container) - edge_offset : _util.get.scrolltop(_container) + _util.get.height(_container) - edge_offset, b, triggersize, group; while (g--) { // group loop group = groups[g]; b = group.members.length; triggersize = _util.get[paramdimension](group.element.firstchild); while (b--) { // indicators loop css[parampos] = edge - triggersize; _util.css(group.members[b].bounds, css); } } }; // updates the positions of all trigger groups attached to a controller or a specific one, if provided this._indicators.updatetriggergrouppositions = function (specificgroup) { var // constant vars groups = specificgroup ? [specificgroup] : _indicators.groups, i = groups.length, container = _isdocument ? document.body : _container, containeroffset = _isdocument ? { top: 0, left: 0 } : _util.get.offset(container, true), edge = _vertical ? _util.get.width(_container) - edge_offset : _util.get.height(_container) - edge_offset, paramdimension = _vertical ? "width" : "height", paramtransform = _vertical ? "y" : "x"; var // changing vars group, elem, pos, elemsize, transform; while (i--) { group = groups[i]; elem = group.element; pos = group.triggerhook * controller.info("size"); elemsize = _util.get[paramdimension](elem.firstchild.firstchild); transform = pos > elemsize ? "translate" + paramtransform + "(-100%)" : ""; _util.css(elem, { top: containeroffset.top + (_vertical ? pos : edge - group.members[0].options.indent), left: containeroffset.left + (_vertical ? edge - group.members[0].options.indent : pos) }); _util.css(elem.firstchild.firstchild, { "-ms-transform": transform, "-webkit-transform": transform, "transform": transform }); } }; // updates the label for the group to contain the name, if it only has one member this._indicators.updatetriggergrouplabel = function (group) { var text = "trigger" + (group.members.length > 1 ? "" : " " + group.members[0].options.name), elem = group.element.firstchild.firstchild, doupdate = elem.textcontent !== text; if (doupdate) { elem.textcontent = text; if (_vertical) { // bounds position is dependent on text length, so update _indicators.updateboundspositions(); } } }; // add indicators if global option is set this.addscene = function (newscene) { if (this._options.addindicators && newscene instanceof scrollmagic.scene && newscene.controller() === controller) { newscene.addindicators(); } // call original destroy method this.$super.addscene.apply(this, arguments); }; // remove all previously set listeners on destroy this.destroy = function () { _container.removeeventlistener("resize", handletriggerpositionchange); if (!_isdocument) { window.removeeventlistener("resize", handletriggerpositionchange); window.removeeventlistener("scroll", handletriggerpositionchange); } _container.removeeventlistener("resize", handleboundspositionchange); _container.removeeventlistener("scroll", handleboundspositionchange); // call original destroy method this.$super.destroy.apply(this, arguments); }; return controller; }); /* * ---------------------------------------------------------------- * internal class for the construction of indicators * ---------------------------------------------------------------- */ var indicator = function (scene, options) { var indicator = this, _elembounds = tpl.bounds(), _elemstart = tpl.start(options.colorstart), _elemend = tpl.end(options.colorend), _boundscontainer = options.parent && _util.get.elements(options.parent)[0], _vertical, _ctrl; var log = function () { if (scene._log) { // not available, when main source minified array.prototype.splice.call(arguments, 1, 0, "(" + namespace + ")", "->"); scene._log.apply(this, arguments); } }; options.name = options.name || _autoindex; // prepare bounds elements _elemstart.firstchild.textcontent += " " + options.name; _elemend.textcontent += " " + options.name; _elembounds.appendchild(_elemstart); _elembounds.appendchild(_elemend); // set public variables indicator.options = options; indicator.bounds = _elembounds; // will be set later indicator.triggergroup = undefined; // add indicators to dom this.add = function () { _ctrl = scene.controller(); _vertical = _ctrl.info("vertical"); var isdocument = _ctrl.info("isdocument"); if (!_boundscontainer) { // no parent supplied or doesnt exist _boundscontainer = isdocument ? document.body : _ctrl.info("container"); // check if window/document (then use body) } if (!isdocument && _util.css(_boundscontainer, "position") === 'static') { // position mode needed for correct positioning of indicators _util.css(_boundscontainer, { position: "relative" }); } // add listeners for updates scene.on("change.plugin_addindicators", handletriggerparamschange); scene.on("shift.plugin_addindicators", handleboundsparamschange); // updates trigger & bounds (will add elements if needed) updatetriggergroup(); updatebounds(); settimeout(function () { // do after all execution is finished otherwise sometimes size calculations are off _ctrl._indicators.updateboundspositions(indicator); }, 0); log(3, "added indicators"); }; // remove indicators from dom this.remove = function () { if (indicator.triggergroup) { // if not set there's nothing to remove scene.off("change.plugin_addindicators", handletriggerparamschange); scene.off("shift.plugin_addindicators", handleboundsparamschange); if (indicator.triggergroup.members.length > 1) { // just remove from memberlist of old group var group = indicator.triggergroup; group.members.splice(group.members.indexof(indicator), 1); _ctrl._indicators.updatetriggergrouplabel(group); _ctrl._indicators.updatetriggergrouppositions(group); indicator.triggergroup = undefined; } else { // remove complete group removetriggergroup(); } removebounds(); log(3, "removed indicators"); } }; /* * ---------------------------------------------------------------- * internal event handlers * ---------------------------------------------------------------- */ // event handler for when bounds params change var handleboundsparamschange = function () { updatebounds(); }; // event handler for when trigger params change var handletriggerparamschange = function (e) { if (e.what === "triggerhook") { updatetriggergroup(); } }; /* * ---------------------------------------------------------------- * bounds (start / stop) management * ---------------------------------------------------------------- */ // adds an new bounds elements to the array and to the dom var addbounds = function () { var v = _ctrl.info("vertical"); // apply stuff we didn't know before... _util.css(_elemstart.firstchild, { "border-bottom-width": v ? 1 : 0, "border-right-width": v ? 0 : 1, "bottom": v ? -1 : options.indent, "right": v ? options.indent : -1, "padding": v ? "0 8px" : "2px 4px", }); _util.css(_elemend, { "border-top-width": v ? 1 : 0, "border-left-width": v ? 0 : 1, "top": v ? "100%" : "", "right": v ? options.indent : "", "bottom": v ? "" : options.indent, "left": v ? "" : "100%", "padding": v ? "0 8px" : "2px 4px" }); // append _boundscontainer.appendchild(_elembounds); }; // remove bounds from list and dom var removebounds = function () { _elembounds.parentnode.removechild(_elembounds); }; // update the start and end positions of the scene var updatebounds = function () { if (_elembounds.parentnode !== _boundscontainer) { addbounds(); // add bounds elements (start/end) } var css = {}; css[_vertical ? "top" : "left"] = scene.triggerposition(); css[_vertical ? "height" : "width"] = scene.duration(); _util.css(_elembounds, css); _util.css(_elemend, { display: scene.duration() > 0 ? "" : "none" }); }; /* * ---------------------------------------------------------------- * trigger and trigger group management * ---------------------------------------------------------------- */ // adds an new trigger group to the array and to the dom var addtriggergroup = function () { var triggerelem = tpl.trigger(options.colortrigger); // new trigger element var css = {}; css[_vertical ? "right" : "bottom"] = 0; css[_vertical ? "border-top-width" : "border-left-width"] = 1; _util.css(triggerelem.firstchild, css); _util.css(triggerelem.firstchild.firstchild, { padding: _vertical ? "0 8px 3px 8px" : "3px 4px" }); document.body.appendchild(triggerelem); // directly add to body var newgroup = { triggerhook: scene.triggerhook(), element: triggerelem, members: [indicator] }; _ctrl._indicators.groups.push(newgroup); indicator.triggergroup = newgroup; // update right away _ctrl._indicators.updatetriggergrouplabel(newgroup); _ctrl._indicators.updatetriggergrouppositions(newgroup); }; var removetriggergroup = function () { _ctrl._indicators.groups.splice(_ctrl._indicators.groups.indexof(indicator.triggergroup), 1); indicator.triggergroup.element.parentnode.removechild(indicator.triggergroup.element); indicator.triggergroup = undefined; }; // updates the trigger group -> either join existing or add new one /* * logic: * 1 if a trigger group exist, check if it's in sync with scene settings – if so, nothing else needs to happen * 2 try to find an existing one that matches scene parameters * 2.1 if a match is found check if already assigned to an existing group * if so: * a: it was the last member of existing group -> kill whole group * b: the existing group has other members -> just remove from member list * 2.2 assign to matching group * 3 if no new match could be found, check if assigned to existing group * a: yes, and it's the only member -> just update parameters and positions and keep using this group * b: yes but there are other members -> remove from member list and create a new one * c: no, so create a new one */ var updatetriggergroup = function () { var triggerhook = scene.triggerhook(), closeenough = 0.0001; // have a group, check if it still matches if (indicator.triggergroup) { if (math.abs(indicator.triggergroup.triggerhook - triggerhook) < closeenough) { // _util.log(0, "trigger", options.name, "->", "no need to change, still in sync"); return; // all good } } // don't have a group, check if a matching one exists // _util.log(0, "trigger", options.name, "->", "out of sync!"); var groups = _ctrl._indicators.groups, group, i = groups.length; while (i--) { group = groups[i]; if (math.abs(group.triggerhook - triggerhook) < closeenough) { // found a match! // _util.log(0, "trigger", options.name, "->", "found match"); if (indicator.triggergroup) { // do i have an old group that is out of sync? if (indicator.triggergroup.members.length === 1) { // is it the only remaining group? // _util.log(0, "trigger", options.name, "->", "kill"); // was the last member, remove the whole group removetriggergroup(); } else { indicator.triggergroup.members.splice(indicator.triggergroup.members.indexof(indicator), 1); // just remove from memberlist of old group _ctrl._indicators.updatetriggergrouplabel(indicator.triggergroup); _ctrl._indicators.updatetriggergrouppositions(indicator.triggergroup); // _util.log(0, "trigger", options.name, "->", "removing from previous member list"); } } // join new group group.members.push(indicator); indicator.triggergroup = group; _ctrl._indicators.updatetriggergrouplabel(group); return; } } // at this point i am obviously out of sync and don't match any other group if (indicator.triggergroup) { if (indicator.triggergroup.members.length === 1) { // _util.log(0, "trigger", options.name, "->", "updating existing"); // out of sync but i'm the only member => just change and update indicator.triggergroup.triggerhook = triggerhook; _ctrl._indicators.updatetriggergrouppositions(indicator.triggergroup); return; } else { // _util.log(0, "trigger", options.name, "->", "removing from previous member list"); indicator.triggergroup.members.splice(indicator.triggergroup.members.indexof(indicator), 1); // just remove from memberlist of old group _ctrl._indicators.updatetriggergrouplabel(indicator.triggergroup); _ctrl._indicators.updatetriggergrouppositions(indicator.triggergroup); indicator.triggergroup = undefined; // need a brand new group... } } // _util.log(0, "trigger", options.name, "->", "add a new one"); // did not find any match, make new trigger group addtriggergroup(); }; }; /* * ---------------------------------------------------------------- * templates for the indicators * ---------------------------------------------------------------- */ var tpl = { start: function (color) { // inner element (for bottom offset -1, while keeping top position 0) var inner = document.createelement("div"); inner.textcontent = "start"; _util.css(inner, { position: "absolute", overflow: "visible", "border-width": 0, "border-style": "solid", color: color, "border-color": color }); var e = document.createelement('div'); // wrapper _util.css(e, { position: "absolute", overflow: "visible", width: 0, height: 0 }); e.appendchild(inner); return e; }, end: function (color) { var e = document.createelement('div'); e.textcontent = "end"; _util.css(e, { position: "absolute", overflow: "visible", "border-width": 0, "border-style": "solid", color: color, "border-color": color }); return e; }, bounds: function () { var e = document.createelement('div'); _util.css(e, { position: "absolute", overflow: "visible", "white-space": "nowrap", "pointer-events": "none", "font-size": font_size }); e.style.zindex = zindex; return e; }, trigger: function (color) { // inner to be above or below line but keep position var inner = document.createelement('div'); inner.textcontent = "trigger"; _util.css(inner, { position: "relative", }); // inner wrapper for right: 0 and main element has no size var w = document.createelement('div'); _util.css(w, { position: "absolute", overflow: "visible", "border-width": 0, "border-style": "solid", color: color, "border-color": color }); w.appendchild(inner); // wrapper var e = document.createelement('div'); _util.css(e, { position: "fixed", overflow: "visible", "white-space": "nowrap", "pointer-events": "none", "font-size": font_size }); e.style.zindex = zindex; e.appendchild(w); return e; }, }; }));