addLayer()

in src/core/animpack/AnimationFeature.js [436:485]


  addLayer(name = 'NewLayer', options = {}, index) {
    const numLayers = this._layers.length;
    let layerIndex = index;

    // Make sure the given index is within the range of layers
    if (index === undefined || index === -1) {
      layerIndex = this._layers.length;
    } else {
      layerIndex = this._validateIndex(index, false);

      if (layerIndex === undefined) {
        // Insert at the beginning if the user passed in a negative index
        if (index < 0) {
          layerIndex = 0;
        }
        // Otherwise append to the end
        else {
          layerIndex = this._layers.length;
        }

        console.warn(
          `Index ${index} is invalid for host ${this._host.id}. New layer will be added at the closest valid index: ${layerIndex}.`
        );
      }
    }

    // Make sure the layer name is unique
    const layerName = Utils.getUniqueName(name, Object.keys(this._layerMap));

    if (name !== layerName) {
      console.warn(
        `Layer name ${name} is not unique. New layer will be added with the name ${layerName}.`
      );
    }

    const layer = new AnimationLayer({...options, name: layerName});
    this._layerMap[layerName] = layer;

    if (layerIndex === numLayers) {
      this._layers.push(layer);
    } else {
      this._layers.splice(layerIndex, 0, layer);
    }

    // Notify that a layer has been added to the feature
    const eventData = {name: layerName, index: layerIndex};
    this.emit(this.constructor.EVENTS.addLayer, eventData);

    return eventData;
  }