Skip to content

Commit 258fa42

Browse files
authored
[ts-sdk] Add width, height to Image component (#1126)
1 parent 30e25fd commit 258fa42

File tree

7 files changed

+41
-64
lines changed

7 files changed

+41
-64
lines changed

ts/examples/node-examples/src/dynamic-text.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function ExampleApp() {
4949
<PartialText text="Example partial text that transition in 1 second" transitionMs={1_000} />
5050
<PartialText text="Example partial text that transition in 2 second" transitionMs={2_000} />
5151
<PartialText text="Example partial text that transition in 5 second" transitionMs={5_000} />
52-
<Image imageId="image_1" />
52+
<Image imageId="image_1" style={{ width: 600 }} />
5353
</View>
5454
);
5555
}

ts/examples/node-examples/src/web-view.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ async function run() {
3636
await smelter.registerImage('logo', {
3737
assetType: 'svg',
3838
url: 'https://smelter.dev/images/smelter-logo.svg',
39-
resolution: { width: 800, height: 200 },
4039
});
4140
await smelter.registerWebRenderer(WEBSITE_INSTANCE, {
4241
url: `file://${path.join(__dirname, './web-view.html')}`,

ts/smelter-core/src/api/renderer.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,10 @@ export function intoRegisterImage(image: Renderers.RegisterImage): Api.ImageSpec
66
url: image.url,
77
path: image.serverPath,
88
};
9-
if (image.assetType === 'svg') {
10-
return {
11-
asset_type: 'svg',
12-
resolution: image.resolution,
13-
...source,
14-
};
15-
} else {
16-
return {
17-
asset_type: image.assetType,
18-
...source,
19-
};
20-
}
9+
return {
10+
asset_type: image.assetType,
11+
...source,
12+
};
2113
}
2214

2315
export function intoRegisterWebRenderer(

ts/smelter-web-wasm/src/compositor/api.ts

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,10 @@
11
import type { Output } from '@swmansion/smelter-core';
22
import type { Api, Renderers } from '@swmansion/smelter';
33

4-
export type RegisterImage =
5-
| {
6-
assetType: 'png';
7-
url: string;
8-
}
9-
| {
10-
assetType: 'jpeg';
11-
url: string;
12-
}
13-
| {
14-
assetType: 'svg';
15-
url: string;
16-
resolution?: Api.Resolution;
17-
}
18-
| {
19-
assetType: 'gif';
20-
url: string;
21-
}
22-
| {
23-
assetType: 'auto';
24-
url: string;
25-
};
4+
export type RegisterImage = {
5+
assetType: 'png' | 'jpeg' | 'svg' | 'gif' | 'auto';
6+
url: string;
7+
};
268

279
export type RegisterShader = Renderers.RegisterShader;
2810

ts/smelter/src/api.generated.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,14 @@ export type Component =
432432
* Id of an image. It identifies an image registered using a [`register image`](../routes.md#register-image) request.
433433
*/
434434
image_id: RendererId;
435+
/**
436+
* Width of the image in pixels. If `height` is not explicitly provided, the image will automatically adjust its height to maintain its original aspect ratio relative to the width.
437+
*/
438+
width?: number | null;
439+
/**
440+
* Height of the image in pixels. If `width` is not explicitly provided, the image will automatically adjust its width to maintain its original aspect ratio relative to the height.
441+
*/
442+
height?: number | null;
435443
}
436444
| {
437445
type: "text";

ts/smelter/src/components/Image.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,23 @@ export type ImageProps = Omit<ComponentBaseProps, 'children'> &
1919
source: string;
2020
imageId?: never; // Ensuring 'imageId' cannot be used alongside 'source'
2121
}
22-
);
22+
) & {
23+
/**
24+
* Component styling properties.
25+
*/
26+
style?: ImageStyleProps;
27+
};
28+
29+
type ImageStyleProps = {
30+
/**
31+
* Width of the image in pixels. If `height` is not explicitly provided, the image will automatically adjust its height to maintain its original aspect ratio relative to the width.
32+
*/
33+
width?: number;
34+
/**
35+
* Height of the image in pixels. If `width` is not explicitly provided, the image will automatically adjust its width to maintain its original aspect ratio relative to the height.
36+
*/
37+
height?: number;
38+
};
2339

2440
type ImageSceneBuliderProps = Omit<ImageProps, 'imageId'> & { imageId: string };
2541

@@ -105,6 +121,8 @@ function sceneBuilder(props: ImageSceneBuliderProps, _children: SceneComponent[]
105121
type: 'image',
106122
id: props.id,
107123
image_id: props.imageId,
124+
width: props.style?.width,
125+
height: props.style?.height,
108126
};
109127
}
110128

ts/smelter/src/types/resource.ts

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,11 @@ import type * as Api from '../api.js';
22

33
export type RegisterShader = Api.ShaderSpec;
44

5-
export type RegisterImage =
6-
| {
7-
assetType: 'png';
8-
url?: string;
9-
serverPath?: string;
10-
}
11-
| {
12-
assetType: 'jpeg';
13-
url?: string;
14-
serverPath?: string;
15-
}
16-
| {
17-
assetType: 'svg';
18-
url?: string;
19-
serverPath?: string;
20-
resolution?: Api.Resolution;
21-
}
22-
| {
23-
assetType: 'gif';
24-
url?: string;
25-
serverPath?: string;
26-
}
27-
| {
28-
assetType: 'auto';
29-
url?: string;
30-
serverPath?: string;
31-
};
5+
export type RegisterImage = {
6+
assetType: 'png' | 'jpeg' | 'svg' | 'gif' | 'auto';
7+
url?: string;
8+
serverPath?: string;
9+
};
3210

3311
export type ImageAssetType = RegisterImage['assetType'];
3412

0 commit comments

Comments
 (0)