Skip to content

Commit f8daf37

Browse files
committed
Add copy and clone methods to shape classes
Introduces `copy` and `clone` methods to Sprite, Circle, Ellipse, and RoundedRectangle classes for easier duplication and property transfer. Updates type definitions to reflect these additions and improves object serialization for Sprite. Also adds support for deserializing sprites in Group.fromObject.
1 parent d3f36ac commit f8daf37

File tree

3 files changed

+192
-42
lines changed

3 files changed

+192
-42
lines changed

src/effects/sprite.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ export class Sprite extends Rectangle {
151151
constructor(path, ox, oy, cols, rows, frameRate) {
152152
super(ox, oy, 0, 0);
153153

154+
this._renderer.type = 'sprite';
155+
154156
for (let prop in proto) {
155157
Object.defineProperty(this, prop, proto[prop]);
156158
}
@@ -351,6 +353,7 @@ export class Sprite extends Rectangle {
351353
*/
352354
toObject() {
353355
const object = super.toObject.call(this);
356+
object.renderer.type = 'sprite';
354357
object.texture = this.texture.toObject();
355358
object.columns = this.columns;
356359
object.rows = this.rows;
@@ -375,25 +378,25 @@ export class Sprite extends Rectangle {
375378
dispose() {
376379
// Call parent dispose for inherited cleanup (vertices, fill/stroke effects)
377380
super.dispose();
378-
381+
379382
// Stop animation if playing
380383
if (this._playing) {
381384
this._playing = false;
382385
}
383-
386+
384387
// Clear animation callbacks
385388
this._onLastFrame = null;
386-
389+
387390
// Reset timing properties
388391
this._startTime = 0;
389-
392+
390393
// Dispose texture (more thorough than unbind)
391394
if (this._texture && typeof this._texture.dispose === 'function') {
392395
this._texture.dispose();
393396
} else if (this._texture && typeof this._texture.unbind === 'function') {
394397
this._texture.unbind();
395398
}
396-
399+
397400
return this;
398401
}
399402

src/group.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ export class Group extends Shape {
321321
return Ellipse.fromObject(child);
322322
case 'image':
323323
return Image.fromObject(child);
324+
case 'sprite':
325+
return Image.fromObject(child);
324326
case 'points':
325327
return Points.fromObject(child);
326328
case 'polygon':

types.d.ts

Lines changed: 182 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3439,6 +3439,43 @@ declare module 'two.js/src/effects/sprite' {
34393439
* @description A convenient package to display still or animated images through a tiled image source. For more information on the principals of animated imagery through tiling see [Texture Atlas](https://en.wikipedia.org/wiki/Texture_atlas) on Wikipedia.
34403440
*/
34413441
export class Sprite extends Rectangle {
3442+
/**
3443+
* @name Two.Sprite.Properties
3444+
* @property {String[]} - A list of properties that are on every {@link Two.Sprite}.
3445+
*/
3446+
static Properties: (
3447+
| 'texture'
3448+
| 'columns'
3449+
| 'rows'
3450+
| 'frameRate'
3451+
| 'index'
3452+
| 'firstFrame'
3453+
| 'lastFrame'
3454+
| 'loop'
3455+
| string
3456+
)[];
3457+
/**
3458+
* @name Two.Sprite.fromObject
3459+
* @function
3460+
* @param {Object} obj - Object notation of a {@link Two.Sprite} to create a new instance
3461+
* @returns {Two.Sprite}
3462+
* @description Create a new {@link Two.Sprite} from an object notation of a {@link Two.Sprite}.
3463+
* @nota-bene Works in conjunction with {@link Two.Sprite#toObject}
3464+
*/
3465+
static fromObject(
3466+
obj:
3467+
| object
3468+
| (Parameters<typeof Rectangle.fromObject> & {
3469+
texture?: Parameters<typeof Texture.fromObject>;
3470+
columns?: number;
3471+
rows?: number;
3472+
frameRate?: number;
3473+
index?: number;
3474+
firstFrame?: number;
3475+
lastFrame?: number;
3476+
loop?: boolean;
3477+
})
3478+
): Sprite;
34423479
constructor(
34433480
path?: string | Texture,
34443481
ox?: number,
@@ -3558,6 +3595,13 @@ declare module 'two.js/src/effects/sprite' {
35583595
* @property {Number} - The index of the current tile of the sprite to display. Defaults to `0`.
35593596
*/
35603597
index: number;
3598+
/**
3599+
* @name Two.Sprite#copy
3600+
* @function
3601+
* @param {Two.Sprite} sprite - The reference {@link Two.Sprite}
3602+
* @description Copy the properties of one {@link Two.Sprite} onto another.
3603+
*/
3604+
copy(sprite: Sprite): Sprite;
35613605
/**
35623606
* @name Two.Sprite#play
35633607
* @function
@@ -3583,6 +3627,14 @@ declare module 'two.js/src/effects/sprite' {
35833627
* @description Halt animation playback of a {@link Two.Sprite} and set the current frame back to the first frame.
35843628
*/
35853629
stop(): Sprite;
3630+
/**
3631+
* @name Two.Sprite#clone
3632+
* @function
3633+
* @param {Two.Group} [parent] - The parent group or scene to add the clone to.
3634+
* @returns {Two.Sprite}
3635+
* @description Create a new instance of {@link Two.Sprite} with the same properties of the current sprite.
3636+
*/
3637+
clone(parent?: Group): Sprite;
35863638
/**
35873639
* @name Two.Sprite#dispose
35883640
* @function
@@ -3596,17 +3648,17 @@ declare module 'two.js/src/effects/sprite' {
35963648
}
35973649
import { Rectangle } from 'two.js/src/shapes/rectangle';
35983650
import { Texture } from 'two.js/src/effects/texture';
3651+
import { Group } from 'two.js/src/group';
35993652
}
36003653
declare module 'two.js/src/shapes/circle' {
36013654
/**
3602-
* @name Two.Circle
3603-
* @class
3604-
3605-
* @param {Number} [x=0] - The x position of the circle.
3606-
* @param {Number} [y=0] - The y position of the circle.
3607-
* @param {Number} [radius=0] - The radius value of the circle.
3608-
* @param {Number} [resolution=4] - The number of vertices used to construct the circle.
3609-
*/
3655+
* @name Two.Circle
3656+
* @class
3657+
* @param {Number} [x=0] - The x position of the circle.
3658+
* @param {Number} [y=0] - The y position of the circle.
3659+
* @param {Number} [radius=0] - The radius value of the circle.
3660+
* @param {Number} [resolution=4] - The number of vertices used to construct the circle.
3661+
*/
36103662
export class Circle extends Path {
36113663
/**
36123664
* @name Two.Circle#_flagRadius
@@ -3620,25 +3672,83 @@ declare module 'two.js/src/shapes/circle' {
36203672
* @see {@link Two.Circle#radius}
36213673
*/
36223674
private _radius;
3623-
static Properties: string[];
3675+
/**
3676+
* @name Two.Circle.Properties
3677+
* @property {String[]} - A list of properties that are on every {@link Two.Circle}.
3678+
*/
3679+
static Properties: ('radius' | string)[];
3680+
/**
3681+
* @name Two.Circle.fromObject
3682+
* @function
3683+
* @param {Object} obj - Object notation of a {@link Two.Circle} to create a new instance
3684+
* @returns {Two.Circle}
3685+
* @description Create a new {@link Two.Circle} from an object notation of a {@link Two.Circle}.
3686+
* @nota-bene Works in conjunction with {@link Two.Circle#toObject}
3687+
*/
3688+
static fromObject(
3689+
obj:
3690+
| object
3691+
| (Parameters<typeof Path.fromObject> & {
3692+
radius?: number;
3693+
})
3694+
): Circle;
36243695
constructor(x?: number, y?: number, radius?: number, resolution?: number);
3696+
/**
3697+
* @name Two.Circle#radius
3698+
* @property {Number} - The size of the radius of the circle.
3699+
*/
36253700
radius: number;
3701+
/**
3702+
* @name Two.Circle#copy
3703+
* @function
3704+
* @param {Two.Circle} circle - The reference {@link Two.Circle}
3705+
* @description Copy the properties of one {@link Two.Circle} onto another.
3706+
*/
3707+
copy(circle: Circle): Circle;
3708+
/**
3709+
* @name Two.Circle#clone
3710+
* @function
3711+
* @param {Two.Group} [parent] - The parent group or scene to add the clone to.
3712+
* @returns {Two.Circle}
3713+
* @description Create a new instance of {@link Two.Circle} with the same properties of the current path.
3714+
*/
3715+
clone(parent: Group): Circle;
36263716
}
36273717
import { Path } from 'two.js/src/path';
3718+
import { Group } from 'two.js/src/group';
36283719
}
36293720
declare module 'two.js/src/shapes/ellipse' {
36303721
/**
3631-
* @name Two.Ellipse
3632-
* @class
3633-
3634-
* @param {Number} [x=0] - The x position of the ellipse.
3635-
* @param {Number} [y=0] - The y position of the ellipse.
3636-
* @param {Number} [rx=0] - The radius value of the ellipse in the x direction.
3637-
* @param {Number} [ry=0] - The radius value of the ellipse in the y direction.
3638-
* @param {Number} [resolution=4] - The number of vertices used to construct the ellipse.
3639-
*/
3722+
* @name Two.Ellipse
3723+
* @class
3724+
* @param {Number} [x=0] - The x position of the ellipse.
3725+
* @param {Number} [y=0] - The y position of the ellipse.
3726+
* @param {Number} [rx=0] - The radius value of the ellipse in the x direction.
3727+
* @param {Number} [ry=0] - The radius value of the ellipse in the y direction.
3728+
* @param {Number} [resolution=4] - The number of vertices used to construct the ellipse.
3729+
*/
36403730
export class Ellipse extends Path {
3641-
static Properties: string[];
3731+
/**
3732+
* @name Two.Ellipse.Properties
3733+
* @property {String[]} - A list of properties that are on every {@link Two.Ellipse}.
3734+
*/
3735+
static Properties: ('width' | 'height' | string)[];
3736+
/**
3737+
* @name Two.Ellipse.fromObject
3738+
* @function
3739+
* @param {Object} obj - Object notation of a {@link Two.Ellipse} to create a new instance
3740+
* @returns {Two.Ellipse}
3741+
* @description Create a new {@link Two.Ellipse} from an object notation of a {@link Two.Ellipse}.
3742+
* @nota-bene Works in conjunction with {@link Two.Ellipse#toObject}
3743+
*/
3744+
fromObject(
3745+
obj:
3746+
| object
3747+
| (Parameters<typeof Path.fromObject> & {
3748+
width?: number;
3749+
height?: number;
3750+
})
3751+
): Ellipse;
36423752
constructor(
36433753
x?: number,
36443754
y?: number,
@@ -3672,6 +3782,13 @@ declare module 'two.js/src/shapes/ellipse' {
36723782
private _height;
36733783
width: number;
36743784
height: number;
3785+
/**
3786+
* @name Two.Ellipse#copy
3787+
* @function
3788+
* @param {Two.Ellipse} ellipse - The reference {@link Two.Ellipse}
3789+
* @description Copy the properties of one {@link Two.Ellipse} onto another.
3790+
*/
3791+
copy(ellipse: Ellipse): Ellipse;
36753792
}
36763793
import { Path } from 'two.js/src/path';
36773794
}
@@ -3686,7 +3803,7 @@ declare module 'two.js/src/shapes/line' {
36863803
* @param {Number} [y2=0] - The y position of the second vertex on the line.
36873804
*/
36883805
export class Line extends Path {
3689-
static Properties: string[];
3806+
static Properties: ('left' | 'right' | string)[];
36903807
constructor(x1?: number, y1?: number, x2?: number, y2?: number);
36913808

36923809
/**
@@ -3717,7 +3834,28 @@ declare module 'two.js/src/shapes/rounded-rectangle' {
37173834
* @param {Number} [resolution=12] - The number of vertices used to construct the rounded rectangle.
37183835
*/
37193836
export class RoundedRectangle extends Path {
3720-
static Properties: string[];
3837+
/**
3838+
* @name Two.RoundedRectangle.Properties
3839+
* @property {String[]} - A list of properties that are on every {@link Two.RoundedRectangle}.
3840+
*/
3841+
static Properties: ('width' | 'height' | 'radius' | string)[];
3842+
/**
3843+
* @name Two.RoundedRectangle.fromObject
3844+
* @function
3845+
* @param {Object} obj - Object notation of a {@link Two.RoundedRectangle} to create a new instance
3846+
* @returns {Two.RoundedRectangle}
3847+
* @description Create a new {@link Two.RoundedRectangle} from an object notation of a {@link Two.RoundedRectangle}.
3848+
* @nota-bene Works in conjunction with {@link Two.RoundedRectangle#toObject}
3849+
*/
3850+
fromObject(
3851+
obj:
3852+
| object
3853+
| (Parameters<typeof Path.fromObject> & {
3854+
width?: number;
3855+
height?: number;
3856+
radius?: number;
3857+
})
3858+
): RoundedRectangle;
37213859
constructor(
37223860
x?: number,
37233861
y?: number,
@@ -3761,10 +3899,18 @@ declare module 'two.js/src/shapes/rounded-rectangle' {
37613899
* @see {@link Two.RoundedRectangle#radius}
37623900
*/
37633901
private _radius;
3764-
closed: boolean;
37653902
width: number;
37663903
height: number;
37673904
radius: number | Vector;
3905+
/**
3906+
* @name Two.RoundedRectangle.fromObject
3907+
* @function
3908+
* @param {Object} obj - Object notation of a {@link Two.RoundedRectangle} to create a new instance
3909+
* @returns {Two.RoundedRectangle}
3910+
* @description Create a new {@link Two.RoundedRectangle} from an object notation of a {@link Two.RoundedRectangle}.
3911+
* @nota-bene Works in conjunction with {@link Two.RoundedRectangle#toObject}
3912+
*/
3913+
copy(roundedRectangle: RoundedRectangle): RoundedRectangle;
37683914
}
37693915
import { Path } from 'two.js/src/path';
37703916
import { Vector } from 'two.js/src/vector';
@@ -3776,16 +3922,15 @@ declare module 'two.js/src/text' {
37763922
export type DirectionProperties = 'ltr' | 'rtl';
37773923
export type BaselineProperties = 'top' | 'middle' | 'bottom' | 'baseline';
37783924
/**
3779-
* @name Two.Text
3780-
* @class
3781-
3782-
* @param {String} [message] - The String to be rendered to the scene.
3783-
* @param {Number} [x=0] - The position in the x direction for the object.
3784-
* @param {Number} [y=0] - The position in the y direction for the object.
3785-
* @param {Object} [styles] - An object where styles are applied. Attribute must exist in Two.Text.Properties.
3786-
* @description This is a primitive class for creating drawable text that can be added to the scenegraph.
3787-
* @returns {Text}
3788-
*/
3925+
* @name Two.Text
3926+
* @class
3927+
* @param {String} [message] - The String to be rendered to the scene.
3928+
* @param {Number} [x=0] - The position in the x direction for the object.
3929+
* @param {Number} [y=0] - The position in the y direction for the object.
3930+
* @param {Object} [styles] - An object where styles are applied. Attribute must exist in Two.Text.Properties.
3931+
* @description This is a primitive class for creating drawable text that can be added to the scenegraph.
3932+
* @returns {Text}
3933+
*/
37893934
export class Text extends Shape {
37903935
/**
37913936
* @name Two.Text.Ratio
@@ -3996,17 +4141,17 @@ declare module 'two.js/src/text' {
39964141
* @name Two.Text#alignment
39974142
* @property {String} - Alignment of text in relation to {@link Two.Text#translation}'s coordinates. Possible values include `'left'`, `'center'`, `'right'`. Defaults to `'center'`.
39984143
*/
3999-
alignment: 'left' | 'center' | 'right';
4144+
alignment: AlignmentProperties;
40004145
/**
40014146
* @name Two.Text#baseline
40024147
* @property {String} - The vertical aligment of the text in relation to {@link Two.Text#translation}'s coordinates. Possible values include `'top'`, `'middle'`, `'bottom'`, and `'baseline'`. Defaults to `'baseline'`.
40034148
*/
4004-
baseline: 'top' | 'middle' | 'bottom' | 'baseline';
4149+
baseline: BaselineProperties;
40054150
/**
40064151
* @name Two.Text#style
40074152
* @property {String} - The font's style. Possible values include '`normal`', `'italic'`. Defaults to `'normal'`.
40084153
*/
4009-
style: 'normal' | 'italic';
4154+
style: StyleProperties;
40104155
/**
40114156
* @name Two.Text#weight
40124157
* @property {Number} - A number at intervals of 100 to describe the font's weight. This compatibility varies with the typeface's variant weights. Larger values are bolder. Smaller values are thinner. Defaults to `500`.
@@ -4016,12 +4161,12 @@ declare module 'two.js/src/text' {
40164161
* @name Two.Text#decoration
40174162
* @property {String} - String to delineate whether text should be decorated with for instance an `'underline'`. Defaults to `'none'`.
40184163
*/
4019-
decoration: 'underline' | 'strikethrough' | 'none';
4164+
decoration: DecorationProperties;
40204165
/**
40214166
* @name Two.Text#direction
40224167
* @property {String} - String to determine what direction the text should run. Possibly values are `'ltr'` for left-to-right and `'rtl'` for right-to-left. Defaults to `'ltr'`.
40234168
*/
4024-
direction: 'ltr' | 'rtl';
4169+
direction: DirectionProperties;
40254170
/**
40264171
* @name Two.Text#fill
40274172
* @property {(String|Gradient|Texture)} - The value of what the text object should be filled in with.

0 commit comments

Comments
 (0)