• Jump To … +
    ./source/asset-management/image-asset.js ./source/asset-management/noise-asset.js ./source/asset-management/raw-asset.js ./source/asset-management/reaction-diffusion-asset.js ./source/asset-management/sprite-asset.js ./source/asset-management/video-asset.js ./source/core/animation-loop.js ./source/core/display-cycle.js ./source/core/document.js ./source/core/events.js ./source/core/init.js ./source/core/library.js ./source/core/snippets.js ./source/core/user-interaction.js ./source/factory/action.js ./source/factory/anchor.js ./source/factory/animation.js ./source/factory/bezier.js ./source/factory/block.js ./source/factory/button.js ./source/factory/canvas.js ./source/factory/cell.js ./source/factory/cog.js ./source/factory/color.js ./source/factory/conic-gradient.js ./source/factory/crescent.js ./source/factory/element.js ./source/factory/emitter.js ./source/factory/enhanced-label.js ./source/factory/filter.js ./source/factory/gradient.js ./source/factory/grid.js ./source/factory/group.js ./source/factory/label.js ./source/factory/line-spiral.js ./source/factory/line.js ./source/factory/loom.js ./source/factory/mesh.js ./source/factory/net.js ./source/factory/oval.js ./source/factory/particle-force.js ./source/factory/particle-spring.js ./source/factory/particle-world.js ./source/factory/particle.js ./source/factory/pattern.js ./source/factory/picture.js ./source/factory/polygon.js ./source/factory/polyline.js ./source/factory/quadratic.js ./source/factory/radial-gradient.js ./source/factory/rectangle.js ./source/factory/render-animation.js ./source/factory/shape.js ./source/factory/spiral.js ./source/factory/stack.js ./source/factory/star.js ./source/factory/tetragon.js ./source/factory/ticker.js ./source/factory/tracer.js ./source/factory/tween.js ./source/factory/unstacked-element.js ./source/factory/wheel.js ./source/helper/array-pool.js ./source/helper/color-engine.js ./source/helper/document-root-elements.js ./source/helper/filter-engine-bluenoise-data.js ./source/helper/filter-engine.js ./source/helper/random-seed.js ./source/helper/shape-path-calculation.js ./source/helper/shared-vars.js ./source/helper/system-flags.js ./source/helper/utilities.js ./source/helper/workstore.js ./source/mixin/anchor.js ./source/mixin/asset-advanced-functionality.js ./source/mixin/asset-consumer.js ./source/mixin/asset.js ./source/mixin/base.js ./source/mixin/button.js ./source/mixin/cascade.js ./source/mixin/cell-key-functions.js ./source/mixin/delta.js ./source/mixin/display-shape.js ./source/mixin/dom.js ./source/mixin/entity.js ./source/mixin/filter.js ./source/mixin/hidden-dom-elements.js ./source/mixin/mimic.js ./source/mixin/path.js ./source/mixin/pattern.js ./source/mixin/pivot.js ./source/mixin/position.js ./source/mixin/shape-basic.js ./source/mixin/shape-curve.js ./source/mixin/styles.js ./source/mixin/text.js ./source/mixin/tween.js ./source/scrawl.js ./source/untracked-factory/cell-fragment.js ./source/untracked-factory/coordinate.js ./source/untracked-factory/drag-zone.js ./source/untracked-factory/keyboard-zone.js ./source/untracked-factory/observe-update.js ./source/untracked-factory/palette.js ./source/untracked-factory/particle-history.js ./source/untracked-factory/quaternion.js ./source/untracked-factory/state.js ./source/untracked-factory/text-style.js ./source/untracked-factory/vector.js
  • §

    Tracer factory

    The Tracer entity generates a single non-recycled (in other words: long lasting) particle with a history, which we can use to display trace effects in the animation.

  • §

    Imports

    import { artefact, constructors } from '../core/library.js';
    
    import { doCreate, isa_fn, isa_obj, mergeOver, pushUnique, xta, λnull, Ωempty } from '../helper/utilities.js';
    
    import { currentGroup } from './canvas.js';
    import { makeParticle } from './particle.js';
    
    import { releaseVector, requestVector } from '../untracked-factory/vector.js';
    
    import baseMix from '../mixin/base.js';
    import entityMix from '../mixin/entity.js';
  • §

    Shared constants

    import { _isArray, _isFinite, _piDouble, BLACK, ENTITY } from '../helper/shared-vars.js';
  • §

    Local constants

    const T_TRACER = 'Tracer';
  • §

    Tracer constructor

    const Tracer = function (items = Ωempty) {
    
        this.makeName(items.name);
        this.register();
        this.initializePositions();
        this.set(this.defs);
  • §

    The entity has a hit zone which can be used for drag-and-drop, and other user interactions. Thus the onXYZ UI functions remain relevant.

        this.onEnter = λnull;
        this.onLeave = λnull;
        this.onDown = λnull;
        this.onUp = λnull;
  • §

    As part of its stamp functionality the Tracer entity will invoke the stampAction function. If not supplied, the entity will not display anything on the canvas.

        this.stampAction = λnull;
    
        if (!items.group) items.group = currentGroup;
  • §

    Tracer entitys use just one Particle, which gets initialized here and stored in the trace attribute

        this.trace = makeParticle(items);
    
        this.set(items);
    
        return this;
    };
  • §

    Tracer prototype

    const P = Tracer.prototype = doCreate();
    P.type = T_TRACER;
    P.lib = ENTITY;
    P.isArtefact = true;
    P.isAsset = false;
  • §

    Mixins

    baseMix(P);
    entityMix(P);
  • §

    Tracer attributes

    const defaultAttributes = {
  • §

    Note that Tracer entitys, unlike Emitters or Nets, do not need or use World objects; they are not affected by Forces or springs. They are - effectively - entitys that record their location coordinate, with the ability to remember a history of its recent locations.

    artefact - In theory, any Scrawl-canvas object whose isArtefact flag is set to true can be assigned to this attribute. However this has not been tested on non-entity artefacts. For now, stick to Scrawl-canvas entity objects.

    • Can be set using the String name of an artefact object, or the artefact object itself.
        artefact: null,
  • §

    historyLength - positive integer Number - every Particle will keep a record of its recent state, in a set of ParticleHistory arrays stored in the Particle’s history Array. The Emitter entity will set the maximum permitted length of the history array whenever it generates a new Particle.

        historyLength: 1,
  • §

    Note that the hitRadius attribute is tied directly to the width and height attributes (which are effectively meaningless for this entity)

    • This attribute is absolute - unlike other Scrawl-canvas radius attributes it cannot be set using a percentage String value
        hitRadius: 10,
  • §

    We can tell the entity to display its hit zone by setting the showHitRadius flag. The hit zone outline color attribute hitRadiusColor accepts any valid CSS color String value

        showHitRadius: false,
        hitRadiusColor: BLACK,
  • §
    Not defined in the defs object, but set up in the constructor and setters
  • §

    trace - the entity’s Particle object

  • §

    stampAction - define all major rendering actions in this function. The function receives the following arguments: (artefact, trace, host) - where artefact is the Tracer entity’s artefact object (if any has been defined/set); trace is the entity’s Particle object whose history needs to be rendered onto the canvas; and host is the Cell wrapper on which we will draw our graphics

    };
    P.defs = mergeOver(P.defs, defaultAttributes);
  • §

    Packet management

    P.packetObjects = pushUnique(P.packetObjects, ['artefact', 'trace']);
    P.packetFunctions = pushUnique(P.packetFunctions, ['stampAction']);
    
    P.finalizePacketOut = function (copy) {
    
        return copy;
    };
  • §

    Clone management

    P.postCloneAction = function(clone, items) {
    
        clone.trace = makeParticle({
            name: clone.name,
            historyLength: items.historyLength || this.historyLength || 1,
        });
    
        return clone;
    };
  • §

    Kill management

    P.factoryKill = function (killArtefact) {
    
        if (killArtefact) this.artefact.kill();
        this.trace.kill();
    };
  • §

    Get, Set, deltaSet

    const S = P.setters;
    
    S.stampAction = function (item) {
    
        if (isa_fn(item)) this.stampAction = item;
    };
    
    S.artefact = function (item) {
    
        let art;
    
        if (item.substring) art = artefact[item];
        else if (isa_obj(item) && item.isArtefact) art = item;
    
        if (art) {
    
            this.artefact = art;
            this.dirtyFilterIdentifier = true;
        }
    };
  • §

    Prototype functions

  • §

    regularStamp - overwriters the functionality defined in the entity.js mixin

    P.regularStamp = function () {
    
        const {artefact, trace, stampAction, showHitRadius, hitRadius, hitRadiusColor, currentStampPosition} = this;
    
        const host = this.currentHost;
    
        trace.set({
            position: currentStampPosition,
        });
    
        trace.manageHistory(0, host);
        stampAction.call(this, artefact, trace, host);
    
        if (showHitRadius) {
    
            const engine = host.engine;
    
            engine.save();
            engine.lineWidth = 1;
            engine.strokeStyle = hitRadiusColor;
    
            engine.resetTransform();
            engine.beginPath();
            engine.arc(currentStampPosition[0], currentStampPosition[1], hitRadius, 0, _piDouble);
            engine.stroke();
    
            engine.restore();
        }
    };
  • §

    checkHit - overwrites the function defined in mixin/position.js

    • The Tracer entity’s hit area is a circle centred on the entity’s rotation/reflection (start) position or, where the entity’s position is determined by reference (pivot, mimic, path, etc), the reference’s current position.
    • Tracer entitys can be dragged and dropped around a canvas display like any other Scrawl-canvas artefact.
    P.checkHit = function (items = []) {
    
        if (this.noUserInteraction) return false;
    
        const tests = (!_isArray(items)) ?  [items] : items;
    
        const currentStampPosition = this.currentStampPosition;
    
        let res = false,
            tx, ty;
    
        if (tests.some(test => {
    
            if (_isArray(test)) {
    
                tx = test[0];
                ty = test[1];
            }
            else if (xta(test, test.x, test.y)) {
    
                tx = test.x;
                ty = test.y;
            }
            else return false;
    
            if (!_isFinite(tx) || !_isFinite(ty)) return false;
    
            const v = requestVector(currentStampPosition).vectorSubtract(test);
    
            if (v.getMagnitude() < this.hitRadius) res = true;
    
            releaseVector(v);
    
            return res;
    
        }, this)) {
    
            const r = this.checkHitReturn(tx, ty);
    
            return r;
        }
        return false;
    };
  • §

    Factory

    
    
    export const makeTracer = function (items) {
    
        if (!items) return false;
        return new Tracer(items);
    };
    
    constructors.Tracer = Tracer;