Skip to content

Commit d3f36ac

Browse files
committed
Refactor image mode constants to Modes object
Replaces individual static mode constants with a single static Modes object on the Image class for better organization and Figma-inspired API. Updates all references and type definitions to use Image.Modes. Also sets renderer type to 'image' in constructor and toObject method.
1 parent 1cb988f commit d3f36ac

File tree

2 files changed

+58
-56
lines changed

2 files changed

+58
-56
lines changed

src/effects/image.js

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ export class Image extends Rectangle {
4747
constructor(path, ox, oy, width, height, mode) {
4848
super(ox, oy, width || 1, height || 1);
4949

50+
this._renderer.type = 'image';
51+
5052
for (let prop in proto) {
5153
Object.defineProperty(this, prop, proto[prop]);
5254
}
@@ -72,34 +74,21 @@ export class Image extends Rectangle {
7274
}
7375

7476
/**
75-
* @name Two.Image.fill
76-
* @property {String} - Scale image to fill the bounds while preserving aspect ratio.
77-
*/
78-
static fill = 'fill';
79-
80-
/**
81-
* @name Two.Image.fit
82-
* @property {String} - Scale image to fit within bounds while preserving aspect ratio.
83-
*/
84-
static fit = 'fit';
85-
86-
/**
87-
* @name Two.Image.crop
88-
* @property {String} - Scale image to fill bounds while preserving aspect ratio, cropping excess.
89-
*/
90-
static crop = 'crop';
91-
92-
/**
93-
* @name Two.Image.tile
94-
* @property {String} - Repeat image at original size to fill the bounds.
95-
*/
96-
static tile = 'tile';
97-
98-
/**
99-
* @name Two.Image.stretch
100-
* @property {String} - Stretch image to fill dimensions, ignoring aspect ratio.
77+
* @name Two.Image.Modes
78+
* @property {Object} mode - Different mode types to render an image inspired by Figma.
79+
* @property {String} mode.fill - Scale image to fill the bounds while preserving aspect ratio.
80+
* @property {String} mode.fit - Scale image to fit within bounds while preserving aspect ratio.
81+
* @property {String} mode.crop - Scale image to fill bounds while preserving aspect ratio, cropping excess.
82+
* @property {String} mode.tile - Repeat image at original size to fill the bounds.
83+
* @property {String} mode.stretch - Stretch image to fill dimensions, ignoring aspect ratio.
10184
*/
102-
static stretch = 'stretch';
85+
static Modes = {
86+
fill: 'fill',
87+
fit: 'fit',
88+
crop: 'crop',
89+
tile: 'tile',
90+
stretch: 'stretch',
91+
};
10392

10493
/**
10594
* @name Two.Image.Properties
@@ -175,6 +164,7 @@ export class Image extends Rectangle {
175164
*/
176165
toObject() {
177166
const object = super.toObject.call(this);
167+
object.renderer.type = 'image';
178168
object.texture = this.texture.toObject();
179169
object.mode = this.mode;
180170
return object;
@@ -231,7 +221,7 @@ export class Image extends Rectangle {
231221

232222
// Apply scaling based on mode
233223
switch (this._mode) {
234-
case Image.fill: {
224+
case Image.Modes.fill: {
235225
// Fill within bounds while preserving aspect ratio
236226
const scale = Math.max(scaleX, scaleY);
237227
effect.scale = scale;
@@ -241,7 +231,7 @@ export class Image extends Rectangle {
241231
break;
242232
}
243233

244-
case Image.fit: {
234+
case Image.Modes.fit: {
245235
// Fit within bounds while preserving aspect ratio
246236
const scale = Math.min(scaleX, scaleY);
247237
effect.scale = scale; // TODO: For SVG this works `new Vector(scaleX, scaleY);`
@@ -251,21 +241,21 @@ export class Image extends Rectangle {
251241
break;
252242
}
253243

254-
case Image.crop: {
244+
case Image.Modes.crop: {
255245
// Intentionally left blank to allow
256246
// external developer to control
257247
break;
258248
}
259249

260-
case Image.tile: {
250+
case Image.Modes.tile: {
261251
// Repeat image and align it correctly
262252
effect.offset.x = (iw - rw) / 2;
263253
effect.offset.y = (ih - rh) / 2;
264254
effect.repeat = 'repeat';
265255
break;
266256
}
267257

268-
case Image.stretch:
258+
case Image.Modes.stretch:
269259
default: {
270260
// Stretch the image texture to whatever the dimensions of the rect are
271261
effect.scale = new Vector(scaleX, scaleY);

types.d.ts

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3323,7 +3323,7 @@ declare module 'two.js/src/shapes/rectangle' {
33233323
import { Group } from 'two.js/src/group';
33243324
}
33253325
declare module 'two.js/src/effects/image' {
3326-
type ModeProperties = 'fill' | 'fit' | 'crop' | 'tile' | 'stretch';
3326+
export type ModeProperties = 'fill' | 'fit' | 'crop' | 'tile' | 'stretch';
33273327
/**
33283328
* @name Two.Image
33293329
* @class
@@ -3337,31 +3337,43 @@ declare module 'two.js/src/effects/image' {
33373337
*/
33383338
export class Image extends Rectangle {
33393339
/**
3340-
* @name Two.Image.fill
3341-
* @property {String} - Scale image to fill the bounds while preserving aspect ratio.
3342-
*/
3343-
static fill: 'fill';
3344-
/**
3345-
* @name Two.Image.fit
3346-
* @property {String} - Scale image to fit within bounds while preserving aspect ratio.
3347-
*/
3348-
static fit: 'fit';
3349-
/**
3350-
* @name Two.Image.crop
3351-
* @property {String} - Scale image to fill bounds while preserving aspect ratio, cropping excess.
3352-
*/
3353-
static crop: 'crop';
3354-
/**
3355-
* @name Two.Image.tile
3356-
* @property {String} - Repeat image at original size to fill the bounds.
3357-
*/
3358-
static tile: 'tile';
3340+
* @name Two.Image.Properties
3341+
* @property {String[]} - A list of properties that are on every {@link Two.Image}.
3342+
*/
3343+
static Properties: ('texture' | 'mode' | string)[];
3344+
/**
3345+
* @name Two.Image.Modes
3346+
* @property {Object} mode - Different mode types to render an image inspired by Figma.
3347+
* @property {String} mode.fill - Scale image to fill the bounds while preserving aspect ratio.
3348+
* @property {String} mode.fit - Scale image to fit within bounds while preserving aspect ratio.
3349+
* @property {String} mode.crop - Scale image to fill bounds while preserving aspect ratio, cropping excess.
3350+
* @property {String} mode.tile - Repeat image at original size to fill the bounds.
3351+
* @property {String} mode.stretch - Stretch image to fill dimensions, ignoring aspect ratio.
3352+
*/
3353+
static Modes: {
3354+
fill: 'fill';
3355+
fit: 'fit';
3356+
crop: 'crop';
3357+
tile: 'tile';
3358+
stretch: 'stretch';
3359+
};
3360+
33593361
/**
3360-
* @name Two.Image.stretch
3361-
* @property {String} - Stretch image to fill dimensions, ignoring aspect ratio.
3362+
* @name Two.Image.fromObject
3363+
* @function
3364+
* @param {Object} obj - Object notation of a {@link Two.Image} to create a new instance
3365+
* @returns {Two.Image}
3366+
* @description Create a new {@link Two.Image} from an object notation of a {@link Two.Image}.
3367+
* @nota-bene Works in conjunction with {@link Two.Image#toObject}
33623368
*/
3363-
static stretch: 'stretch';
3364-
3369+
static fromObject(
3370+
obj:
3371+
| object
3372+
| (Parameters<typeof Rectangle.fromObject> & {
3373+
texture?: Parameters<typeof Texture.fromObject>;
3374+
mode?: ModeProperties;
3375+
})
3376+
): Image;
33653377
constructor(
33663378
path?: string | Texture,
33673379
ox?: number,

0 commit comments

Comments
 (0)