• 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
  • ¶

    The Animation Loop

    Scrawl-canvas runs a single, centralized requestAnimationFrame (RAF) function - animationLoop - for all animation objects.

    Animation objects are Scrawl-canvas objects with a fn attribute - a function which returns a Promise; all Animation objects contained in the animate array will be invoked as part of a Promise.all action. Once the Promise.all resolves, animationLoop is invoked again.

    The RAF function is first invoked as part of Scrawl-canvas initialization when it loads into a web page, and continues to run while the doAnimation flag remains true.

    To stop the RAF:

    scrawl.stopCoreAnimationLoop()

    To restart the RAF after it has been stopped:

    scrawl.startCoreAnimationLoop()

    … Functionality tested in Demo DOM-009

    As part of initialization, two animation objects are created and added to the animate array:

    • coreTickersAnimation (order: 0) - which will invoke all ticker animations currently subscribed to it - factory/ticker.js
    • coreListenersTracker (order: 0) - which channels window-based events (mouse/touch movement, viewport resize, scrolling) to Scrawl-canvas objects that need to take action in response (thus choking such actions to a maximum 60-per-second) - core/userInteraction.js

    Additional animations can be created via a number of ‘make’ functions:

    • makeAnimation (runs by default) - factory/animation.js
    • makeRender (runs by default) - factory/renderAnimation.js
    • makeTicker (halted by default) - factory/ticker.js
    • makeTween (halted by default) - factory/tween.js

    These objects can be added to the animate Array by invoking their run function, and removed by invoking their halt function.

    The order in which animations objects run during each RAF cycle (= Display cycle) can be (partly) determined by the objects’ order attribute: those with a lower order value will be invoked before those with a higher value.

    Note that ticker/tween animations will all run before other animations (order differences between tickers/tween objects will be respected).

  • ¶

    Imports

    import { animation } from "./library.js";
  • ¶

    Local variables

    let doAnimation = false,
        resortBatchAnimations = true,
        animate_sorted = [];
  • ¶

    Exported array (to modules). The animate array, which holds handles to all animation objects due to be run at the next RAF invocation, is exported to other modules so code can add/remove animation objects as required.

    let animate = [];
  • ¶

    Exported function (to modules). Force the animation objects to be sorted at the start of the next RAF invocation

    const resortAnimations = function () {
        resortBatchAnimations = true;
    };
  • ¶

    Scrawl-canvas animation sorter uses a ‘bucket sort’ algorithm

    const sortAnimations = function () {
    
        if (resortBatchAnimations) {
    
            resortBatchAnimations = false;
    
            let floor = Math.floor,
                buckets = [],
                obj, order;
    
            animate.forEach(name => {
    
                obj = animation[name];
    
                if (obj) {
    
                    order = floor(obj.order) || 0;
    
                    if (!buckets[order]) buckets[order] = [];
    
                    buckets[order].push(obj);
                }
            });
    
            animate_sorted = buckets.reduce((a, v) => a.concat(v), []);
        }
    };
  • ¶

    The requestAnimationFrame function

    const animationLoop = function () {
    
        let promises = [];
    
        if (resortBatchAnimations) sortAnimations();
    
        animate_sorted.forEach(item => {
    
            if (item && item.fn) promises.push(item.fn());
        });
    
        Promise.all(promises)
        .then(() => {
    
            if (doAnimation) window.requestAnimationFrame(() => animationLoop());
        })
        .catch(err => console.log('animationLoop error: ', err));
    };
  • ¶

    Exported function (modules and scrawl object). Start the RAF function running

    const startCoreAnimationLoop = function () {
    
        doAnimation = true;
        animationLoop();
    };
  • ¶

    Exported function (modules and scrawl object). Halt the RAF function

    const stopCoreAnimationLoop = function () {
        
        doAnimation = false;
    };
  • ¶

    Exports

    export {
        animate,
        resortAnimations,
        startCoreAnimationLoop,
        stopCoreAnimationLoop,
    };