• Jump To … +
    ./demo/canvas-001.js ./demo/canvas-002.js ./demo/canvas-003.js ./demo/canvas-004.js ./demo/canvas-005.js ./demo/canvas-006.js ./demo/canvas-007.js ./demo/canvas-008.js ./demo/canvas-009.js ./demo/canvas-010.js ./demo/canvas-011.js ./demo/canvas-012.js ./demo/canvas-013.js ./demo/canvas-014.js ./demo/canvas-015.js ./demo/canvas-016.js ./demo/canvas-017.js ./demo/canvas-018.js ./demo/canvas-019.js ./demo/canvas-020.js ./demo/canvas-021.js ./demo/canvas-022.js ./demo/canvas-023.js ./demo/canvas-024.js ./demo/canvas-025.js ./demo/canvas-026.js ./demo/canvas-027.js ./demo/canvas-028.js ./demo/canvas-029.js ./demo/canvas-030.js ./demo/canvas-031.js ./demo/canvas-032.js ./demo/canvas-033.js ./demo/canvas-034.js ./demo/canvas-035.js ./demo/canvas-036.js ./demo/canvas-037.js ./demo/canvas-038.js ./demo/canvas-039.js ./demo/canvas-040.js ./demo/canvas-041.js ./demo/canvas-042.js ./demo/canvas-043.js ./demo/canvas-044.js ./demo/canvas-045.js ./demo/canvas-046.js ./demo/canvas-047.js ./demo/component-001.js ./demo/component-002.js ./demo/component-003.js ./demo/component-004.js ./demo/component-005.js ./demo/component-006.js ./demo/component-007.js ./demo/core-001.js ./demo/dom-001.js ./demo/dom-002.js ./demo/dom-003.js ./demo/dom-004.js ./demo/dom-005.js ./demo/dom-006.js ./demo/dom-007.js ./demo/dom-008.js ./demo/dom-009.js ./demo/dom-010.js ./demo/dom-011.js ./demo/dom-012.js ./demo/dom-013.js ./demo/dom-014a.js ./demo/dom-014b.js ./demo/dom-014c.js ./demo/dom-015.js ./demo/dom-016.js ./demo/filters-001.js ./demo/filters-002.js ./demo/filters-003.js ./demo/filters-004.js ./demo/filters-005.js ./demo/filters-006.js ./demo/filters-007.js ./demo/filters-008.js ./demo/filters-009.js ./demo/filters-010.js ./demo/filters-011.js ./demo/filters-012.js ./demo/filters-013.js ./demo/filters-014.js ./demo/filters-015.js ./demo/filters-016.js ./demo/filters-017.js ./demo/filters-018.js ./demo/filters-019.js ./demo/filters-020.js ./demo/filters-501.js ./demo/filters-502.js ./demo/filters-503.js ./demo/filters-504.js ./demo/filters-505.js ./demo/particles-001.js ./demo/particles-002.js ./demo/particles-003.js ./demo/particles-004.js ./demo/particles-005.js ./demo/particles-006.js ./demo/particles-007.js ./demo/particles-008.js ./demo/particles-009.js ./demo/particles-010.js ./demo/particles-011.js ./demo/particles-012.js ./demo/particles-013.js ./demo/particles-014.js ./demo/particles-015.js ./demo/particles-016.js ./demo/temp-001.js ./demo/temp-inkscapeSvgFilters.js
  • ¶

    Demo Canvas 044

    Building more complex patterns

  • ¶

    Run code

    import scrawl from '../source/scrawl.js';
  • ¶

    Scene setup

    const canvas = scrawl.library.canvas.mycanvas;
    
    canvas.setBase({
    
        backgroundColor: 'azure',
  • ¶

    The order in which we compile Cells becomes important when building more complex patterns with additional Cells

        compileOrder: 2,
    });
  • ¶

    STEP 1. We define a gradient, then apply it to some Blocks we create in a new canvas Cell. This gives us a more interesting gradient pattern than the default ‘linear’ and ‘radial’ gradients supplied by the Canvas API

    scrawl.makeGradient({
        name: 'linear',
        endX: '100%',
        endY: '100%',
    })
    .updateColor(0, 'white')
    .updateColor(420, 'red')
    .updateColor(500, 'yellow')
    .updateColor(580, 'red')
    .updateColor(999, 'black');
  • ¶

    Add a new Cell to our canvas

    const patternCell = canvas.buildCell({
    
        name: 'gradient-pattern-cell',
        dimensions: [50, 50],
        shown: false,
        compileOrder: 0,
    });
  • ¶

    Populate our new Cell with Block entitys that use our linear gradient

    scrawl.makeBlock({
    
        name: 'gradient-block-br',
        group: 'gradient-pattern-cell',
        dimensions: [25, 25],
        start: ['center', 'center'],
        fillStyle: 'linear',
        lockFillStyleToEntity: true,
    
    }).clone({
    
        name: 'gradient-block-bl',
        roll: 90,
    
    }).clone({
    
        name: 'gradient-block-tl',
        roll: 180,
    
    }).clone({
    
        name: 'gradient-block-tr',
        roll: 270,
    });
  • ¶

    STEP 2. Apply a Noise-based displacement filter to our pattern Cell. We can then animate this filter to make it more interesting

  • ¶

    Create the Noise asset

    scrawl.makeNoise({
    
        name: 'my-noise-generator',
        width: 50,
        height: 50,
        octaves: 5,
        scale: 2,
        noiseFunction: 'simplex',
    });
  • ¶

    TEST: see if we can load the Noise asset directly into a Picture entity, and into a Pattern style - we’ll use these for background textures.

    scrawl.makePicture({
    
        name: 'test-picture',
    
        dimensions: [300, 400],
        copyDimensions: ['100%', '100%'],
    
        asset: 'my-noise-generator',
    
        globalAlpha: 0.2,
    });
    
    scrawl.makePattern({
    
        name: 'test-pattern',
        asset: 'my-noise-generator',
    });
    
    scrawl.makeBlock({
    
        name: 'test-pattern-block',
        startX: 300,
        dimensions: [300, 400],
    
        fillStyle: 'test-pattern',
    
        globalAlpha: 0.2,
    });
  • ¶

    Build filters that use the Noise asset

    scrawl.makeFilter({
    
        name: 'noise',
        method: 'image',
        asset: 'my-noise-generator',
        width: 400,
        height: 400,
        copyWidth: '100%',
        copyHeight: '100%',
        lineOut: 'map',
    });
    
    const displacer =  scrawl.makeFilter({
    
        name: 'displace',
        method: 'displace',
        lineMix: 'map',
        scaleX: 20,
        scaleY: 20,
    });
  • ¶

    Update our Cell with the filters

    patternCell.set({
        filters: ['noise', 'displace']
    });
  • ¶

    Animate the displacer filter using a Tween

    scrawl.makeTween({
    
        name: 'turbulence',
        duration: 6000,
        targets: displacer,
        cycles: 0,
        reverseOnCycleEnd: true,
        definitions: [
            {
                attribute: 'scaleX',
                start: 1,
                end: 150,
                engine: 'easeOutIn'
            },
            {
                attribute: 'scaleY',
                start: 150,
                end: 1,
                engine: 'easeOutIn'
            },
        ]
    }).run();
  • ¶

    STEP 3. We are now in a position where we can use our Cells as pattern fills for some SC entitys.

    scrawl.makePolygon({
    
        name: 'hex',
        sides: 6,
        radius: 90,
        roll: 30,
        start: [470, 140],
        lineWidth: 2,
        strokeStyle: 'green',
        lineJoin: 'round',
        method: 'fillThenDraw',
  • ¶

    To use a Cell as a pattern we just assign its name to the entity’s fillStyle attribute fillStyle: ‘warped-pattern’,

        fillStyle: 'gradient-pattern-cell',
    });
  • ¶

    STEP 4. If we want, we can add some color-based filters to our entitys, to give our pattern a different look.

    scrawl.makeFilter({
    
        name: 'notred',
        method: 'notred',
    
    }).clone({
    
        name: 'sepia',
        method: 'sepia',
    
    }).clone({
    
        name: 'invert',
        method: 'invert',
    });
    
    scrawl.makeOval({
    
        name: 'egg',
        radiusX: 60,
        radiusY: 80,
        roll: 30,
        intersectY: 0.6,
        start: [70, 210],
        lineWidth: 2,
        strokeStyle: 'green',
        lineJoin: 'round',
        method: 'fillThenDraw',
        fillStyle: 'gradient-pattern-cell',
        filters: ['sepia'],
    });
    
    scrawl.makeTetragon({
    
        name: 'arrow',
        start: [160, 290],
        fillStyle: 'gradient-pattern-cell',
        radiusX: 60,
        radiusY: 80,
        intersectY: 1.2,
        intersectX: 0.32,
        roll: -60,
        filters: ['invert'],
        lineWidth: 2,
        strokeStyle: 'green',
        lineJoin: 'round',
        method: 'fillThenDraw',
    });
  • ¶

    STEP 5. There’s one additional thing we can do with our Cell-based pattern - pass it into a Pattern object where we can warp and resize it. Then we can apply it to entitys via the Pattern object.

    scrawl.makePattern({
    
        name: 'wavy-pattern',
        asset: 'gradient-pattern-cell',
        matrixB: 0.7,
        matrixF: -150,
    });
    
    scrawl.makeBlock({
    
        name: 'boring-block',
        start: [50, 50],
        dimensions: [140, 170],
        fillStyle: 'wavy-pattern',
        lineWidth: 2,
        strokeStyle: 'green',
        lineJoin: 'round',
        method: 'fillThenDraw',
    
    }).clone({
    
        name: 'tipsy-block',
        start: [250, 130],
        dimensions: [210, 90],
        roll: -30,
        filters: ['notred'],
    });
  • ¶

    Scene animation

    Function to display frames-per-second data, and other information relevant to the demo

    let report = function () {
    
        let testTicker = Date.now(),
            testTime, testNow,
            testMessage = document.querySelector('#reportmessage');
    
        return function () {
    
            testNow = Date.now();
            testTime = testNow - testTicker;
            testTicker = testNow;
    
            testMessage.textContent = `Screen refresh: ${Math.ceil(testTime)}ms; fps: ${Math.floor(1000 / testTime)}`;
        };
    }();
  • ¶

    Create the Display cycle animation

    scrawl.makeRender({
    
        name: 'demo-animation',
        target: canvas,
        afterShow: report,
    });
  • ¶

    User interaction

    scrawl.makeGroup({
        name: 'my-draggable-entitys',
    }).addArtefacts('hex', 'egg', 'arrow', 'boring-block', 'tipsy-block');
    
    scrawl.makeDragZone({
        zone: canvas,
        collisionGroup: 'my-draggable-entitys',
        endOn: ['up', 'leave'],
    });
  • ¶

    Development and testing

    console.log(scrawl.library);