Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/image-editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- Add `applyImageEdit( state, edit, options? )`, which turns an `ImageEdit` into an editor state, fitting the crop to the image as the editor does unless `strict` is set ([#86665](https://github.com/WordPress/gutenberg/pull/86665)).

### Bug Fixes

- `getImageEdit` and `getOutputSize` return a value within a millionth of a whole number as that number, instead of floating-point noise such as `1439.9999999999998` ([#87376](https://github.com/WordPress/gutenberg/pull/87376)).

### Internal

- Update Storybook development dependencies to 10.6.8 ([#87255](https://github.com/WordPress/gutenberg/pull/87255)).
Expand Down
4 changes: 2 additions & 2 deletions packages/image-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ Whether two states have the same public edit fields, ignoring tiny floating-poin

### `getOutputSize( state )`

The dimensions that the edit from `getImageEdit` would produce on the image in `state.image`. A nearly full crop that `getImageEdit` omits gives the full image dimensions. Use this for a size readout or a minimum-size check against the loaded image. A processor that edits a different file or rounds pixels may produce different dimensions. Returns `null` when no image has loaded.
The dimensions that the edit from `getImageEdit` would produce on the image in `state.image`. A nearly full crop that `getImageEdit` omits gives the full image dimensions. Use this for a size readout or a minimum-size check against the loaded image. A processor that edits a different file or rounds pixels may produce different dimensions. A dimension within a millionth of a pixel of a whole number is returned as that number, so nine tenths of a 1600 pixel wide image is 1440, not 1439.9999999999998. Returns `null` when no image has loaded.

### `getImageEdit( state )`

Use it when saving. The editor stores where the image sits, how far it is zoomed, and a crop area measured against the view. `getImageEdit` describes the same result against the full-size image loaded in `state.image`, which may itself be a file someone edited before.

The edit has an optional `flip`, `rotation` and `crop`. Apply them in that order; anything absent needs nothing done. With exactly one axis flipped, `rotation` is 360 minus `state.rotation`, because the flip comes first. The crop is given as fractions of the image after rotation. An edit with no parts means there is nothing to apply, and `null` means no image has loaded. A locked ratio can produce a crop before the person edits anything.
The edit has an optional `flip`, `rotation` and `crop`. Apply them in that order; anything absent needs nothing done. With exactly one axis flipped, `rotation` is 360 minus `state.rotation`, because the flip comes first. The crop is given as fractions of the image after rotation; a fraction within a millionth of 0 or 1 is returned as 0 or 1. An edit with no parts means there is nothing to apply, and `null` means no image has loaded. A locked ratio can produce a crop before the person edits anything.

### `mapImageEdit( edit, handlers )`

Expand Down
28 changes: 24 additions & 4 deletions packages/image-editor/src/get-image-edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ export interface ImageEdit {
*/
export const CROP_TOLERANCE = 0.001;

/**
* How close a value must be to a whole number to be read as that number.
* Working the edit back out of the state leaves floating-point noise, such
* as a crop edge at 7e-8 instead of 0 or a width of 1439.9999999999998
* pixels. A millionth, of the image or of a pixel, is far below anything
* a person can set, so a value that close to a whole number is that number.
*/
const WHOLE_NUMBER_TOLERANCE = 1e-6;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're using 1e-6 in a few places. Not for now, but if we could potentially consolidate them if we think it's a universal tolerance we want to use? E.g. there's also:

const STATE_EPSILON = 1e-6;

Not a big deal to have them separate if you like the clarity of differently named constants, though!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, it's in a few places. I guess it's common enough to warrant centralization. Thanks for spotting. I'll do a follow up if I can come up with a decent enough const name 😄


/**
* `value`, or the whole number it is within `WHOLE_NUMBER_TOLERANCE` of.
*
* @param value A fraction of the image, or a size in pixels.
* @return The value without floating-point noise next to a whole number.
*/
export function snapToWholeNumber( value: number ): number {
const whole = Math.round( value );
return Math.abs( value - whole ) < WHOLE_NUMBER_TOLERANCE ? whole : value;
}

/**
* The person's edit, described against the image loaded in `state.image`.
*
Expand Down Expand Up @@ -128,10 +148,10 @@ export function getImageEdit( state: ImageEditorState ): ImageEdit | null {
const fullY = snapY + offsetY;

// Fractions of the full AABB — the crop frame after rotate.
const x = fullX / fullW;
const y = fullY / fullH;
const width = widthPx / fullW;
const height = heightPx / fullH;
const x = snapToWholeNumber( fullX / fullW );
const y = snapToWholeNumber( fullY / fullH );
const width = snapToWholeNumber( widthPx / fullW );
const height = snapToWholeNumber( heightPx / fullH );

const coversFullFrame =
x <= CROP_TOLERANCE &&
Expand Down
11 changes: 7 additions & 4 deletions packages/image-editor/src/get-output-size.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ImageEditorState, Size } from './core/types';
import { getRotatedBBox } from './core/camera';
import { getImageEdit } from './get-image-edit';
import { getImageEdit, snapToWholeNumber } from './get-image-edit';

/**
* The dimensions that the edit from `getImageEdit` would produce on the
Expand Down Expand Up @@ -33,10 +33,13 @@ export function getOutputSize( state: ImageEditorState ): Size | null {
edit.rotation ?? 0
);
if ( ! edit.crop ) {
return { width: rotated.width, height: rotated.height };
return {
width: snapToWholeNumber( rotated.width ),
height: snapToWholeNumber( rotated.height ),
};
}
return {
width: edit.crop.width * rotated.width,
height: edit.crop.height * rotated.height,
width: snapToWholeNumber( edit.crop.width * rotated.width ),
height: snapToWholeNumber( edit.crop.height * rotated.height ),
};
}
57 changes: 51 additions & 6 deletions packages/image-editor/src/test/get-image-edit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,11 @@ describe( 'getOutputSize agrees with getImageEdit', () => {

it( 'swaps the sides on a quarter turn', () => {
const state = makeState( { image: BIG, rotation: 90 } );
const size = getOutputSize( state );

expect( size?.width ).toBeCloseTo( 10000, 6 );
expect( size?.height ).toBeCloseTo( 20000, 6 );
expect( getOutputSize( state ) ).toEqual( {
width: 10000,
height: 20000,
} );
} );

it( 'takes the crop out of the image at its rotation', () => {
Expand All @@ -406,10 +407,54 @@ describe( 'getOutputSize agrees with getImageEdit', () => {
rotation: 90,
cropRect: { x: 0.25, y: 0, width: 0.5, height: 1 },
} );
const size = getOutputSize( state );

expect( size?.width ).toBeCloseTo( 5000, 6 );
expect( size?.height ).toBeCloseTo( 20000, 6 );
expect( getOutputSize( state ) ).toEqual( {
width: 5000,
height: 20000,
} );
} );
} );

// The pan and zoom that `applyImageEdit` works out leave floating-point
// noise, such as a width of 1439.9999999999998 pixels.
describe( 'reading an edit back removes floating-point noise', () => {
function stateOf( edit: ImageEdit ) {
const result = applyImageEdit( makeState(), edit );
if ( ! ( 'state' in result ) ) {
throw new Error( `Rejected: ${ result.rejected }` );
}
return result.state;
}

it( 'reads a crop of nine tenths as whole pixels', () => {
const state = stateOf( {
crop: { x: 0.05, y: 0.05, width: 0.9, height: 0.9 },
} );

expect( getOutputSize( state ) ).toEqual( {
width: 1440,
height: 810,
} );
} );

it( 'reads a crop on the edge of the image as starting at 0', () => {
const state = stateOf( {
crop: { x: 0, y: 0, width: 0.5, height: 1 },
} );

expect( getImageEdit( state )?.crop ).toEqual( {
x: 0,
y: 0,
width: 0.5,
height: 1,
} );
} );

it( 'keeps a size that is not near whole pixels', () => {
const size = getOutputSize( stateOf( { rotation: 5 } ) );

expect( size?.width ).toBeCloseTo( 1389.9286, 3 );
expect( Number.isInteger( size?.width ) ).toBe( false );
} );
} );

Expand Down
Loading