- 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

- §

  # Pattern mixin

  Most of the code relating to the CanvasPattern API can be found here.

  - To create a pattern from an image asset, use the Pattern factory.
  - We can also use a Scrawl-canvas Cell as the asset for a pattern.
  - In both cases, we assign the pattern to an entity’s `fillStyle` or `strokeStyle` attribute by supplying the Pattern object or Cell wrapper’s String name to it.
- §

  #### Imports

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

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

      import { _isArray, _A, _B, _C, _D, _E, _F, BLANK, MAT_POS, MAT_REPEAT, REPEAT, T_CELL, T_NOISE } from '../core/shared-vars.js';

- §

  #### Export function

      export default function (P = Ωempty) {

- §

  #### Shared attributes

          const defaultAttributes = {

- §

  repeat - String indicating how to repeat the pattern’s image. Possible values are: `repeat` (default), `repeat-x`, `repeat-y`, `no-repeat`

              repeat: REPEAT,

- §

  patternMatrix - Scrawl-canvas will apply a 2d-style, 6 value DOMMatrix to the pattern each time it is recreated. Changing the values of the matrix will change the rotation, skew, etc of the pattern. Pseudo-attributes can be used to set individual elements of the matrix, as follows:

  - `stretchX` (or `matrixA`) - generally used for horizontal (x axis) scale
  - `skewY` (or `matrixB`) - generally used for horizontal (x axis) skew
  - `skewX` (or `matrixC`) - generally used for vertical (y axis) skew
  - `stretchY` (or `matrixD`) - generally used for vertical (y axis) scale
  - `shiftX` (or `matrixE`) - generally used for horizontal (x axis) positioning
  - `shiftY` (or `matrixF`) - generally used for vertical (y axis) positioning

  To rotate the pattern, update the B and C matrix values in tandem. Results will be dependent on the surrounding matrix values. See demo Canvas-035 to explore further.

              patternMatrix: null,
          };
          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 S = P.setters;

- §

  repeat

          S.repeat = function (item) {

              if (MAT_REPEAT.includes(item)) this.repeat = item;
              else this.repeat = this.defs.repeat;
          };

- §

  `updateMatrixNumber` - internal helper function

          P.updateMatrixNumber = function (item, pos) {

              if (!this.patternMatrix) this.patternMatrix = new DOMMatrix();

              item = (item.substring) ? parseFloat(item) : item;

              const posCheck = MAT_POS.includes(pos);

              if (isa_number(item) && posCheck) this.patternMatrix[pos] = item;
          };

- §

  matrixA, matrixB, matrixC, matrixD, matrixE, matrixF - these pseudo-attributes can be used to set individual attributes of the `patternMatrix` DOMMatrix object

          S.matrixA = function (item) { this.updateMatrixNumber(item, _A); };
          S.matrixB = function (item) { this.updateMatrixNumber(item, _B); };
          S.matrixC = function (item) { this.updateMatrixNumber(item, _C); };
          S.matrixD = function (item) { this.updateMatrixNumber(item, _D); };
          S.matrixE = function (item) { this.updateMatrixNumber(item, _E); };
          S.matrixF = function (item) { this.updateMatrixNumber(item, _F); };

- §

  stretchX, skewY, skewX, stretchY, shiftX, shiftY - these pseudo-attributes can be used to set individual attributes of the `patternMatrix` DOMMatrix object

          S.stretchX = function (item) { this.updateMatrixNumber(item, _A); };
          S.skewY = function (item) { this.updateMatrixNumber(item, _B); };
          S.skewX = function (item) { this.updateMatrixNumber(item, _C); };
          S.stretchY = function (item) { this.updateMatrixNumber(item, _D); };
          S.shiftX = function (item) { this.updateMatrixNumber(item, _E); };
          S.shiftY = function (item) { this.updateMatrixNumber(item, _F); };

- §

  patternMatrix - the argument must be an Array containing 6 Number elements in the form of `[a, b, c, d, e, f]`

          S.patternMatrix = function (item) {

              if (_isArray(item)) {

                  const update = this.updateMatrixNumber;

                  update(item[0], _A);
                  update(item[1], _B);
                  update(item[2], _C);
                  update(item[3], _D);
                  update(item[4], _E);
                  update(item[5], _F);
              }
          };

- §

  #### Prototype functions

- §

  `buildStyle` - internal function: creates the pattern on the Cell’s CanvasRenderingContext2D engine.

          P.buildStyle = function (mycell) {

              if (mycell) {

                  if (mycell.substring) mycell = cell[mycell];

                  let source = this.source,
                      loaded = this.sourceLoaded;

                  const repeat = this.repeat,
                      engine = mycell.engine;

                  if (this.type == T_CELL || this.type == T_NOISE) {

                      source = this.element;
                      loaded = true;
                  }
                  if (engine && loaded) {

                      const p = engine.createPattern(source, repeat);

                      p.setTransform(this.patternMatrix);

                      return p;
                  }
              }
              return BLANK;
          };
      }