All files / packages/core/src/RenderingEngine/helpers/cpuFallback/rendering correctShift.ts

0% Statements 0/14
0% Branches 0/6
0% Functions 0/1
0% Lines 0/12

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                                                                             
import { CPUFallbackViewport, Point2 } from '../../../../types';
 
type Shift = {
  x: number;
  y: number;
};
/**
 * Corrects the shift by accounting for viewport rotation and flips.
 *
 * @param shift - The shift to correct.
 * @param viewportOrientation - Object containing information on the viewport orientation.
 */
export default function (
  shift: Shift,
  viewportOrientation: CPUFallbackViewport
): Shift {
  const { hflip, vflip, rotation } = viewportOrientation;
 
  // Apply Flips
  shift.x *= hflip ? -1 : 1;
  shift.y *= vflip ? -1 : 1;
 
  // Apply rotations
  if (rotation !== 0) {
    const angle = (rotation * Math.PI) / 180;
 
    const cosA = Math.cos(angle);
    const sinA = Math.sin(angle);
 
    const newX = shift.x * cosA - shift.y * sinA;
    const newY = shift.x * sinA + shift.y * cosA;
 
    shift.x = newX;
    shift.y = newY;
  }
 
  return shift;
}