react-native-zoom-toolkit
Version:
Smoothly zoom any image, video or component you want!
65 lines (62 loc) • 2.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.crop = void 0;
var _reactNativeReanimated = require("react-native-reanimated");
const flipVector = vector => {
return {
width: vector.height,
height: vector.width
};
};
const crop = options => {
'worklet';
const {
cropSize,
canvas,
resolution,
position,
scale,
fixedWidth,
context
} = options;
const isFlipped = context.rotationAngle % 180 !== 0;
const currentCanvas = isFlipped ? flipVector(canvas) : canvas;
const currentResolution = isFlipped ? flipVector(resolution) : resolution;
const offsetX = (currentCanvas.width * scale - cropSize.width) / 2;
const offsetY = (currentCanvas.height * scale - cropSize.height) / 2;
const normalizedX = Math.abs(offsetX) + position.x;
const normalizedY = Math.abs(offsetY) + position.y;
const relativeX = cropSize.width / (currentCanvas.width * scale);
const relativeY = cropSize.height / (currentCanvas.height * scale);
const posX = (0, _reactNativeReanimated.interpolate)(normalizedX, [0, 2 * offsetX], [1 - relativeX, 0]);
const posY = (0, _reactNativeReanimated.interpolate)(normalizedY, [0, 2 * offsetY], [1 - relativeY, 0]);
const x = currentResolution.width * posX;
const y = currentResolution.height * posY;
const width = currentResolution.width * relativeX;
const height = currentResolution.height * relativeY;
let resize;
// Make a normal crop, if the fixedWidth is defined just resize everything to meet the ratio
// between fixedWidth and the width of the crop.
let sizeModifier = 1;
if (fixedWidth !== undefined) {
sizeModifier = fixedWidth / width;
resize = {
width: Math.ceil(resolution.width * sizeModifier),
height: Math.ceil(resolution.height * sizeModifier)
};
}
return {
crop: {
originX: x * sizeModifier,
originY: y * sizeModifier,
width: Math.round(width * sizeModifier),
height: Math.round(height * sizeModifier)
},
context,
resize
};
};
exports.crop = crop;
//# sourceMappingURL=crop.js.map