Skip to content

Commit

Permalink
Ensure toolbar stays visible for large images in ImageEditor (#10037)
Browse files Browse the repository at this point in the history
* recalculate canvas dimensions for images larger than max_height

* add changeset

* tweak default height logic

* tweak height logic

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
hannahblair and gradio-pr-bot authored Nov 26, 2024
1 parent a95fda1 commit d0b74ba
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/honest-pianos-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@gradio/imageeditor": patch
"gradio": patch
---

fix:Ensure toolbar stays visible for large images in ImageEditor
1 change: 1 addition & 0 deletions js/imageeditor/shared/InteractiveImageEditor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@
bind:bg
bind:active_mode
background_file={value?.background || value?.composite || null}
max_height={height}
></Sources>

{#if transforms.includes("crop")}
Expand Down
4 changes: 3 additions & 1 deletion js/imageeditor/shared/tools/Sources.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
export let upload: Client["upload"];
export let stream_handler: Client["stream"];
export let dragging: boolean;
export let max_height: number;
const { active_tool } = getContext<ToolContext>(TOOL_KEY);
const { pixi, dimensions, register_context, reset, editor_box } =
Expand Down Expand Up @@ -108,7 +109,8 @@
$pixi.background_container,
$pixi.renderer,
background,
$pixi.resize
$pixi.resize,
max_height
);
$dimensions = await add_image.start();
Expand Down
41 changes: 39 additions & 2 deletions js/imageeditor/shared/tools/sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,53 @@ export function add_bg_image(
container: Container,
renderer: IRenderer,
background: Blob | File,
resize: (width: number, height: number) => void
resize: (width: number, height: number) => void,
max_height = 450
): BgImageCommand {
let sprite: Sprite & DisplayObject;

function calculate_dimensions(
width: number,
height: number,
max_height: number
): [number, number] {
const MAX_HEIGHT = max_height * 1.5;
const MAX_WIDTH = MAX_HEIGHT * 2;

let new_width = width;
let new_height = height;

if (height > MAX_HEIGHT) {
const ratio = MAX_HEIGHT / height;
new_width = width * ratio;
new_height = MAX_HEIGHT;
}

if (new_width > MAX_WIDTH) {
const ratio = MAX_WIDTH / new_width;
new_width = MAX_WIDTH;
new_height = new_height * ratio;
}

return [Math.round(new_width), Math.round(new_height)];
}

return {
async start() {
container.removeChildren();
const img = await createImageBitmap(background);
const bitmap_texture = Texture.from(img);
sprite = new Sprite(bitmap_texture) as Sprite & DisplayObject;
return [sprite.width, sprite.height];

const [new_width, new_height] = calculate_dimensions(
sprite.width,
sprite.height,
max_height
);
sprite.width = new_width;
sprite.height = new_height;

return [new_width, new_height];
},
async execute() {
// renderer.resize(sprite.width, sprite.height);
Expand Down

0 comments on commit d0b74ba

Please sign in to comment.