transform-image.spec.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { Dimensions, Point, resizeToFocalPoint } from './transform-image';
  2. describe('resizeToFocalPoint', () => {
  3. it('no resize, crop left', () => {
  4. const original: Dimensions = { w: 200, h: 100 };
  5. const target: Dimensions = { w: 100, h: 100 };
  6. const focalPoint: Point = { x: 50, y: 50 };
  7. const result = resizeToFocalPoint(original, target, focalPoint);
  8. expect(result.width).toBe(200);
  9. expect(result.height).toBe(100);
  10. expect(result.region).toEqual({
  11. left: 0,
  12. top: 0,
  13. width: 100,
  14. height: 100,
  15. });
  16. });
  17. it('no resize, crop top left', () => {
  18. const original: Dimensions = { w: 200, h: 100 };
  19. const target: Dimensions = { w: 100, h: 100 };
  20. const focalPoint: Point = { x: 0, y: 0 };
  21. const result = resizeToFocalPoint(original, target, focalPoint);
  22. expect(result.width).toBe(200);
  23. expect(result.height).toBe(100);
  24. expect(result.region).toEqual({
  25. left: 0,
  26. top: 0,
  27. width: 100,
  28. height: 100,
  29. });
  30. });
  31. it('no resize, crop center', () => {
  32. const original: Dimensions = { w: 200, h: 100 };
  33. const target: Dimensions = { w: 100, h: 100 };
  34. const focalPoint: Point = { x: 100, y: 50 };
  35. const result = resizeToFocalPoint(original, target, focalPoint);
  36. expect(result.width).toBe(200);
  37. expect(result.height).toBe(100);
  38. expect(result.region).toEqual({
  39. left: 50,
  40. top: 0,
  41. width: 100,
  42. height: 100,
  43. });
  44. });
  45. it('crop with resize', () => {
  46. const original: Dimensions = { w: 200, h: 100 };
  47. const target: Dimensions = { w: 25, h: 50 };
  48. const focalPoint: Point = { x: 50, y: 50 };
  49. const result = resizeToFocalPoint(original, target, focalPoint);
  50. expect(result.width).toBe(100);
  51. expect(result.height).toBe(50);
  52. expect(result.region).toEqual({
  53. left: 13,
  54. top: 0,
  55. width: 25,
  56. height: 50,
  57. });
  58. });
  59. });