@/utils#clamp TypeScript Examples

The following examples show how to use @/utils#clamp. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FocusController.ts    From pixi-live2d-display with MIT License 6 votes vote down vote up
/**
     * Sets the focus position.
     * @param x - X position in range `[-1, 1]`.
     * @param y - Y position in range `[-1, 1]`.
     * @param instant - Should the focus position be instantly applied.
     */
    focus(x: number, y: number, instant: boolean = false) {
        this.targetX = clamp(x, -1, 1);
        this.targetY = clamp(y, -1, 1);

        if (instant) {
            this.x = this.targetX;
            this.y = this.targetY;
        }
    }
Example #2
Source File: Live2DEyeBlink.ts    From pixi-live2d-display with MIT License 5 votes vote down vote up
setEyeParams(value: number) {
        this.eyeParamValue = clamp(value, 0, 1);
        this.coreModel.setParamFloat(this.leftParam, this.eyeParamValue);
        this.coreModel.setParamFloat(this.rightParam, this.eyeParamValue);
    }
Example #3
Source File: Live2DPose.ts    From pixi-live2d-display with MIT License 5 votes vote down vote up
normalizePartsOpacityGroup(partsGroup: Live2DPartsParam[], dt: DOMHighResTimeStamp) {
        const model = this.coreModel;
        const phi = 0.5;
        const maxBackOpacity = 0.15;
        let visibleOpacity = 1;

        let visibleIndex = partsGroup.findIndex(
            ({ paramIndex, partsIndex }) => partsIndex >= 0 && model.getParamFloat(paramIndex) !== 0,
        );

        if (visibleIndex >= 0) {
            const originalOpacity = model.getPartsOpacity(partsGroup[visibleIndex]!.partsIndex);

            visibleOpacity = clamp(originalOpacity + dt / this.opacityAnimDuration, 0, 1);
        } else {
            visibleIndex = 0;
            visibleOpacity = 1;
        }

        partsGroup.forEach(({ partsIndex }, index) => {
            if (partsIndex >= 0) {
                if (visibleIndex == index) {
                    model.setPartsOpacity(partsIndex, visibleOpacity);
                } else {
                    let opacity = model.getPartsOpacity(partsIndex);

                    // I can't understand this part, so just leave it original
                    let a1;
                    if (visibleOpacity < phi) {
                        a1 = (visibleOpacity * (phi - 1)) / phi + 1;
                    } else {
                        a1 = ((1 - visibleOpacity) * phi) / (1 - phi);
                    }
                    let backOp = (1 - a1) * (1 - visibleOpacity);
                    if (backOp > maxBackOpacity) {
                        a1 = 1 - maxBackOpacity / (1 - visibleOpacity);
                    }
                    if (opacity > a1) {
                        opacity = a1;
                    }

                    model.setPartsOpacity(partsIndex, opacity);
                }
            }
        });
    }