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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | /* * Copyright (c) 2022-2023 Braun Nathanael * * This project is dual licensed under one of the following licenses: * - Creative Commons Attribution-NoDerivatives 4.0 International License. * - GNU AFFERO GENERAL PUBLIC LICENSE Version 3 * * You should have received a copy of theses licenses along with this work. * If not, see <http://creativecommons.org/licenses/by-nd/4.0/> or <http://www.gnu.org/licenses/agpl-3.0.txt>. */ import is from "is"; /** * Utility functions for manipulating tween descriptor arrays before passing them * to `addScrollableAnim()` or `pushAnim()`. All functions are pure — they return * new arrays and never mutate the input. This makes them safe to compose: * e.g. `offset(scale(reverse(items), 1000), 200)` */ export const re_cssValueWithUnit = new RegExp( "([+-]?(?:[0-9]*[.])?[0-9]+)\\s*(" + ['box', 'bz', 'bh', 'bw', 'em', 'ex', '%', 'px', 'cm', 'mm', 'in', 'pt', 'pc', 'ch', 'rem', 'vh', 'vw', 'vmin', 'vmax'].join('|') + ")" ); /** * add any css val with it unit ( todo: optims&use objects for multi unit * @param val1 * @param val2 * @returns {Array} */ export function cssAdd( val1, val2, ...argz ) { if ( !is.array(val1) ) val1 = [val1]; if ( !is.array(val2) ) val2 = [val2]; let units1 = val1.map(v => (v && v.match && v.match(re_cssValueWithUnit) || [, v || 0, 'px'])), units2 = val2.map(v => (v && v.match && v.match(re_cssValueWithUnit) || [, v || 0, 'px'])), remap = {}, result = [], i; i = 0; while ( i < units1.length ) { remap[units1[i][2]] = remap[units1[i][2]] || 0; remap[units1[i][2]] += parseFloat(units1[i][1]); i++; } i = 0; while ( i < units2.length ) { remap[units2[i][2]] = remap[units2[i][2]] || 0; remap[units2[i][2]] += parseFloat(units2[i][1]); i++; } Object.keys(remap) .forEach( unit => (result.push(remap[unit] + unit)) ); return argz.length ? cssAdd(result, ...argz) : result; } /** * Multiply any css val with it unit ( todo: optims & use objects for multi unit * @param val1 * @param val2 * @returns {Array} */ export function cssMult( val1, val ) { if ( !is.array(val1) ) val1 = [val1]; let units1 = val1.map(v => (v && v.match && v.match(re_cssValueWithUnit) || [, v || 0, 'px'])), remap = {}, result = [], i; i = 0; while ( i < units1.length ) { remap[units1[i][2]] = remap[units1[i][2]] || 1; remap[units1[i][2]] = parseFloat(units1[i][1]) * val; i++; } Object.keys(remap) .forEach( unit => (result.push(remap[unit] + unit)) ); return result; } /** * Shift all tween `from` positions by `start` ms. Used to concatenate multiple * tween arrays end-to-end when composing complex timelines. */ export function offset( items, start = 0 ) { items = is.array(items) ? items : items && [items] || items; return items.map( item => ( { ...item, from: item.from + start } ) ) } /** * Proportionally rescale all tween `from` and `duration` values so the entire * timeline fits exactly within `duration` ms. Useful for normalising imported * animations to a specific scroll range. */ export function scale( items, duration = 0, withOffset ) { items = is.array(items) ? items : items && [items] || items; // get items current duration let iDuration = 0; items.forEach( item => { iDuration = Math.max(iDuration, item.from + item.duration) } ) items = items.map( item => ( { ...item, from : (item.from / iDuration) * duration, duration: (item.duration / iDuration) * duration } ) ) return withOffset ? offset(items, withOffset) : items } function inverseValues( v ) { if ( is.number(v) ) return -v; if ( is.object(v) ) return Object.keys(v).reduce(( h, key ) => (h[key] = inverseValues(v[key]), h), {}); if ( is.array(v) ) return v.map(( item ) => (inverseValues(item))); let values = v.split(/(\-?\d+(?:\.\d+)?|\-?\.\d+)/ig); return values.map( ( val, i ) => (i % 2 ? -parseFloat(val) : val) ).join(""); } /** * Mirror a timeline so it plays backward. Each descriptor's `from` is remapped to * `totalDuration - (from + duration)` and all numeric values in `apply` are negated * via `inverseValues`, so the end state of the forward animation becomes the start * state of the reversed one. */ export function reverse( items ) { items = is.array(items) ? items : items && [items] || items; // get items current duration let iDuration = 0; items.forEach( item => { iDuration = Math.max(iDuration, item.from + item.duration) } ) return items.map( item => { item = { ...item, from: iDuration - (item.from + item.duration), ...(item.apply ? { apply: inverseValues(item.apply) } : undefined) }; return item; } ) } const MultiCssProps = { "transform": true, "filter": true, "textShadow": true, "boxShadow": true } /** * Merge CSS descriptor objects by accumulating values rather than overwriting. * Used internally by `deMuxLine` to build the `allPropsById` aggregate. * * Special cases: * - Multi-value CSS properties (transform, filter, boxShadow, textShadow) are * treated as arrays and concatenated — preserving separate layer objects. * - Array values (multi-unit CSS like `["50%", "10vw"]`) are merged by unit. * - Nested objects (e.g. individual transform layer objects) are merged recursively. */ export function addCss( target, ...sources ) { let source = sources.shift(); for ( const key in source ) { if ( !source.hasOwnProperty(key) ) continue; if ( MultiCssProps[key] ) { if ( !target[key] ) { target[key] = []; } if ( !is.array(source[key]) ) { addCss(target[key], [source[key]]); } else addCss(target[key], source[key]); } else { // adding units if ( is.array(source[key]) ) { if ( !target[key] ) { target[key] = []; } if ( !is.array(target[key]) ) { target[key] = [...source[key], target[key]]; } else target[key].push(...source[key]); } else { if ( !target[key] ) { if ( !is.object(source[key]) ) { target[key] = source[key]; } else { target[key] = { ...source[key] }; } } else if ( is.object(target[key]) && is.object(source[key]) ) { addCss(target[key], source[key]); } else { //else target[key].push(...source[key]); target[key] = addAllType(target[key], source[key]) //console.log(key, target[key]) } } } } return sources.length && addCss(target, ...sources) || target; } function addAllType( v1, v2 ) { if ( !v1 ) return v2; if ( !v2 ) return v1; let values1 = ('' + v1).split(/(\-?\d+(?:\.\d+)?|\-?\.\d+)/ig), values2 = ('' + v2).split(/(\-?\d+(?:\.\d+)?|\-?\.\d+)/ig), r = values1.map( ( val, i ) => (i % 2 ? parseFloat(val) + parseFloat(values2[i] || 0) : val) ).filter(i => (i !== '')); return r.length === 1 ? parseInt(r[0]) : r.join("") } export function extractCss( items, inverse ) { let css = {}; items = is.array(items) ? items : items && [items] || items; items.forEach( item => { addCss(css, item.apply) } ); if ( inverse ) css = inverseValues(css); //if ( inverse && css.hasOwnProperty('opacity') ) // css.opacity -= 1; return css; } export function target( items, target ) { items = is.array(items) ? items : items && [items] || items; return items.map( item => ( { ...item, target } ) ) } /** * Prepend `shift` empty transform layer objects `{}` to the beginning of each * descriptor's transform array. This reserves layer slots so that an additive * animation can target a specific layer index without colliding with existing * transforms defined by other animations on the same node. * * Example: a base animation uses layer 0 (translateX); a hover overlay uses * `shiftTransforms(hoverItems, 1)` so it targets layer 1 instead. */ export function shiftTransforms( items, shift = 1 ) { items = is.array(items) ? items : items && [items] || items; return items.map( item => { let t = item.apply && item.apply.transform; if ( t ) { t = is.array(t) ? [...t] : [t]; for ( let i = 0; i < shift; i++ ) t.unshift({}); item = { ...item, apply: { ...item.apply, transform: t } }; } return item; } ) } |