Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: added fadeIn to opacityComp #62

Merged
merged 5 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions examples/fadeIn.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ loadBean();
const bean = add([
sprite("bean"),
pos(120, 80),
opacity(), // opacity() component gives it opacity which is required for fadeIn
fadeIn(), // fadeIn() component makes it fade in
opacity(1), // opacity() component gives it opacity which is required for fadeIn
]);

bean.fadeIn(1) // makes it fade in

// spawn another bean that takes 5 seconds to fade in halfway
// SPOOKY!
let spookyBean = add([
sprite("bean"),
pos(240, 80),
opacity(0.5), // opacity() component gives it opacity which is required for fadeIn (set to 0.5 so it will be half transparent)
fadeIn(5), // fadeIn() component makes it fade in (set to 5 so that it takes 5 seconds to fade in)
]);

spookyBean.fadeIn(5) // makes it fade in (set to 5 so that it takes 5 seconds to fade in)
10 changes: 9 additions & 1 deletion src/components/draw/opacity.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { getInternalContext, getKaboomContext } from "@/kaboom";
import type { OpacityComp, TweenController } from "@/types";

// TODO: fadeIn here?
export function opacity(a: number): OpacityComp {
const k = getKaboomContext(this);
const internal = getInternalContext(k);
Expand All @@ -12,6 +11,15 @@ export function opacity(a: number): OpacityComp {
inspect() {
return `${internal.toFixed(this.opacity, 1)}`;
},
fadeIn(time = 1, easeFunc = k.easings.linear): TweenController {
return k.tween(
0,
this.opacity,
time,
(a) => this.opacity = a,
easeFunc,
);
},
fadeOut(time = 1, easeFunc = k.easings.linear): TweenController {
return k.tween(
this.opacity,
Expand Down
14 changes: 2 additions & 12 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,7 @@ export interface KaboomCtx {
*
* @since v3000.0
* @group Components
* @deprecated since v3001.0
*/
fadeIn(time: number): Comp;
/**
Expand Down Expand Up @@ -5137,21 +5138,10 @@ export interface ColorComp extends Comp {
*/
export interface OpacityComp extends Comp {
opacity: number;
fadeIn(time?: number, easeFunc?: EaseFunc): TweenController;
fadeOut(time?: number, easeFunc?: EaseFunc): TweenController;
}

/**
* @group Component System
*/
export interface OpacityOpt {
/**
* Fade in n seconds when object is added to scene.
*
* @since v3000.0
*/
fadeIn?: number;
}

/**
* @group Component System
*/
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
"src/**/*",
"scripts/**/*"
]
}
}