All files / src/projection/geometry delta-remove.ts

24.32% Statements 9/37
0% Branches 0/34
0% Functions 0/4
28.13% Lines 9/32

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 11237x 37x   37x           37x                                       37x                                                                                         37x                                         37x 37x           37x                  
import { mix } from "popmotion"
import { percent } from "style-value-types"
import { ResolvedValues } from "../../render/types"
import { scalePoint } from "./delta-apply"
import { Axis, Box } from "./types"
 
/**
 * Remove a delta from a point. This is essentially the steps of applyPointDelta in reverse
 */
export function removePointDelta(
    point: number,
    translate: number,
    scale: number,
    originPoint: number,
    boxScale?: number
): number {
    point -= translate
    point = scalePoint(point, 1 / scale, originPoint)
 
    if (boxScale !== undefined) {
        point = scalePoint(point, 1 / boxScale, originPoint)
    }
 
    return point
}
 
/**
 * Remove a delta from an axis. This is essentially the steps of applyAxisDelta in reverse
 */
export function removeAxisDelta(
    axis: Axis,
    translate: number | string = 0,
    scale: number = 1,
    origin: number = 0.5,
    boxScale?: number,
    originAxis: Axis = axis,
    sourceAxis: Axis = axis
): void {
    if (percent.test(translate)) {
        translate = parseFloat(translate as string)
        const relativeProgress = mix(
            sourceAxis.min,
            sourceAxis.max,
            translate / 100
        )
        translate = relativeProgress - sourceAxis.min
    }
 
    if (typeof translate !== "number") return
 
    let originPoint = mix(originAxis.min, originAxis.max, origin)
    if (axis === originAxis) originPoint -= translate
 
    axis.min = removePointDelta(
        axis.min,
        translate,
        scale,
        originPoint,
        boxScale
    )
 
    axis.max = removePointDelta(
        axis.max,
        translate,
        scale,
        originPoint,
        boxScale
    )
}
 
/**
 * Remove a transforms from an axis. This is essentially the steps of applyAxisTransforms in reverse
 * and acts as a bridge between motion values and removeAxisDelta
 */
export function removeAxisTransforms(
    axis: Axis,
    transforms: ResolvedValues,
    [key, scaleKey, originKey]: string[],
    origin?: Axis,
    sourceAxis?: Axis
) {
    removeAxisDelta(
        axis,
        transforms[key] as number,
        transforms[scaleKey] as number,
        transforms[originKey] as number,
        transforms.scale as number,
        origin,
        sourceAxis
    )
}
 
/**
 * The names of the motion values we want to apply as translation, scale and origin.
 */
const xKeys = ["x", "scaleX", "originX"]
const yKeys = ["y", "scaleY", "originY"]
 
/**
 * Remove a transforms from an box. This is essentially the steps of applyAxisBox in reverse
 * and acts as a bridge between motion values and removeAxisDelta
 */
export function removeBoxTransforms(
    box: Box,
    transforms: ResolvedValues,
    originBox?: Box,
    sourceBox?: Box
): void {
    removeAxisTransforms(box.x, transforms, xKeys, originBox?.x, sourceBox?.x)
    removeAxisTransforms(box.y, transforms, yKeys, originBox?.y, sourceBox?.y)
}