Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | 4x 4x 4x 4x 4x 1x 4x 1x 1x 1x 1x 1x 1x 3x 3x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 1x 1x 4x 4x 4x | export default function configure(sigma) {
Iif (typeof sigma === "undefined") throw new Error("sigma is not declared");
// Initialize packages:
sigma.utils.pkg("sigma.misc.animation.running");
/**
* Generates a unique ID for the animation.
*
* @return {string} Returns the new ID.
*/
const _getID = (function getId() {
let id = 0;
return function next() {
return `${++id}`;
};
})();
/**
* This function animates a camera. It has to be called with the camera to
* animate, the values of the coordinates to reach and eventually some
* options. It returns a number id, that you can use to kill the animation,
* with the method sigma.misc.animation.kill(id).
*
* The available options are:
*
* {?number} duration The duration of the animation.
* {?function} onNewFrame A callback to execute when the animation
* enter a new frame.
* {?function} onComplete A callback to execute when the animation
* is completed or killed.
* {?(string|function)} easing The name of a function from the package
* sigma.utils.easings, or a custom easing
* function.
*
* @param {camera} camera The camera to animate.
* @param {object} target The coordinates to reach.
* @param {?object} options Eventually an object to specify some options to
* the function. The available options are
* presented in the description of the function.
* @return {number} The animation id, to make it easy to kill
* through the method "sigma.misc.animation.kill".
*/
sigma.misc.animation.camera = function animationCamera(camera, val, options) {
Iif (
!(camera instanceof sigma.classes.camera) ||
typeof val !== "object" ||
!val
)
throw new Error("animation.camera: Wrong arguments.");
Iif (
typeof val.x !== "number" &&
typeof val.y !== "number" &&
typeof val.ratio !== "number" &&
typeof val.angle !== "number"
)
throw new Error(
"There must be at least one valid coordinate in the given val."
);
let id;
let anim;
const o = options || {};
const start = sigma.utils.dateNow();
// Store initial values:
const initialVal = {
x: camera.x,
y: camera.y,
ratio: camera.ratio,
angle: camera.angle
};
const easing =
typeof o.easing !== "function"
? sigma.utils.easings[o.easing || "quadraticInOut"]
: o.easing;
function tick() {
let coef;
const t = o.duration ? (sigma.utils.dateNow() - start) / o.duration : 1;
// If the animation is over:
if (t >= 1) {
camera.isAnimated = false;
camera.goTo({
x: val.x !== undefined ? val.x : initialVal.x,
y: val.y !== undefined ? val.y : initialVal.y,
ratio: val.ratio !== undefined ? val.ratio : initialVal.ratio,
angle: val.angle !== undefined ? val.angle : initialVal.angle
});
cancelAnimationFrame(id);
delete sigma.misc.animation.running[id];
// Check callbacks:
Eif (typeof o.onComplete === "function") o.onComplete();
// Else, let's keep going:
} else {
coef = easing(t);
camera.isAnimated = true;
camera.goTo({
x:
val.x !== undefined
? initialVal.x + (val.x - initialVal.x) * coef
: initialVal.x,
y:
val.y !== undefined
? initialVal.y + (val.y - initialVal.y) * coef
: initialVal.y,
ratio:
val.ratio !== undefined
? initialVal.ratio + (val.ratio - initialVal.ratio) * coef
: initialVal.ratio,
angle:
val.angle !== undefined
? initialVal.angle + (val.angle - initialVal.angle) * coef
: initialVal.angle
});
// Check callbacks:
Eif (typeof o.onNewFrame === "function") o.onNewFrame();
anim.frameId = requestAnimationFrame(tick);
}
}
id = _getID();
anim = {
frameId: requestAnimationFrame(tick),
target: camera,
type: "camera",
options: o,
fn: tick
};
sigma.misc.animation.running[id] = anim;
return id;
};
/**
* Kills a running animation. It triggers the eventual onComplete callback.
*
* @param {number} id The id of the animation to kill.
* @return {object} Returns the sigma.misc.animation package.
*/
sigma.misc.animation.kill = function kill(id) {
if (arguments.length !== 1 || typeof id !== "number")
throw new Error("animation.kill: Wrong arguments.");
const o = sigma.misc.animation.running[id];
if (o) {
cancelAnimationFrame(id);
delete sigma.misc.animation.running[o.frameId];
if (o.type === "camera") o.target.isAnimated = false;
// Check callbacks:
if (typeof (o.options || {}).onComplete === "function")
o.options.onComplete();
}
return this;
};
/**
* Kills every running animations, or only the one with the specified type,
* if a string parameter is given.
*
* @param {?(string|object)} filter A string to filter the animations to kill
* on their type (example: "camera"), or an
* object to filter on their target.
* @return {number} Returns the number of animations killed
* that way.
*/
sigma.misc.animation.killAll = function killAll(filter) {
let o;
let count = 0;
const type = typeof filter === "string" ? filter : null;
const target = typeof filter === "object" ? filter : null;
const { running } = sigma.misc.animation;
Object.keys(running).forEach(id => {
if (
(!type || running[id].type === type) &&
(!target || running[id].target === target)
) {
o = sigma.misc.animation.running[id];
cancelAnimationFrame(o.frameId);
delete sigma.misc.animation.running[id];
if (o.type === "camera") o.target.isAnimated = false;
// Increment counter:
count++;
// Check callbacks:
if (typeof (o.options || {}).onComplete === "function")
o.options.onComplete();
}
});
return count;
};
/**
* Returns "true" if any animation that is currently still running matches
* the filter given to the function.
*
* @param {string|object} filter A string to filter the animations to kill
* on their type (example: "camera"), or an
* object to filter on their target.
* @return {boolean} Returns true if any running animation
* matches.
*/
sigma.misc.animation.has = function has(filter) {
const type = typeof filter === "string" ? filter : null;
const target = typeof filter === "object" ? filter : null;
const { running } = sigma.misc.animation;
return Object.keys(running).some(id => {
return (
(!type || running[id].type === type) &&
(!target || running[id].target === target)
);
});
};
}
|