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

    Canvas factory

    Scrawl-canvas mediates between its system and <canvas> elements in the DOM using Canvas wrapper objects. The wrapper includes a handle to the visible element, but most work is done on a second <canvas> element - the base Cell - which is not part of the DOM, thus excluded from a browser’s Document interface (including events).

    The Canvas factory is not used directly; the factory is not exported as part of the scrawl object during Scrawl-canvas initialization. Instead, wrappers can be created for DOM-based <canvas> elements using the following scrawl functions:

    • scrawl.getCanvas - locates a <canvas> element in the DOM and creates a wrapper for it.
    • scrawl.addCanvas - generates a new <canvas> element, creates a wrapper for it, then adds it to the DOM.

    During initialization Scrawl-canvas will search the DOM tree and automatically create Canvas wrappers for all the <canvas> elements it discovers. The first <canvas> element discovered becomes the current canvas; all entitys created without a specified group attribute will be assigned to that element’s wrapper’s base Cell’s Group object. We can change the current canvas by invoking the scrawl.setCurrentCanvas function.

    A canvas wrapper can include more than one Cell object. It will always include a base Cell object; additional Cells can be treated as cell layers and/or normal artefacts contributing to the final display.

    During their creation, Canvas wrappers will directly modify the DOM, adding <div> and <nav> elements to it. These new elements are used as holds where the Canvas will store data and text, mainly to expose <a> links and <p> blocks which expose scene details to assistive technologies (accessibility). These additional elements have zero dimensions and should not affect the layout or painting of the rest of the web page.

    Canvas wrapper objects use the base, position, dom and anchor mixins. Thus Canvas wrappers are also artefact objects: if a <canvas> element is a direct child of a Stack wrapper’s element then it can be positioned, dimensioned and rotated like any other artefact.

    By default, all Canvas wrappers will track mouse/touch movements across their <canvas> elements, supplying this data to constituent Cell objects and artefacts as-and-when-required.

    Canvas wrappers are used by Scrawl-canvas to invoke the Display cycle cascade. As such, they include clear, compile, show and render functions to manage the Display cycle. These functions are asynchronous, returning Promises.

    Canvas wrappers are excluded from the Scrawl-canvas packet system; they cannot be saved or cloned. Killing a Canvas wrapper will remove its <canvas> element from the DOM, alongside the additional elements added to the DOM during Canvas creation.

  • ¶

    Demos:

    • All canvas and component demos, and a few of the stack demos, include Canvas wrapper functionality - most of which happens behind the scenes and does not need to be directly coded.
    • Canvas-009 - Pattern styles; Entity web link anchors; Dynamic accessibility
    • DOM-011 - Canvas controller fit attribute; Cell positioning (mouse)
    • DOM-012 - Add and remove (kill) Scrawl-canvas canvas elements programmatically
    • Component-001 - Scrawl-canvas DOM element components
  • ¶

    Imports

    import { cell, constructors, artefact, group } from '../core/library.js';
    import { rootElements, setRootElementsSort, setCurrentCanvas, domShow, scrawlCanvasHold } from '../core/document.js';
    import { mergeOver, pushUnique, removeItem, xt, λthis, λnull } from '../core/utilities.js';
    import { uiSubscribedElements } from '../core/userInteraction.js';
    
    import { makeState } from './state.js';
    import { makeCell } from './cell.js';
    import { makeCoordinate } from './coordinate.js';
    
    import baseMix from '../mixin/base.js';
    import domMix from '../mixin/dom.js';
    import displayMix from '../mixin/displayShape.js';
  • ¶

    Canvas constructor

    const Canvas = function (items = {}) {
    
        let g, el;
    
        this.makeName(items.name);
        this.register();
        this.initializePositions();
    
        this.dimensions[0] = 300;
        this.dimensions[1] = 150;
    
        this.pathCorners = [];
        this.css = {};
        this.here = {};
        this.perspective = {
    
            x: '50%',
            y: '50%',
            z: 0
        };
        this.dirtyPerspective = true;
    
        this.initializeDomLayout(items);
    
        this.set(this.defs);
    
        if (!items.label) items.label = `${this.name} canvas element`;
    
        this.initializeDisplayShapeActions();
    
        this.set(items);
    
        this.cleanDimensions();
    
        el = this.domElement;
    
        if (el) {
    
            this.engine = this.domElement.getContext('2d');
    
            this.state = makeState({
                engine: this.engine
            });
  • ¶

    set up additional cells array

            this.cells = [];
            this.cellBatchesClear = [];
            this.cellBatchesCompile = [];
            this.cellBatchesShow = [];
  • ¶

    setup base cell

            let cellArgs = {
                name: `${this.name}_base`,
                element: false,
                width: this.currentDimensions[0],
                height: this.currentDimensions[1],
                cleared: true,
                compiled: true,
                shown: false,
                isBase: true,
                host: this.name,
                controller: this,
                order: 10,
            };
            this.base = this.buildCell(cellArgs);
  • ¶

    even if it is a child of a stack element, it needs to be recorded as a ‘rootElement’

            pushUnique(rootElements, this.name);
            setRootElementsSort();
  • ¶
    Accessibility
            if (!el.getAttribute('role')) el.setAttribute('role', 'img');
    
            let navigation = document.createElement('nav');
            navigation.id = `${this.name}-navigation`;
            navigation.style.width = '0px';
            navigation.style.height = '0px';
            navigation.style.maxWidth = '0px';
            navigation.style.maxHeight = '0px';
            navigation.style.border = '0px';
            navigation.style.padding = '0px';
            navigation.style.margin = '0px';
            navigation.style.overflow = 'hidden';
            this.navigation = navigation;
            el.appendChild(navigation);
    
            let textHold = document.createElement('div');
            textHold.id = `${this.name}-text-hold`;
            textHold.style.width = '0px';
            textHold.style.height = '0px';
            textHold.style.maxWidth = '0px';
            textHold.style.maxHeight = '0px';
            textHold.style.border = '0px';
            textHold.style.padding = '0px';
            textHold.style.margin = '0px';
            textHold.style.overflow = 'hidden';
            this.textHold = textHold;
            el.appendChild(textHold);
    
            let ariaLabel = document.createElement('div');
            ariaLabel.id = `${this.name}-ARIA-label`;
            ariaLabel.textContent = this.label;
            this.ariaLabelElement = ariaLabel;
            scrawlCanvasHold.appendChild(ariaLabel);
            el.setAttribute('aria-labelledby', ariaLabel.id);
            el.setAttribute('aria-live', 'polite');
    
            let ariaDescription = document.createElement('div');
            ariaDescription.id = `${this.name}-ARIA-description`;
            ariaDescription.textContent = this.description;
            this.ariaDescriptionElement = ariaDescription;
            scrawlCanvasHold.appendChild(ariaDescription);
            el.setAttribute('aria-describedby', ariaDescription.id);
            el.setAttribute('aria-live', 'polite');
    
            this.cleanAria();
        }
    
        this.dirtyCells = true;
        this.apply();
    
        this.dirtyDomDimensions = true;
        if (items.setAsCurrentCanvas) this.setAsCurrentCanvas();
        
        return this;
    };
  • ¶

    Canvas prototype

    let P = Canvas.prototype = Object.create(Object.prototype);
    P.type = 'Canvas';
    P.lib = 'canvas';
    P.isArtefact = true;
    P.isAsset = false;
  • ¶

    Mixins

    • base
    • dom
    • display
    P = baseMix(P);
    P = domMix(P);
    P = displayMix(P);
  • ¶

    Canvas attributes

    • Attributes defined in the base mixin: name.
    • Attributes defined in the position mixin: group, visibility, order, start, startX, startY, handle, handleX, handleY, offset, offsetX, offsetY, dimensions, width, height, pivoted, mimicked, lockTo, lockXTo, lockYTo, scale, roll, noUserInteraction, noPositionDependencies, noCanvasEngineUpdates, noFilters, noPathUpdates, purge, bringToFrontOnDrag.
    • Attributes defined in the delta mixin: delta, noDeltaUpdates.
    • Attributes defined in the pivot mixin: pivot, pivotCorner, addPivotHandle, addPivotOffset, addPivotRotation.
    • Attributes defined in the mimic mixin: mimic, useMimicDimensions, useMimicScale, useMimicStart, useMimicHandle, useMimicOffset, useMimicRotation, useMimicFlip, addOwnDimensionsToMimic, addOwnScaleToMimic, addOwnStartToMimic, addOwnHandleToMimic, addOwnOffsetToMimic, addOwnRotationToMimic.
    • Attributes defined in the path mixin: path, pathPosition, addPathHandle, addPathOffset, addPathRotation, constantPathSpeed.
    • Attributes defined in the anchor mixin: anchor.
    • Attributes defined in the dom mixin: domElement, pitch, yaw, offsetZ, css, classes, position, actionResize, trackHere, domAttributes.
    • Attributes defined in the display mixin: breakToBanner, breakToLandscape, breakToPortrait, breakToSkyscraper, actionBannerShape, actionLandscapeShape, actionRectangleShape, actionPortraitShape, actionSkyscraperShape.
    let defaultAttributes = {
  • ¶

    position - the CSS position value for the <canvas> element. This value will be set to absolute when the element is an artefact associated with a Stack; relative in other cases.

        position: 'relative',
  • ¶

    trackHere

        trackHere: 'subscribe',
  • ¶

    fit - String indicating how the base Cell should copy its contents over to the <canvas> element as the final step in the Display cycle. Accepted values are: fill, contain, cover, none (but not scale-down).

    The aim of this functionality is to replicate the CSS object-fit property - detailed here - for <canvas> elements. Unlike the CSS property, we apply the fit attribute to the Canvas wrapper, not the element itself or its parent element.

        fit: 'none',
  • ¶

    isComponent - set to true if canvas is being used as part of a Scrawl-canvas component.

        isComponent: false,
  • ¶

    renderOnResize - perform an additional render of the scene following a change in the canvas’s dimensions

        renderOnResize: true,
  • ¶
    Accessibility attributes

    <canvas> elements are raster images - they contain no information within their content (beyond pixel data) which can be analyzed or passed on to the browser or other device. The element can include title and various item attributes (alongside custom data- attributes) but inclusion of these depends entirely on the developer remembering to include them when coding up a web page.

    Scrawl-canvas attempts to automate some (but not all) accessibility work through inclusion of the following Canvas attributes (specifically, Scrawl-canvas implements ARIA attributes):

    • title - this attribute is applied to the <canvas> element’s ‘title’ attribute, and will appear as a tooltip when the user hovers over the canvas
    • label and description - these attributes are applied to (offscreen) div elements which are referenced by the <canvas> element using aria-labelledby and aria-describedby attributes

    If no label value is supplied to the Canvas factory (as part of the function’s argument object), then Scrawl-canvas will auto-generate a label based on the canvas’s name. All three attributes can be updated dynamically using the usual set() functionality.

    Beyond the Canvas object, Scrawl-canvas also encourages Phrase entitys (which handle graphical text in the canvas display) to expose their content to the DOM, to make it accessible. Also, any artefact given an Anchor link will expose the Anchor’s <a> element in the DOM, which allows the canvas display to become part of the document’s navigation (for example, by keyboard tabbing).

        title: '',
        label: '',
        description: '',
    };
    P.defs = mergeOver(P.defs, defaultAttributes);
  • ¶

    Packet/Clone management

    This functionality is disabled for Canvas objects

    P.stringifyFunction = λnull;
    P.processPacketOut = λnull;
    P.finalizePacketOut = λnull;
    P.saveAsPacket = function () {
    
        return `[${this.name}, ${this.type}, ${this.lib}, {}]`
    };
    P.clone = λthis;
  • ¶

    Kill functionality

    P.factoryKill = function () {
    
        let name = this.name,
            host = this.host,
            h, g;
  • ¶

    rootElements and uiSubscribedElements arrays

        removeItem(rootElements, name);
        setRootElementsSort();
    
        removeItem(uiSubscribedElements, name);
  • ¶

    Host and host Group

        if (host && host !== 'root') {
    
            h = (this.currentHost) ? this.currentHost : artefact[host];
    
            if (h) {
    
                h.removeGroups(name);
    
                g = group[h.name];
                if (g) g.removeArtefacts(name);
            }
        }
  • ¶

    Canvas Group

        if (group[name]) group[name].kill();
  • ¶

    Base Cell

        this.base.kill();
  • ¶

    DOM removals

        this.navigation.remove();
        this.textHold.remove();
        this.ariaLabelElement.remove();
        this.ariaDescriptionElement.remove();
        this.domElement.remove();
    };
  • ¶

    Get, Set, deltaSet

    let G = P.getters,
        S = P.setters,
        D = P.deltaSetters;
  • ¶

    fit

    P.fitDefaults = ['fill', 'contain', 'cover'];
    S.fit = function (item) {
    
        this.fit = (this.fitDefaults.indexOf(item) >= 0) ? item : 'none';
    };
  • ¶

    title - String

    S.title = function (item) {
    
        this.title = item;
        this.dirtyAria = true;
    };
  • ¶

    label - String

    S.label = function (item) {
    
        this.label = item;
        this.dirtyAria = true;
    };
  • ¶

    description - String

    S.description = function (item) {
    
        this.description = item;
        this.dirtyAria = true;
    };
  • ¶
    Get and set base cell attributes

    For convenience, Scrawl-canvas allows us to get/set a limited number of base Cell attributes via their Canvas wrapper’s get, set and deltaSet functions. We can set and deltaSet other base Cell attributes by invoking the mycanvas.setBase and mycanvas.deltaSetBase functions.

  • ¶

    backgroundColor - String. We can set the base Cell’s background color to any CSS color String value

    G.backgroundColor = function () {
    
        return this.base.backgroundColor;
    };
    S.backgroundColor = function (item) {
    
        if (this.base) {
    
            this.base.set({
                backgroundColor: item
            });
        }
    };
  • ¶

    alpha - float Number between 0.0 and 1.0. When the base Cell stamps itself onto the Canvas wrapper’s <canvas> element, it can set the element’s engine to a globalAlpha value stored in the Cell’s alpha attribute.

    G.alpha = function () {
    
        return this.base.alpha;
    };
    S.alpha = function (item) {
    
        if (this.base) {
    
            this.base.set({
                alpha: item
            });
        }
    };
    D.alpha = function(item) {
    
        if (this.base) {
    
            this.base.deltaSet({
                alpha: item
            });
        }
    };
  • ¶

    composite - String. When the base Cell stamps itself onto the Canvas wrapper’s <canvas> element, it can set the element’s engine to a globalCompositeOperation value stored in the Cell’s composite attribute.

    G.composite = function () {
    
        return this.base.composite;
    };
    S.composite = function (item) {
    
        if (this.base) {
            this.base.set({
                composite: item
            });
        }
    };
  • ¶

    Prototype functions

  • ¶

    setAsCurrentCanvas - invoke function (without arguments) to make this Canvas wrapper the current canvas for future entity generation operations. This is additional to the scrawl.setCurrentCanvas function.

    P.setAsCurrentCanvas = function () {
    
        if (this.base) setCurrentCanvas(this);
        return this;
    };
  • ¶

    setBase - pass the argument object through to the base Cell’s set function.

    P.setBase = function (items) {
    
        if (this.base) {
    
            this.base.set(items);
            this.setBaseHelper();
        }
        return this;
    };
  • ¶

    deltaSetBase - pass the argument object through to the base Cell’s deltaSet function.

    P.deltaSetBase = function (items) {
    
        if (this.base) {
    
            this.base.deltaSet(items);
            this.setBaseHelper();
        }
        return this;
    };
  • ¶

    Internal function - passes the Canvas wrapper’s current here object and fit attribute to the base Cell for further processing

    P.updateBaseHere = function () {
    
        if (this.base) this.base.updateBaseHere(this.here, this.fit);
    };
  • ¶

    Internal helper function

    P.setBaseHelper = function () {
    
        let items = {};
    
        if (this.base.dirtyScale) items.dirtyScale = true;
        if (this.base.dirtyDimensions) items.dirtyDimensions = true;
        if (this.base.dirtyLock) items.dirtyLock = true;
        if (this.base.dirtyStart) items.dirtyStart = true;
        if (this.base.dirtyOffset) items.dirtyOffset = true;
        if (this.base.dirtyHandle) items.dirtyHandle = true;
        if (this.base.dirtyRotation) items.dirtyRotation = true;
    
        this.cleanCells();
        this.base.prepareStamp();
    
        this.updateCells(items);
    };
  • ¶

    updateCells - internal function. Iterate through all Cells registered in the wrapper’s cells Array and set a range of dirty flags on them, for future processing by each Cell.

    P.updateCells = function (items = {}) {
    
        this.cells.forEach(name => {
    
            let mycell = cell[name];
    
            if (mycell) {
    
                if (items.dirtyScale) mycell.dirtyScale = true;
                if (items.dirtyDimensions) mycell.dirtyDimensions = true;
                if (items.dirtyLock) mycell.dirtyLock = true;
                if (items.dirtyStart) mycell.dirtyStart = true;
                if (items.dirtyOffset) mycell.dirtyOffset = true;
                if (items.dirtyHandle) mycell.dirtyHandle = true;
                if (items.dirtyRotation) mycell.dirtyRotation = true;
            }
        });
    };
  • ¶

    buildCell - internal helper function, called by the Canvas constructor

    P.buildCell = function (items = {}) {
    
        let host = items.host || false;
    
        if (!host) items.host = this.base.name;
    
        let mycell = makeCell(items);
        
        this.addCell(mycell);
        this.cleanCells();
        return mycell;
    };
  • ¶

    cleanDimensionsAdditionalActions - overwrites mixin/position function. Promulgates canvas dimension changes through to all Cells in the wrapper’s cells Array.

    P.cleanDimensionsAdditionalActions = function () {
    
        if (this.cells) {
    
            this.updateCells({
                dirtyDimensions: true,
            });
        }
    
        this.dirtyDomDimensions = true;
        this.dirtyDisplayShape = true;
        this.dirtyDisplayArea = true;
    };
  • ¶

    addCell - add a Cell object to the wrapper’s cells Array; argument can be the Cell’s name-String, or the Cell object itself

    P.addCell = function (item) {
    
        item = (item.substring) ? item : item.name || false;
    
        if (item) {
    
            pushUnique(this.cells, item);
            cell[item].prepareStamp();
            this.dirtyCells = true;
        }
        return item;
    };
  • ¶

    removeCell - remove a Cell object from the wrapper’s cells Array; argument can be the Cell’s name-String, or the Cell object itself

    P.removeCell = function (item) {
    
        item = (item.substring) ? item : item.name || false;
    
        if (item) {
    
            removeItem(this.cells, item);
            this.dirtyCells = true;
        }
        return this;
    };
  • ¶

    killCell - invoke a Cell object’s kill function; argument can be the Cell’s name-String, or the Cell object itself

    P.killCell = function (item) {
    
        let mycell = (item.substring) ? cell[item] : item;
    
        if (mycell) mycell.kill();
    
        this.dirtyCells = true;
        return this;
    };
  • ¶
    Display cycle functions
  • ¶

    clear - For Cell objects in the wrapper’s cells (and associated) Arrays (returns a Promise):

    • if the Cell object’s cleared flag is true, invoke the Cell’s clear function
    P.clear = function () {
    
        let self = this;
    
        if (self.dirtyCells) self.cleanCells();
    
        let next = (counter) => {
    
            return new Promise((resolve, reject) => {
    
                let myCell = self.cellBatchesClear[counter];
    
                if (myCell) {
    
                    myCell.clear()
                    .then(res => {
    
                        next(counter + 1)
                        .then(res => resolve(true))
                        .catch(err => reject(err));
                    })
                    .catch(err => reject(err));
                }
                else resolve(true);
            });
        };
        return next(0);
    };
  • ¶

    compile - For Cell objects in the wrapper’s cells (and associated) Arrays (returns a Promise):

    • sort Cell objects depending on their compileOrder attribute - Cells with lower order values will be processed first
    • if the Cell object’s compiled flag is true, invoke the Cell’s compile function
    P.compile = function () {
    
        let self = this;
    
        if (self.dirtyCells) self.cleanCells();
    
        let next = (counter) => {
    
            return new Promise((resolve, reject) => {
    
                let myCell = self.cellBatchesCompile[counter];
    
                if (myCell) {
    
                    myCell.compile()
                    .then(res => {
    
                        next(counter + 1)
                        .then(res => resolve(true))
                        .catch(err => reject(err));
                    })
                    .catch(err => reject(err));
                }
                else {
    
                    self.prepareStamp();
    
                    self.stamp()
                    .then(res => resolve(true))
                    .catch(err => reject(err));
                }
            });
        };
        return next(0);
    };
  • ¶

    show - For Cell objects in the wrapper’s cells (and associated) Arrays (returns a Promise):

    • sort Cell objects depending on their showOrder attribute - Cells with lower order values will be processed first
    • if the Cell object’s shown flag is true, invoke the Cell’s show function

    And then:

    • copy the base Cells display over to the Canvas wrapper’s <canvas> element
    • update ARIA and other associated metadata
    P.show = function(){
    
        let self = this;
    
        if (self.dirtyCells) self.cleanCells();
    
        let next = (counter) => {
    
            return new Promise((resolve, reject) => {
    
                let myCell = self.cellBatchesShow[counter];
    
                if (myCell) {
    
                    myCell.show()
                    .then(res => {
    
                        next(counter + 1)
                        .then(res => resolve(true))
                        .catch(err => reject(err));
                    })
                    .catch(err => reject(err));
                }
                else {
    
                    self.engine.clearRect(0, 0, self.localWidth, self.localHeight);
                    
                    self.base.show()
                    .then(res => {
    
                        domShow();
    
                        if (this.dirtyAria) this.cleanAria();
    
                        resolve(true);
                    })
                    .catch(err => reject(err));
                }
            });
        };
        return next(0);
    };
  • ¶

    render - orchestrate a single Display cycle - clear, then compile, then show (returns a Promise).

    P.render = function () {
    
        let self = this;
    
        return new Promise((resolve) => {
    
            self.clear()
            .then(() => self.compile())
            .then(() => self.show())
            .then(() => resolve(true))
            .catch(() => resolve(false));
        });
    };
  • ¶

    cleanCells - internal function triggered each time there is an update to the cells Array attribute, or a constituent Cell sets the Canvas wrapper’s dirtyCell flag.

    • Cell objects include attribute flags - cleared, compiled, shown - which tell their Canvas wrapper whether they should be included in each part of the Display cycle
    • For the compile and show steps, Cell objects also include attributes - compileOrder, showOrder - indicating the order in which they should be processed (compared to other Cell objects).
    • The sorting algorithm for all three operations is a bucket sort.
    • The results of these operations are deposited in three internal Arrays which are used as part of the Canvas wrapper’s clear/compile/show functionality.

    We do things this way because there may be situations where a Cell needs to calculate and compile before another cell, but should be shown (applied to) the base Cell after the other Cells (so it appears on top of them).

    P.cleanCells = function () {
    
        this.dirtyCells = false;
    
        let tempClear = [],
            tempCompile = [],
            tempShow = [],
            cells = this.cells,
            order;
    
        cells.forEach(item => {
    
            let mycell = cell[item];
    
            if (mycell) {
    
                if (mycell.cleared) tempClear.push(mycell);
    
                if (mycell.compiled) {
    
                    order = mycell.compileOrder;
    
                    if (!tempCompile[order]) tempCompile[order] = [];
    
                    tempCompile[order].push(mycell);
                }
    
                if (mycell.shown) {
    
                    order = mycell.showOrder;
    
                    if (!tempShow[order]) tempShow[order] = [];
                    tempShow[order].push(mycell);
                }
            }
        });
    
        this.cellBatchesClear = [].concat(tempClear);
        this.cellBatchesCompile = tempCompile.reduce((a, v) => a.concat(v), []);
        this.cellBatchesShow = tempShow.reduce((a, v) => a.concat(v), []);
    };
  • ¶
    Handling DOM events

    cascadeEventAction - The Canvas wrapper’s <canvas> element is part of the DOM, thus it is able to participate in all normal DOM events. We use the cascadeEventAction function to tell the wrapper which of the events it receives should be cascaded down to its constituent Cell objects and, in turn, their Groups’ artefacts.

    This allows us to ‘regionalize’ various parts of the <canvas> element so that they respond in a similar way to an HTML image map (defined using <map> and <area> elements)

    • Cascaded events are limited to mouse and touch events, which Scrawl-canvas bundles together into 5 types of event: down, up (which also captures click events), enter, leave, move.
    • Returns an Array of name Strings for the entitys at the current mouse cursor coordinates; the Array is also accessible from the Canvas wrapper’s currentActiveEntityNames attribute.
    P.cascadeEventAction = function (action) {
    
        if (!this.currentActiveEntityNames) this.currentActiveEntityNames = [];
    
        let currentActiveEntityNames = this.currentActiveEntityNames,
            testActiveEntityObjects = [],
            testActiveEntityNames = [],
            newActiveEntityObjects = [],
            newActiveEntityNames = [],
            knownActiveEntityObjects = [],
            knownActiveEntityNames = [];
  • ¶
    1. Find all entitys currently colliding with the mouse/touch coordinate over the canvas
        this.cells.forEach(name => {
    
            let myCell = cell[name];
    
            if (myCell && (myCell.shown || myCell.isBase)) testActiveEntityObjects.push(myCell.getEntityHits());
        });
    
        testActiveEntityObjects = testActiveEntityObjects.reduce((a, v) => a.concat(v), []);
  • ¶
    1. Process the returned test results
        testActiveEntityObjects.forEach(item => {
    
            let name = item.name;
    
            if (testActiveEntityNames.indexOf(name) < 0) {
    
                testActiveEntityNames.push(name);
    
                if (currentActiveEntityNames.indexOf(name) < 0) {
    
                    newActiveEntityObjects.push(item);
                    newActiveEntityNames.push(name);
                }
                else {
    
                    knownActiveEntityObjects.push(item);
                    knownActiveEntityNames.push(name);
                }
            }
        });
    
        let currentActiveEntityObjects = newActiveEntityObjects.concat(knownActiveEntityObjects);
  • ¶
    1. Trigger the required action on each affected entity
        let doLeave = function () {
    
            if (currentActiveEntityNames.length) {
    
                newActiveEntityNames.forEach(name => removeItem(currentActiveEntityNames, name));
                knownActiveEntityNames.forEach(name => removeItem(currentActiveEntityNames, name));
    
                currentActiveEntityNames.forEach(name => {
    
                    let myArt = artefact[name];
    
                    if (myArt) myArt.onLeave();
                });
            }
        };
    
        switch (action) {
    
            case 'down' :
                currentActiveEntityObjects.forEach(art => art.onDown());
                break;
    
            case 'up' :
                currentActiveEntityObjects.forEach(art => art.onUp());
                break;
    
            case 'enter' :
                newActiveEntityObjects.forEach(art => art.onEnter());
                break;
    
            case 'leave' :
                doLeave();
                break;
    
            case 'move' :
                doLeave();
                newActiveEntityObjects.forEach(art => art.onEnter());
                break;
        }
  • ¶
    1. Cleanup and return
        this.currentActiveEntityNames = newActiveEntityNames.concat(knownActiveEntityNames);
    
        return this.currentActiveEntityNames;
    };
  • ¶

    cleanAria - internal function; transfers updated title, label and description attribute values into the relevant DOM elements

    P.cleanAria = function () {
    
        this.dirtyAria = false;
        this.domElement.setAttribute('title', this.title);
        this.ariaLabelElement.textContent = this.label;
        this.ariaDescriptionElement.textContent = this.description;
    }
  • ¶

    Factory

    const makeCanvas = function (items) {
        return new Canvas(items);
    };
    
    constructors.Canvas = Canvas;
  • ¶

    Exports

    export {
        makeCanvas,
    };