• Jump To … +
    ./source/core/animationloop.js ./source/core/component.js ./source/core/document.js ./source/core/events.js ./source/core/init.js ./source/core/library.js ./source/core/userInteraction.js ./source/core/utilities.js ./source/factory/action.js ./source/factory/anchor.js ./source/factory/animation.js ./source/factory/bezier.js ./source/factory/block.js ./source/factory/canvas.js ./source/factory/cell.js ./source/factory/cog.js ./source/factory/color.js ./source/factory/coordinate.js ./source/factory/element.js ./source/factory/emitter.js ./source/factory/filter.js ./source/factory/fontAttributes.js ./source/factory/gradient.js ./source/factory/grid.js ./source/factory/group.js ./source/factory/imageAsset.js ./source/factory/line.js ./source/factory/loom.js ./source/factory/mesh.js ./source/factory/net.js ./source/factory/noise.js ./source/factory/oval.js ./source/factory/palette.js ./source/factory/particle.js ./source/factory/particleForce.js ./source/factory/particleHistory.js ./source/factory/particleSpring.js ./source/factory/particleWorld.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/radialGradient.js ./source/factory/rectangle.js ./source/factory/renderAnimation.js ./source/factory/shape.js ./source/factory/spiral.js ./source/factory/spriteAsset.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/unstackedElement.js ./source/factory/vector.js ./source/factory/videoAsset.js ./source/factory/wheel.js ./source/mixin/anchor.js ./source/mixin/asset.js ./source/mixin/assetConsumer.js ./source/mixin/base.js ./source/mixin/cascade.js ./source/mixin/delta.js ./source/mixin/displayShape.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/shapeBasic.js ./source/mixin/shapeCurve.js ./source/mixin/shapePathCalculation.js ./source/mixin/styles.js ./source/mixin/tween.js ./source/worker/filter-string.js ./source/worker/filter.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, isa_number } from '../core/utilities.js';
    import { cell } from '../core/library.js';
  • ¶

    Export function

    export default function (P = {}) {
  • ¶

    Shared attributes

        let 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:

    • matrixA - generally used for horizontal (x axis) scale
    • matrixB - generally used for horizontal (x axis) skew
    • matrixC - generally used for vertical (y axis) skew
    • matrixD - generally used for vertical (y axis) scale
    • matrixE - generally used for horizontal (x axis) positioning
    • 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

        let S = P.setters;
  • ¶

    repeat

        P.repeatValues = ['repeat', 'repeat-x', 'repeat-y', 'no-repeat'];
        S.repeat = function (item) {
    
            if (this.repeatValues.indexOf(item) >= 0) this.repeat = item;
            else this.repeat = this.defs.repeat;
        };
  • ¶

    updateMatrixNumber - internal helper function

        P.matrixNumberPosCheck = ['a', 'b', 'c', 'd', 'e', 'f'];
    
        P.updateMatrixNumber = function (item, pos) {
    
            if (!this.patternMatrix) this.patternMatrix = new DOMMatrix();
    
            item = (item.substring) ? parseFloat(item) : item;
    
            let posCheck = this.matrixNumberPosCheck.indexOf(pos);
    
            if (isa_number(item) && posCheck >= 0) 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'); };
  • ¶

    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 (Array.isArray(item)) {
    
                let 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,
                    repeat = this.repeat,
                    engine = mycell.engine;
    
                if (this.type === 'Cell' || this.type === 'Noise') {
    
                    source = this.element;
                    loaded = true;
                }
                if (engine && loaded) {
    
                    let p = engine.createPattern(source, repeat);
    
                    p.setTransform(this.patternMatrix);
    
                    return p;
                }
            }
            return 'rgba(0,0,0,0)';
        };
  • ¶

    Return the prototype

        return P;
    };