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

    UnstackedElement factory

  • ¶

    To be aware - this artefact factory is HIGHLY EXPERIMENTAL; its API will be subject to short-notice breaking changes as we amend and inprove the artefact’s functionality

  • ¶

    TODO - documentation

  • ¶

    To instantiate objects from the factory

  • ¶

    Library storage

  • ¶

    Clone functionality

  • ¶

    Kill functionality

  • ¶

    Imports

    import { unstackedelement, constructors } from '../core/library.js';
    import { xt, mergeOver } from '../core/utilities.js';
    
    import { makeCanvas } from './canvas.js';
    
    import baseMix from '../mixin/base.js';
  • ¶

    UnstackedElement constructor

    const UnstackedElement = function (el) {
        
        let name = el.id || el.name;
    
        this.makeName(name);
        this.register();
    
        el.setAttribute('data-scrawl-name', this.name);
        this.domElement = el;
    
        this.elementComputedStyles = window.getComputedStyle(el);
        this.hostStyles = {};
    
        this.canvasStartX = 0;
        this.canvasStartY = 0;
        this.canvasWidth = 0;
        this.canvasHeight = 0;
        this.canvasZIndex = 0;
    
        return this;
    };
  • ¶

    UnstackedElement object prototype setup

    let P = UnstackedElement.prototype = Object.create(Object.prototype);
    P.type = 'UnstackedElement';
    P.lib = 'unstackedelement';
    P.isArtefact = false;
    P.isAsset = false;
  • ¶

    Apply mixins to prototype object

    P = baseMix(P);
  • ¶

    Define default attributes

    let defaultAttributes = {
  • ¶

    TODO - documentation

        canvasOnTop: false,
    
    };
    P.defs = mergeOver(P.defs, defaultAttributes);
  • ¶

    Packet management

  • ¶

    TODO

  • ¶

    Define getter, setter and deltaSetter functions

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

    Define prototype functions

  • ¶

    TODO - documentation

  • ¶

    This is going to be rewritten as part of the “kill” review/recode work

    P.demolish = function (removeFromDom = false) {
    
        return true;
    };
  • ¶

    Adds a canvas element to sit behind the element, or in front of it, depending on the setting of the canvasOnTop attribute

    P.addCanvas = function (items = {}) {
    
        if (!this.canvas) {
    
            let canvas = document.createElement('canvas'),
                el = this.domElement,
                style = el.style;
    
            if (style.position === 'static') style.position = 'relative';
    
            el.prepend(canvas);
    
            let art = makeCanvas({
                name: `${this.name}-canvas`,
                domElement: canvas,
    
                position: 'absolute',
            });
    
            this.canvas = art;
    
            art.set(items);
    
            this.updateCanvas()
    
            return art;
        }
    };
  • ¶

    DOM element styles that we need to observe for changes, to keep the canvas in step with its element

    P.includedStyles = ['width', 'height', 'zIndex', 'borderBottomLeftRadius', 'borderBottomRightRadius', 'borderTopLeftRadius', 'borderTopRightRadius'];
  • ¶

    Included styles that we can transfer directly from DOM element to canvas with no further processing

    P.mimickedStyles = ['borderBottomLeftRadius', 'borderBottomRightRadius', 'borderTopLeftRadius', 'borderTopRightRadius'];
  • ¶

    Observer function - runs on every RAF loop (when the DOM element is viewable); aim is to:

  • ¶
    • keep canvas dimensions exactly matched with its DOM element’s dimensions (including border/padding)
    • position the canvas correctly over/under the DOM element (taking into account border/padding)
    • maintain parity between other DOM element CSS values and those values on the canvas (eg border radius)
    P.checkElementStyleValues = function () {
    
        let results = {};
    
        let el = this.domElement,
            wrapper = this.canvas;
    
        if (el && wrapper && wrapper.domElement) {
    
            let host = this.hostStyles,
                style = this.elementComputedStyles,
                canvas = wrapper.domElement,
                includedStyles = this.includedStyles,
                mMax = Math.max;
    
            let {x: elX, y: elY, width: elW, height: elH} = el.getBoundingClientRect();
            let {x: canvasX, y: canvasY} = canvas.getBoundingClientRect();
    
            includedStyles.forEach(item => {
    
                switch (item) {
    
                    case 'width' :
    
                        let w = mMax(parseFloat(style.width), elW);
                        if (this.canvasWidth !== w) {
    
                            this.canvasWidth = w;
                            this.dirtyDimensions = true;
                        }
                        break;
    
                    case 'height' :
    
                        let h = mMax(parseFloat(style.height), elH);
                        if (this.canvasHeight !== h) {
    
                            this.canvasHeight = h;
                            this.dirtyDimensions = true;
                        }
                        break;
    
                    case 'zIndex' :
    
                        let z = (style.zIndex === 'auto') ? 0 : parseInt(style.zIndex, 10);
                        z = (this.canvasOnTop) ? z + 1 : z - 1;
    
                        if (this.canvasZIndex !== z) {
    
                            this.canvasZIndex = z;
                            this.dirtyZIndex = true;
                        }
                        break;
    
                    default :
    
                        let hi = host[item],
                            si = style[item];
    
                        if(!xt(hi) || hi !== si) {
                    
                            host[item] = si;
                            results[item] = si;
                        }
                }
            });
    
            let dx = elX - canvasX,
                dy = elY - canvasY;
    
            if (dx || dy) {
    
                this.canvasStartX += dx;
                this.canvasStartY += dy;
    
                this.dirtyStart = true;
            }
        }
        return results;
    };
  • ¶

    Perform any updates reported by the observer function

  • ¶

    UnstackedElement canvases keep their display and base canvases in dimensional sync - which means that relatively positioned and dimensioned entitys in the canvas (those set with appropriate ‘val%’ string values) will update in line with changes in the DOM element’s width, height, padding and margin values

    P.updateCanvas = function () {
    
        if (this.canvas && this.canvas.domElement) {
    
            let canvas = this.canvas,
                style = canvas.domElement.style,
                mimics = this.mimickedStyles,
                updates = this.checkElementStyleValues();
    
            for (let [key, value] of Object.entries(updates)) {
    
                if (mimics.indexOf(key) >= 0) {
    
                    style[key] = value;
                }
            }
    
            if (this.dirtyStart) {
    
                this.dirtyStart = false;
    
                canvas.set({
                    startX: this.canvasStartX,
                    startY: this.canvasStartY,
                });
            }
    
            if (this.dirtyDimensions) {
    
                this.dirtyDimensions = false;
    
                let w = this.canvasWidth,
                    h = this.canvasHeight;
    
                canvas.set({
                    width: w,
                    height: h,
                });
                canvas.dirtyDimensions = true;
    
                canvas.base.set({
                    width: w,
                    height: h,
                });
                canvas.base.dirtyDimensions = true;
    
                canvas.cleanDimensions();
                canvas.base.cleanDimensions();
            }
    
            if (this.dirtyZIndex) {
    
                this.dirtyZIndex = false;
                style.zIndex = this.canvasZIndex;
            }
        }
    };
  • ¶

    Exported factory function

    const makeUnstackedElement = function (items) {
        
        return new UnstackedElement(items);
    };
    
    constructors.UnstackedElement = UnstackedElement;
    
    export {
        makeUnstackedElement,
    };