transform-image.spec.ts 2.1 KB

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