Skip to content

Visualize Your Ideas With Code

fix: support color to null tweening (#387)

Jacob02e9f22027a1
diff --git a/packages/2d/src/decorators/canvasStyleSignal.ts b/packages/2d/src/decorators/canvasStyleSignal.ts
index a0e9519d..8d4ef104 100644
--- a/packages/2d/src/decorators/canvasStyleSignal.ts
+++ b/packages/2d/src/decorators/canvasStyleSignal.ts
@@ -1,7 +1,8 @@
-import {initial, parser, signal} from './signal';
+import {initial, interpolation, parser, signal} from './signal';
import {canvasStyleParser} from '../utils';
import type {CanvasStyle, PossibleCanvasStyle} from '../partials';
import {Signal} from '@motion-canvas/core/lib/signals';
+import {Color} from '@motion-canvas/core/lib/types';
export type CanvasStyleSignal<T> = Signal<PossibleCanvasStyle, CanvasStyle, T>;
@@ -9,6 +10,7 @@ export function canvasStyleSignal(): PropertyDecorator {
return (target, key) => {
signal()(target, key);
parser(canvasStyleParser)(target, key);
+ interpolation(Color.lerp)(target, key);
initial(null)(target, key);
};
}
diff --git a/packages/core/src/types/Color.ts b/packages/core/src/types/Color.ts
index 4a2c2989..a44d8df8 100644
--- a/packages/core/src/types/Color.ts
+++ b/packages/core/src/types/Color.ts
@@ -27,8 +27,8 @@ declare module 'chroma-js' {
interface ColorStatic {
symbol: symbol;
lerp(
- from: ColorInterface | string,
- to: ColorInterface | string,
+ from: ColorInterface | string | null,
+ to: ColorInterface | string | null,
value: number,
colorSpace?: ColorSpace,
): ColorInterface;
@@ -48,18 +48,35 @@ Color.symbol = Color.prototype.symbol = Symbol.for(
);
Color.lerp = Color.prototype.lerp = (
- from: Color | string,
- to: Color | string,
+ from: Color | string | null,
+ to: Color | string | null,
value: number,
colorSpace: InterpolationMode = 'lch',
) => {
- return mix(from, to, value, colorSpace);
+ if (typeof from === 'string') {
+ from = new Color(from);
+ }
+ if (typeof to === 'string') {
+ to = new Color(to);
+ }
+
+ const fromIsColor = from instanceof Color;
+ const toIsColor = to instanceof Color;
+
+ if (!fromIsColor) {
+ from = toIsColor ? (to as Color).alpha(0) : new Color('rgba(0, 0, 0, 0)');
+ }
+ if (!toIsColor) {
+ to = fromIsColor ? (from as Color).alpha(0) : new Color('rgba(0, 0, 0, 0)');
+ }
+
+ return mix(from as Color, to as Color, value, colorSpace);
};
Color.createLerp = Color.prototype.createLerp =
(colorSpace: InterpolationMode) =>
- (from: Color | string, to: Color | string, value: number) =>
- mix(from, to, value, colorSpace);
+ (from: Color | string | null, to: Color | string | null, value: number) =>
+ Color.lerp(from, to, value, colorSpace);
Color.createSignal = (
initial?: SignalValue<PossibleColor>,