Skip to content

Commit

Permalink
add TintTo action
Browse files Browse the repository at this point in the history
  • Loading branch information
srpatel committed Sep 24, 2023
1 parent 833b5ae commit 4f1fc64
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pixi-actions",
"version": "1.0.6",
"version": "1.0.10",
"description": "An Actions system for easy chainable animations of DisplayObjects.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -16,10 +16,10 @@
"pixijs"
],
"peerDependencies": {
"pixi.js": "^6.0.0"
"pixi.js": "^6.5.4"
},
"devDependencies": {
"pixi.js": "^6.0.0",
"pixi.js": "^6.5.4",
"typescript": "^4.2.3"
}
}
10 changes: 10 additions & 0 deletions src/Actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Repeat from './actions/Repeat';
import FadeTo from './actions/FadeTo';
import Delay from './actions/Delay';
import RunFunc from './actions/RunFunc';
import TintTo from './actions/TintTo';

export default class Actions {
static actions: Array<Action> = [];
Expand Down Expand Up @@ -101,6 +102,15 @@ export default class Actions {
return new RotateTo(target, rotation, seconds, interpolation);
}

static tintTo(
target: PIXI.DisplayObject,
tint: number,
seconds: number,
interpolation: Interpolation = Interpolations.pow2out): Action
{
return new TintTo(target, tint, seconds, interpolation);
}

static sequence(
...actions: Array<Action>): Action
{
Expand Down
52 changes: 52 additions & 0 deletions src/actions/TintTo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import * as PIXI from 'pixi.js';
import TargetedAction from './TargetedAction';
import Interpolation from '../Interpolation';
import Interpolations from '../Interpolations';

export default class TintTo extends TargetedAction {
interpolation: Interpolation;
tintableTarget: PIXI.Sprite | PIXI.BitmapText | PIXI.Graphics | PIXI.Mesh = null;
startTint: Array<number> | Float32Array;
tint: Array<number> | Float32Array;

constructor(
target: PIXI.DisplayObject,
tint: number,
seconds: number,
interpolation: Interpolation = Interpolations.linear)
{
super(target, seconds);
this.interpolation = interpolation;
this.tint = PIXI.utils.hex2rgb(tint);

if (this.target instanceof PIXI.Sprite) {
this.tintableTarget = this.target;
}
}

tick(delta: number): boolean {
if (! this.tintableTarget) {
return true;
}

if (this.time === 0) {
this.startTint = PIXI.utils.hex2rgb(this.tintableTarget.tint);
}

this.time += delta;

const factor: number = this.interpolation(this.timeDistance);
const currentTint = [0, 0, 0];
for (let i = 0; i < currentTint.length; i++) {
currentTint[i] = this.startTint[i] + (this.tint[i] - this.startTint[i]) * factor;
}
this.tintableTarget.tint = PIXI.utils.rgb2hex(currentTint);
return this.timeDistance >= 1;
}

reset() {
super.reset();
this.time = 0;
return this;
}
};

0 comments on commit 4f1fc64

Please sign in to comment.