import { mergeOver, mergeDiscard, xt } from '../core/utilities.js';This mixin defines additional attributes and functions for an artefact that uses delta functionality
import { mergeOver, mergeDiscard, xt } from '../core/utilities.js';export default function (P = {}) { let defaultAttributes = {delta - a Javascript object containing {key:value, key:value, etc} attributes.
artefact.updateByDelta, artefact.reverseByDelta.artefact.set, we can also update the delta object values using artefact.setDeltaValues.// This Block artefact will animate itself across the <canvas> element
// - it will move to the right and upwards until the `delta` values are updated
// - animation will stop when the `noDeltaUpdates` flag is set
let myBlock = scrawl.makeBlock({
start: ['left', 500],
delta: {
startX: 0.5,
startY: '-0.3',
},
noDeltaUpdates: false,
});
delta: null,
noDeltaUpdates: false,
};
P.defs = mergeOver(P.defs, defaultAttributes);
mergeOver(P, defaultAttributes); let S = P.setters,
D = P.deltaSetters;delta
S.delta = function (items = {}) {
if (items) this.delta = mergeDiscard(this.delta, items);
};updateByDelta - this function gets called as part of every display cycle iteration, meaning that if an attribute is set to a non-zero value in the delta attribute object then those delta animations will start playing immediately.
P.updateByDelta = function () {
if (this.name == 'kaliedoscope-clock-background') console.log(this.name, 'updateByDelta')
this.setDelta(this.delta);
return this;
};reverseByDelta - The opposite action to ‘updateByDelta’; values in the delta attribute object will be subtracted from the current value for that Scrawl-canvas object.
P.reverseByDelta = function () {
let temp = {};
Object.entries(this.delta).forEach(([key, val]) => {
if (val.substring) val = -(parseFloat(val)) + '%';
else val = -val;
temp[key] = val;
});
this.setDelta(temp);
return this;
};setDeltaValues
P.setDeltaValues = function (items = {}) {
let delta = this.delta,
oldVal, action;
Object.entries(items).forEach(([key, requirement]) => {
if (xt(delta[key])) {
action = requirement;
oldVal = delta[key];
switch (action) {
case 'reverse' :
if (oldVal.toFixed) delta[key] = -oldVal;TODO: reverse String% (and em, etc) values
break;
case 'zero' :
if (oldVal.toFixed) delta[key] = 0;TODO: zero String% (and em, etc) values
break;
case 'add' :
break;
case 'subtract' :
break;
case 'multiply' :
break;
case 'divide' :
break;
}
}
})
return this;
};Return the prototype
return P;
};