- Jump To … +
  ./source/core/animation-loop.js ./source/core/display-cycle.js ./source/core/document-root-elements.js ./source/core/document.js ./source/core/events.js ./source/core/init.js ./source/core/library.js ./source/core/random-seed.js ./source/core/shared-vars.js ./source/core/snippets.js ./source/core/system-flags.js ./source/core/user-interaction.js ./source/core/utilities.js ./source/factory/action.js ./source/factory/anchor.js ./source/factory/animation.js ./source/factory/array-pool.js ./source/factory/bezier.js ./source/factory/block.js ./source/factory/canvas.js ./source/factory/cell-fragment.js ./source/factory/cell.js ./source/factory/cog.js ./source/factory/color.js ./source/factory/conic-gradient.js ./source/factory/coordinate.js ./source/factory/crescent.js ./source/factory/drag-zone.js ./source/factory/element.js ./source/factory/emitter.js ./source/factory/filter-engine-bluenoise-data.js ./source/factory/filter-engine.js ./source/factory/filter.js ./source/factory/font-attributes.js ./source/factory/gradient.js ./source/factory/grid.js ./source/factory/group.js ./source/factory/image-asset.js ./source/factory/keyboard-zone.js ./source/factory/line-spiral.js ./source/factory/line.js ./source/factory/loom.js ./source/factory/mesh.js ./source/factory/net.js ./source/factory/noise-asset.js ./source/factory/observe-update.js ./source/factory/oval.js ./source/factory/palette.js ./source/factory/particle-force.js ./source/factory/particle-history.js ./source/factory/particle-spring.js ./source/factory/particle-world.js ./source/factory/particle.js ./source/factory/pattern.js ./source/factory/phrase.js ./source/factory/picture.js ./source/factory/polygon.js ./source/factory/polyline.js ./source/factory/quadratic.js ./source/factory/quaternion.js ./source/factory/radial-gradient.js ./source/factory/raw-asset.js ./source/factory/reaction-diffusion-asset.js ./source/factory/rectangle.js ./source/factory/render-animation.js ./source/factory/shape.js ./source/factory/spiral.js ./source/factory/sprite-asset.js ./source/factory/stack.js ./source/factory/star.js ./source/factory/state.js ./source/factory/tetragon.js ./source/factory/ticker.js ./source/factory/tracer.js ./source/factory/tween.js ./source/factory/unstacked-element.js ./source/factory/vector.js ./source/factory/video-asset.js ./source/factory/wheel.js ./source/mixin/anchor.js ./source/mixin/asset-advanced-functionality.js ./source/mixin/asset-consumer.js ./source/mixin/asset.js ./source/mixin/base.js ./source/mixin/cascade.js ./source/mixin/cell-key-functions.js ./source/mixin/delta.js ./source/mixin/display-shape.js ./source/mixin/dom.js ./source/mixin/entity.js ./source/mixin/filter.js ./source/mixin/mimic.js ./source/mixin/path.js ./source/mixin/pattern.js ./source/mixin/pivot.js ./source/mixin/position.js ./source/mixin/shape-basic.js ./source/mixin/shape-curve.js ./source/mixin/shape-path-calculation.js ./source/mixin/styles.js ./source/mixin/tween.js

- §

  # Asset consumer mixin

  This mixin needs to be applied to any factory which wishes to use an asset. Asset objects are wrappers for managing <img>, <video> and (offscreen) <canvas> elements.

  Currently only Picture entity and Pattern style factories use assets. This mixin defines attributes and functionality common to both.

- §

  #### Imports

      import { mergeOver, xt, Ωempty } from '../core/utilities.js';

      import { asset } from '../core/library.js';

      import { _now } from '../core/shared-vars.js';

      import { importImage } from '../factory/image-asset.js';
      import { importVideo } from '../factory/video-asset.js';
      import { importSprite } from '../factory/sprite-asset.js';

      import { ADD_TEXT_TRACK, CAN_PLAY_TYPE, CAPTURE_STREAM, DEFAULT, FAST_SEEK, LOAD, PAUSE, PLAY, SET_MEDIA_KEYS, SET_SINK_ID, T_SPRITE, T_VIDEO } from '../core/shared-vars.js';

- §

  #### Export function

      export default function (P = Ωempty) {

- §

  #### Shared attributes

          const defaultAttributes = {

- §

  asset - eventually becomes the current asset wrapper object (as generated by the `imageAsset`, `spriteAsset` and `videoAsset` factories).

              asset: null,

- §

  removeAssetOnKill - A flag that determines whether, when the entity object is killed (has its `kill` function invoked), the kill functionality should cascade to the asset object.

  - `false` (default) - do not cascade the kill action
  - any value that is not false - remove the asset wrapper object
  - value is a truthy String - for example, `'dom'` - also remove the underlying asset element from the DOM

              removeAssetOnKill: false,

- §

  ##### Spritesheet-specific attributes

              spriteIsRunning: false,
              spriteLastFrameChange: 0,
              spriteCurrentFrame: 0,
              spriteTrack: DEFAULT,
              spriteForward: true,
              spriteFrameDuration: 100,
              spriteWillLoop: true,
          };
          P.defs = mergeOver(P.defs, defaultAttributes);

- §

  #### Packet management

  No additional packet functionality defined here

- §

  #### Clone management

  No additional clone functionality defined here

- §

  #### Kill management

  No additional kill functionality defined here

- §

  #### Get, Set, deltaSet

          const G = P.getters,
              S = P.setters;

          G.sourceDimensions = function () {

              return [this.sourceNaturalWidth, this.sourceNaturalHeight];
          };

- §

  asset - Setting the Asset object. Updating the asset will set the dirtyAsset flag.

          S.asset = function (item) {

              const oldAsset = this.asset,
                  newAsset = (item && item.name) ? item.name : item;

              if (oldAsset && !oldAsset.substring) oldAsset.unsubscribe(this);

              this.asset = newAsset;
              this.dirtyAsset = true;
          };

- §

  ##### Setting the source

  Argument needs to be a path/file String that will be used to import the new Image, Video or Sprite file and construct an appropriate Asset object wrapper for it.

- §

  imageSource

          S.imageSource = function (item) {

              const results = importImage(item);

              if (results) {

                  const myAsset = asset[results[0]];

                  if (myAsset) {

                      const oldAsset = this.asset;

                      if (oldAsset && oldAsset.unsubscribe) oldAsset.unsubscribe(this);

                      myAsset.subscribe(this);
                  }
              }
          };

- §

  videoSource

          S.videoSource = function (item) {

              const result = importVideo(item);

              if (result) {

                  const myAsset = asset[result];

                  if (myAsset) {

                      const oldAsset = this.asset;

                      if (oldAsset && oldAsset.unsubscribe) oldAsset.unsubscribe(this);

                      myAsset.subscribe(this);
                  }
              }
          };

- §

  spriteSource

          S.spriteSource = function (item) {

              const result = importSprite(item);

              if (result) {

                  const myAsset = asset[result];

                  if (myAsset) {

                      const oldAsset = this.asset;

                      if (oldAsset && oldAsset.unsubscribe) oldAsset.unsubscribe(this);

                      myAsset.subscribe(this);
                  }
              }
          };

- §

  #### Prototype functions

- §

  `cleanAsset` - Cleaning the Asset object

          P.cleanAsset = function () {

              const ast = this.asset;

              if (ast && ast.substring) {

                  const myAsset = asset[ast];

                  if (myAsset) {

                      this.dirtyAsset = false;
                      myAsset.subscribe(this);
                  }
              }
          };

- §

  ##### Video actions

  These functions largely map to a selection of the asset’s source <video> element’s functions. They allow the video to be controlled/coded by invoking the appropriate function on the Picture entity or Pattern style instance, rather than having to seek out the asset wrapper object to invoke the functions on them.

- §

  `videoAction` - internal helper function

          P.videoAction = function (action, ...args) {

              const myAsset = this.asset;

              if (myAsset && myAsset.type == T_VIDEO) return myAsset[action](...args);
          };

- §

  `videoPromiseAction` - internal helper function

          P.videoPromiseAction = function (action, ...args) {

              const myAsset = this.asset;

              if (myAsset && myAsset.type == T_VIDEO) return myAsset[action](...args);
              else return Promise.reject('Asset not a video');
          };

- §

  `videoAddTextTrack`

          P.videoAddTextTrack = function (kind, label, language) {
              return this.videoAction(ADD_TEXT_TRACK, kind, label, language);
          };

- §

  `videoCaptureStream`

          P.videoCaptureStream = function () {
              return this.videoAction(CAPTURE_STREAM);
          };

- §

  `videoCanPlayType`

          P.videoCanPlayType = function (mytype) {
              return this.videoAction(CAN_PLAY_TYPE, mytype);
          };

- §

  `videoFastSeek`

          P.videoFastSeek = function (time) {
              return this.videoAction(FAST_SEEK, time);
          };

- §

  `videoLoad`

          P.videoLoad = function () {
              return this.videoAction(LOAD);
          };

- §

  `videoPause`

          P.videoPause = function () {
              return this.videoAction(PAUSE);
          };

- §

  `videoPlay`

          P.videoPlay = function () {
              return this.videoPromiseAction(PLAY);
          };

- §

  `videoSetMediaKeys`

          P.videoSetMediaKeys = function (keys) {
              return this.videoPromiseAction(SET_MEDIA_KEYS, keys);
          };

- §

  `videoSetSinkId`

          P.videoSetSinkId = function () {
              return this.videoPromiseAction(SET_SINK_ID);
          };

- §

  ##### Sprite actions

  Add functions to the Picture entity or Pattern style factories which can be used to control image sprite animation playback.

- §

  `checkSpriteFrame`

          P.checkSpriteFrame = function () {

              const asset = this.asset;

              if (asset && asset.type == T_SPRITE && asset.manifest) {

                  const copyArray = this.copyArray;

                  if (this.spriteIsRunning) {

                      const last = this.spriteLastFrameChange,
                          choke = this.spriteFrameDuration,
                          now = _now();

                      if (now > last + choke) {

                          const manifest = asset.manifest;

                          if (manifest) {

                              const track = manifest[this.spriteTrack],
                                  len = track.length,
                                  loop = this.spriteWillLoop;

                              let frame = this.spriteCurrentFrame;

                              frame = (this.spriteForward) ? frame + 1 : frame - 1;

                              if (frame < 0) frame = (loop) ? len - 1 : 0;
                              if (frame >= len) frame = (loop) ? 0 : len - 1;

                              const [source, x, y, w, h] = track[frame];

                              copyArray.length = 0;
                              copyArray.push(x, y, w, h);

                              this.dirtyCopyStart = false;
                              this.dirtyCopyDimensions = false;

                              const sourceName = this.source.id || this.source.name;

                              if (source != sourceName) {

                                  const newSource = asset.sourceHold[source];

                                  if (newSource) this.source = newSource;
                              }

                              this.spriteCurrentFrame = frame;
                              this.spriteLastFrameChange = now;
                          }
                      }
                  }
                  else {

                      const [, x, y, w, h] = asset.manifest[this.spriteTrack][this.spriteCurrentFrame],
                          [cx, cy, cw, ch] = copyArray;

                      if (cx != x || cy != y || cw != w || ch != h) {

                          copyArray.length = 0;
                          copyArray.push(x, y, w, h);

                          this.dirtyCopyStart = false;
                          this.dirtyCopyDimensions = false;
                      }
                  }
              }
          };

- §

  `playSprite`

          P.playSprite = function (speed, loop, track, forward, frame) {

              if (xt(speed)) this.spriteFrameDuration = speed;
              if (xt(loop)) this.spriteWillLoop = loop;
              if (xt(track)) this.spriteTrack = track;
              if (xt(forward)) this.spriteForward = forward;
              if (xt(frame)) this.spriteCurrentFrame = frame;

              this.spriteLastFrameChange = _now();
              this.spriteIsRunning = true;
          }

- §

  `haltSprite`

          P.haltSprite = function (speed, loop, track, forward, frame) {

              if (xt(speed)) this.spriteFrameDuration = speed;
              if (xt(loop)) this.spriteWillLoop = loop;
              if (xt(track)) this.spriteTrack = track;
              if (xt(forward)) this.spriteForward = forward;
              if (xt(frame)) this.spriteCurrentFrame = frame;

              this.spriteIsRunning = false;
          }
      }