• Jump To … +
    ./source/asset-management/image-asset.js ./source/asset-management/noise-asset.js ./source/asset-management/raw-asset.js ./source/asset-management/reaction-diffusion-asset.js ./source/asset-management/sprite-asset.js ./source/asset-management/video-asset.js ./source/core/animation-loop.js ./source/core/display-cycle.js ./source/core/document.js ./source/core/events.js ./source/core/init.js ./source/core/library.js ./source/core/snippets.js ./source/core/user-interaction.js ./source/factory/action.js ./source/factory/anchor.js ./source/factory/animation.js ./source/factory/bezier.js ./source/factory/block.js ./source/factory/button.js ./source/factory/canvas.js ./source/factory/cell.js ./source/factory/cog.js ./source/factory/color.js ./source/factory/conic-gradient.js ./source/factory/crescent.js ./source/factory/element.js ./source/factory/emitter.js ./source/factory/enhanced-label.js ./source/factory/filter.js ./source/factory/gradient.js ./source/factory/grid.js ./source/factory/group.js ./source/factory/label.js ./source/factory/line-spiral.js ./source/factory/line.js ./source/factory/loom.js ./source/factory/mesh.js ./source/factory/net.js ./source/factory/oval.js ./source/factory/particle-force.js ./source/factory/particle-spring.js ./source/factory/particle-world.js ./source/factory/particle.js ./source/factory/pattern.js ./source/factory/picture.js ./source/factory/polygon.js ./source/factory/polyline.js ./source/factory/quadratic.js ./source/factory/radial-gradient.js ./source/factory/rectangle.js ./source/factory/render-animation.js ./source/factory/shape.js ./source/factory/spiral.js ./source/factory/stack.js ./source/factory/star.js ./source/factory/tetragon.js ./source/factory/ticker.js ./source/factory/tracer.js ./source/factory/tween.js ./source/factory/unstacked-element.js ./source/factory/wheel.js ./source/helper/array-pool.js ./source/helper/color-engine.js ./source/helper/document-root-elements.js ./source/helper/filter-engine-bluenoise-data.js ./source/helper/filter-engine.js ./source/helper/gradient-engine.js ./source/helper/random-seed.js ./source/helper/shape-path-calculation.js ./source/helper/shared-vars.js ./source/helper/system-flags.js ./source/helper/utilities.js ./source/helper/workstore.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/button.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/hidden-dom-elements.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/styles.js ./source/mixin/text.js ./source/mixin/tween.js ./source/scrawl.js ./source/untracked-factory/cell-fragment.js ./source/untracked-factory/coordinate.js ./source/untracked-factory/drag-zone.js ./source/untracked-factory/keyboard-zone.js ./source/untracked-factory/observe-update.js ./source/untracked-factory/palette.js ./source/untracked-factory/particle-history.js ./source/untracked-factory/quaternion.js ./source/untracked-factory/state.js ./source/untracked-factory/text-style.js ./source/untracked-factory/vector.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 { mergeOver, Ωempty } from '../helper/utilities.js';
    
    import { cell } from '../core/library.js';
  • §

    Shared constants

    import { BLANK, DRAW, FILL, T_CELL, T_NOISE } from '../helper/shared-vars.js';
  • §

    Local constants

    const MAT_REPEAT = ['repeat', 'repeat-x', 'repeat-y', 'no-repeat'],
        REPEAT = 'repeat';
  • §

    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,
  • §

    Pattern matrix values

            patternStretchX: 1,
            patternStretchY: 1,
            patternSkewX: 0,
            patternSkewY: 0,
            patternShiftX: 0,
            patternShiftY: 0,
        };
        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;
        };
  • §

    Prototype functions

        P.initializePattern = function () {
    
            this.currentPatternMatrix = new DOMMatrix();
        };
  • §

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

        P.buildStyle = function (mycell, myentity, area) {
    
            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);
    
                    if (p) {
    
                        const {
                            patternStretchX,
                            patternStretchY,
                            patternSkewX,
                            patternSkewY,
                            patternShiftX,
                            patternShiftY,
                        } = this;
    
                        const matrix = this.currentPatternMatrix;
  • §

    Base pattern-owned transform

                        matrix.a = patternStretchX;
                        matrix.b = patternSkewY;
                        matrix.c = patternSkewX;
                        matrix.d = patternStretchY;
                        matrix.e = patternShiftX;
                        matrix.f = patternShiftY;
    
                        if (
                            (area === FILL && myentity.lockFillStyleToEntity) ||
                            (area === DRAW && myentity.lockStrokeStyleToEntity)
                        ) {
    
                            const scale = myentity.currentScale || 1;
  • §

    Entity scale is not part of the canvas transform.

    • Add it only when the pattern is entity-locked.
                            matrix.a *= scale;
                            matrix.b *= scale;
                            matrix.c *= scale;
                            matrix.d *= scale;
                            matrix.e *= scale;
                            matrix.f *= scale;
                        }
                        else {
  • §

    The engine is already translated/rotated for the entity.

    • Undo that transform so the pattern behaves as cell-locked.
                            const inverse = engine.getTransform().invertSelf();
    
                            matrix.preMultiplySelf(inverse);
                        }
    
                        p.setTransform(matrix);
    
                        return p;
                    }
                }
            }
            return BLANK;
        };
    }