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

chore(TS): animation #8297

Merged
merged 122 commits into from
Dec 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
122 commits
Select commit Hold shift + click to select a range
827cf88
initial undocumented types
kristbp2 Sep 10, 2022
9224364
documenting types,
kristbp2 Sep 11, 2022
fc9300f
remove defaults obj for animate
kristbp2 Sep 16, 2022
badf63c
Merge remote-tracking branch 'origin/master' into laz-animate-typing
kristbp2 Sep 16, 2022
c263864
run prettier
kristbp2 Sep 16, 2022
8c39dd5
Typing for color animation and fixes for animate
kristbp2 Sep 18, 2022
2903682
resolve some comments on the PR
kristbp2 Sep 18, 2022
a0e52f2
remove WithReturnType
kristbp2 Sep 18, 2022
d2075e6
animation registry typings
ShaMan123 Sep 19, 2022
8895a5f
revert animate_color.ts
kristbp2 Sep 23, 2022
4cc9713
more explicit typing in animation_registry.ts
kristbp2 Sep 23, 2022
bb86ef1
Merge remote-tracking branch 'origin/master' into laz-animate-typing
kristbp2 Sep 23, 2022
1ddd449
more explicit typing in animation_registry.ts
kristbp2 Sep 24, 2022
757fd7a
fix comments
kristbp2 Sep 24, 2022
6ebfbda
changelog update
kristbp2 Oct 1, 2022
f2b0f89
made the animate function more typesafe
kristbp2 Oct 8, 2022
1b49cb3
refactor(): animation directory
ShaMan123 Oct 12, 2022
fd134b9
extract request/cancel AnimFrame
ShaMan123 Oct 12, 2022
1f5f12e
rename
ShaMan123 Oct 12, 2022
ae4d15b
revert(): animate
ShaMan123 Oct 12, 2022
91af64a
Update types.ts
ShaMan123 Oct 12, 2022
c9e9a14
refactor(): remove from registry
ShaMan123 Oct 12, 2022
052bd3f
fix(): TDegree/TRadian & number
ShaMan123 Oct 13, 2022
e9b8f2a
Revert "fix(): TDegree/TRadian & number"
ShaMan123 Oct 13, 2022
ff27f6c
Merge branch 'master' into ts-animation
ShaMan123 Oct 13, 2022
213ac95
Merge remote-tracking branch 'origin/master' into laz-animate-typing
kristbp2 Oct 16, 2022
a728316
move stuff in from the ts-animation branch
kristbp2 Oct 16, 2022
966f341
fix building
kristbp2 Oct 16, 2022
9b8397e
WIP. fix bug
kristbp2 Oct 16, 2022
a596aab
refactor(): classes
ShaMan123 Oct 22, 2022
d13069c
prepare for color
ShaMan123 Oct 22, 2022
f5d24be
generics
ShaMan123 Oct 22, 2022
ab4eb7d
Update color.class.ts
ShaMan123 Oct 22, 2022
d3c998b
refactor(): color animation
ShaMan123 Oct 22, 2022
00c1adf
refactor(): extract to files
ShaMan123 Oct 22, 2022
a1466cd
Update CHANGELOG.md
ShaMan123 Oct 22, 2022
79628f4
rename
ShaMan123 Oct 22, 2022
f5842c7
rename
ShaMan123 Oct 22, 2022
7134f75
naming
ShaMan123 Oct 22, 2022
d5edccb
cleanup
ShaMan123 Oct 22, 2022
55f62b7
cleanup
ShaMan123 Oct 22, 2022
ce996cf
Update types.ts
ShaMan123 Oct 22, 2022
52211c8
Update misc.ts
ShaMan123 Oct 22, 2022
9655360
Merge branch 'master' into pr/8297
ShaMan123 Oct 22, 2022
fceb050
cleanup
ShaMan123 Oct 22, 2022
f53e2f5
BREAKING: return context
ShaMan123 Oct 22, 2022
2142869
Update CHANGELOG.md
ShaMan123 Oct 22, 2022
f692967
fix timestamps
ShaMan123 Oct 22, 2022
cb1d54f
fix cancelling empty
ShaMan123 Oct 22, 2022
fc0217d
fix: timestamp , abort before start
ShaMan123 Oct 22, 2022
c022254
fix tests
ShaMan123 Oct 22, 2022
fd763b7
dep(): `colorEasing`
ShaMan123 Oct 22, 2022
139ee11
BREAKING change `animateColor` signature
ShaMan123 Oct 22, 2022
d1c1037
accept color byValue
ShaMan123 Oct 22, 2022
a7a9d28
Revert "accept color byValue"
ShaMan123 Oct 22, 2022
41adf12
fix(): `byValue` over `endValue`
ShaMan123 Oct 22, 2022
aa32d24
block `byValue` on color options
ShaMan123 Oct 23, 2022
470854a
Revert "fix(): `byValue` over `endValue`"
ShaMan123 Oct 23, 2022
dfb5910
Update AnimationBase.ts
ShaMan123 Oct 23, 2022
11de652
fix(): color chage rate
ShaMan123 Oct 23, 2022
86d31db
color byValue
ShaMan123 Oct 23, 2022
7ea80e9
fix(): round color pixels
ShaMan123 Oct 23, 2022
9156442
fix tests
ShaMan123 Oct 23, 2022
cf883e2
fix(): endValue
ShaMan123 Oct 23, 2022
43e76a2
comment
ShaMan123 Oct 23, 2022
1b63622
fix(): immutable state
ShaMan123 Oct 23, 2022
127d7a2
cleanup endValue in class
ShaMan123 Oct 23, 2022
d9609d2
Update types.ts
ShaMan123 Oct 23, 2022
abce738
fix(): byValue endValue type
ShaMan123 Oct 23, 2022
20778ec
fix type
ShaMan123 Oct 23, 2022
abfb814
extract AnimationState
ShaMan123 Oct 23, 2022
7bf61db
change naming from rate to ratio
kristbp2 Oct 23, 2022
587e8f8
stray rate -> ratio
kristbp2 Oct 23, 2022
ee985b5
rate => ratio
ShaMan123 Oct 24, 2022
ffc83f7
currentTime => timeElapsed
ShaMan123 Oct 24, 2022
f239704
Update types.ts
ShaMan123 Oct 24, 2022
6098d0a
comment
ShaMan123 Oct 24, 2022
c35ad7e
types(): no any
ShaMan123 Oct 24, 2022
fb20679
fix return type
ShaMan123 Oct 24, 2022
dee235c
revert(): no options
ShaMan123 Oct 24, 2022
422ec25
fix ts
ShaMan123 Oct 24, 2022
a637f81
add ts tests
ShaMan123 Oct 24, 2022
d6b9b3e
remove getter
ShaMan123 Oct 24, 2022
89e7fe0
Update animation.ts
ShaMan123 Oct 24, 2022
0aac2f2
Merge branch 'master' into pr/8297
ShaMan123 Nov 27, 2022
fcccc69
Update AnimationRegistry.ts
ShaMan123 Nov 27, 2022
8e7c894
Merge branch 'master' into pr/8297
ShaMan123 Dec 1, 2022
1e655c5
fix merge
ShaMan123 Dec 1, 2022
f8f220e
type color
ShaMan123 Dec 1, 2022
f8601df
dep(): fromSource
ShaMan123 Dec 1, 2022
dad8a09
Update color.class.ts
ShaMan123 Dec 1, 2022
f8d9870
Update util.ts
ShaMan123 Dec 1, 2022
5c75285
Update object_animation.mixin.ts
ShaMan123 Dec 1, 2022
70cf087
Update color.class.ts
ShaMan123 Dec 1, 2022
e582c7e
fix
ShaMan123 Dec 1, 2022
c128245
Update object_animation.mixin.ts
ShaMan123 Dec 1, 2022
b2b203e
refactor very old code
ShaMan123 Dec 1, 2022
2a78e5b
restore abort
ShaMan123 Dec 1, 2022
167ea19
default abort
ShaMan123 Dec 1, 2022
5e90298
Update AnimationBase.ts
ShaMan123 Dec 1, 2022
fbb08d9
Doc additions and fixes
Lazauya Dec 5, 2022
de550d2
Merge remote-tracking branch 'laz-fork/laz-animate-typing' into laz-a…
Lazauya Dec 5, 2022
58f0ffc
Merge remote-tracking branch 'origin/master' into laz-animate-typing
Lazauya Dec 5, 2022
b56fd41
Merge remote-tracking branch 'origin/master' into laz-animate-typing
Lazauya Dec 10, 2022
d488aef
Doc additions and fixes
Lazauya Dec 10, 2022
3e7e0c5
Merge remote-tracking branch 'origin/master' into laz-animate-typing
Lazauya Dec 11, 2022
a9cd9b8
import fix, commenting, and removed unused color code
Lazauya Dec 11, 2022
76823da
@see fix
Lazauya Dec 11, 2022
a89ce35
cleanup JSDOC
ShaMan123 Dec 11, 2022
98f9966
fix merge conflict
ShaMan123 Dec 11, 2022
36cd48a
imports
ShaMan123 Dec 11, 2022
68fdc11
accept color instance
ShaMan123 Dec 27, 2022
6e8861e
Update color.js
ShaMan123 Dec 27, 2022
9d6c71e
Merge branch 'master' into pr/8297
ShaMan123 Dec 27, 2022
53779e6
fix imports
ShaMan123 Dec 27, 2022
dab0ade
solved merge conflicts
asturur Dec 29, 2022
e1e2c0b
removed unnecessary changes to object.ts
asturur Dec 29, 2022
e5f3cd7
ooops
asturur Dec 29, 2022
30c5172
it builds now
asturur Dec 29, 2022
3366fe6
rename: Animation => ValueAnimation
ShaMan123 Dec 29, 2022
f644fd2
no arrow methods
ShaMan123 Dec 29, 2022
02d8c0d
Merge branch 'laz-animate-typing' of https://github.com/Lazauya/fabri…
ShaMan123 Dec 29, 2022
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [next]

- refactor(TS): `animate` and `AnimationRegistry` to classes [#8297](https://github.com/fabricjs/fabric.js/pull/8297)
BREAKING:
- return animation instance from animate instead of a cancel function and remove `findAnimationByXXX` from `AnimationRegistry`
- change `animateColor` signature to match `animate`, removed `colorEasing`
- fix(Object Stacking): 🔙 refactor logic to support Group 🔝
- chore(TS): migrate Group/ActiveSelection [#8455](https://github.com/fabricjs/fabric.js/pull/8455)
- chore(TS): Migrate smaller mixins to classes (dataurl and serialization ) [#8542](https://github.com/fabricjs/fabric.js/pull/8542)
Expand Down
2 changes: 1 addition & 1 deletion src/canvas/static_canvas.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
TToCanvasElementOptions,
TValidToObjectMethod,
} from '../typedefs';
import { cancelAnimFrame, requestAnimFrame } from '../util/animate';
import { cancelAnimFrame, requestAnimFrame } from '../util/animation';
import {
cleanUpJsdomNode,
getElementOffset,
Expand Down
120 changes: 56 additions & 64 deletions src/color/color.class.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,66 @@
//@ts-nocheck
import { ColorNameMap } from './color_map';
import { reHSLa, reHex, reRGBa } from './constants';
import { hue2rgb, hexify } from './util';

type TColorSource = [number, number, number];
/**
* RGB format
*/
export type TRGBColorSource = [red: number, green: number, blue: number];

/**
* RGBA format
*/
export type TRGBAColorSource = [
red: number,
green: number,
blue: number,
alpha: number
];

type TColorAlphaSource = [number, number, number, number];
export type TColorArg = string | TRGBColorSource | TRGBAColorSource | Color;

/**
* @class Color common color operations
* @tutorial {@link http://fabricjs.com/fabric-intro-part-2/#colors colors}
*/
export class Color {
private _source: TColorAlphaSource;
private _source: TRGBAColorSource;

/**
*
* @param {string} [color] optional in hex or rgb(a) or hsl format or from known color list
*/
constructor(color?: string) {
constructor(color?: TColorArg) {
if (!color) {
// we default to black as canvas does
this.setSource([0, 0, 0, 1]);
} else if (color instanceof Color) {
this.setSource([...color._source]);
} else if (Array.isArray(color)) {
const [r, g, b, a = 1] = color;
this.setSource([r, g, b, a]);
} else {
this._tryParsingColor(color);
this.setSource(this._tryParsingColor(color));
}
}

/**
* @private
* @param {string} [color] Color value to parse
* @returns {TRGBAColorSource}
*/
_tryParsingColor(color?: string) {
protected _tryParsingColor(color: string) {
if (color in ColorNameMap) {
color = ColorNameMap[color];
color = ColorNameMap[color as keyof typeof ColorNameMap];
}

const source =
color === 'transparent'
? [255, 255, 255, 0]
: Color.sourceFromHex(color) ||
return color === 'transparent'
? ([255, 255, 255, 0] as TRGBAColorSource)
: Color.sourceFromHex(color) ||
Color.sourceFromRgb(color) ||
Color.sourceFromHsl(color) || [0, 0, 0, 1]; // color is not recognize let's default to black as canvas does

if (source) {
this.setSource(source);
}
Color.sourceFromHsl(color) ||
// color is not recognized
// we default to black as canvas does
([0, 0, 0, 1] as TRGBAColorSource);
}

/**
Expand All @@ -53,16 +69,16 @@ export class Color {
* @param {Number} r Red color value
* @param {Number} g Green color value
* @param {Number} b Blue color value
* @return {TColorSource} Hsl color
* @return {TRGBColorSource} Hsl color
*/
_rgbToHsl(r: number, g: number, b: number): TColorSource {
_rgbToHsl(r: number, g: number, b: number): TRGBColorSource {
r /= 255;
g /= 255;
b /= 255;
const maxValue = Math.max(r, g, b),
minValue = Math.min(r, g, b);

let h, s;
let h!: number, s: number;
const l = (maxValue + minValue) / 2;

if (maxValue === minValue) {
Expand All @@ -89,17 +105,17 @@ export class Color {

/**
* Returns source of this color (where source is an array representation; ex: [200, 200, 100, 1])
* @return {TColorAlphaSource}
* @return {TRGBAColorSource}
*/
getSource() {
return this._source;
}

/**
* Sets source of this color (where source is an array representation; ex: [200, 200, 100, 1])
* @param {TColorAlphaSource} source
* @param {TRGBAColorSource} source
*/
setSource(source: TColorAlphaSource) {
setSource(source: TRGBAColorSource) {
this._source = source;
}

Expand Down Expand Up @@ -223,20 +239,14 @@ export class Color {
otherColor = new Color(otherColor);
}

const result = [],
alpha = this.getAlpha(),
const [r, g, b, alpha] = this.getSource(),
otherAlpha = 0.5,
source = this.getSource(),
otherSource = otherColor.getSource();

for (let i = 0; i < 3; i++) {
result.push(
Math.round(source[i] * (1 - otherAlpha) + otherSource[i] * otherAlpha)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This calculation is strange.
Should otherAlpha be 0.5?
If so we can simplify to:
Math.round(value * + otherSource[index] * 0.25)

Copy link
Contributor Author

@Lazauya Lazauya Dec 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a lot of ways to blend colors, and using the current RGB method is honestly a pretty crappy way to do it, though still technically correct. This Stackoverflow answer has a good overview. I personally prefer HSL color mixing/interpolation and it's generally described as being the most "intuitive" result wise.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could expose an option for color interpolation, the calculate method is there for such subclassing.
But I want to finalize this PR.
So if you want to propose something fork this branch and start another PR, that's what I do.

Copy link
Contributor

@ShaMan123 ShaMan123 Dec 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would change ColorAnimation to accept color string values or TRGBAColorSource or preferably a Color instance, that way it can accept any color space.
Then with a color space option it becomes very powerful

otherSource = otherColor.getSource(),
[R, G, B] = [r, g, b].map((value, index) =>
Math.round(value * (1 - otherAlpha) + otherSource[index] * otherAlpha)
);
}

result[3] = alpha;
this.setSource(result);
this.setSource([R, G, B, alpha]);
return this;
}

Expand All @@ -259,16 +269,16 @@ export class Color {
* @return {Color}
*/
static fromRgba(color: string): Color {
return Color.fromSource(Color.sourceFromRgb(color));
return new Color(Color.sourceFromRgb(color));
}

/**
* Returns array representation (ex: [100, 100, 200, 1]) of a color that's in RGB or RGBA format
* @memberOf Color
* @param {String} color Color value ex: rgb(0-255,0-255,0-255), rgb(0%-100%,0%-100%,0%-100%)
* @return {TColorAlphaSource | undefined} source
* @return {TRGBAColorSource | undefined} source
*/
static sourceFromRgb(color: string): TColorAlphaSource | undefined {
static sourceFromRgb(color: string): TRGBAColorSource | undefined {
const match = color.match(reRGBa);
if (match) {
const r =
Expand All @@ -281,12 +291,7 @@ export class Color {
(parseInt(match[3], 10) / (/%$/.test(match[3]) ? 100 : 1)) *
(/%$/.test(match[3]) ? 255 : 1);

return [
parseInt(r, 10),
parseInt(g, 10),
parseInt(b, 10),
match[4] ? parseFloat(match[4]) : 1,
];
return [r, g, b, match[4] ? parseFloat(match[4]) : 1];
}
}

Expand All @@ -309,18 +314,18 @@ export class Color {
* @return {Color}
*/
static fromHsla(color: string): Color {
return Color.fromSource(Color.sourceFromHsl(color));
return new Color(Color.sourceFromHsl(color));
}

/**
* Returns array representation (ex: [100, 100, 200, 1]) of a color that's in HSL or HSLA format.
* Adapted from <a href="https://rawgithub.com/mjijackson/mjijackson.github.com/master/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript.html">https://github.com/mjijackson</a>
* @memberOf Color
* @param {String} color Color value ex: hsl(0-360,0%-100%,0%-100%) or hsla(0-360,0%-100%,0%-100%, 0-1)
* @return {TColorAlphaSource | undefined} source
* @return {TRGBAColorSource | undefined} source
* @see http://http://www.w3.org/TR/css3-color/#hsl-color
*/
static sourceFromHsl(color: string): TColorAlphaSource | undefined {
static sourceFromHsl(color: string): TRGBAColorSource | undefined {
const match = color.match(reHSLa);
if (!match) {
return;
Expand All @@ -329,7 +334,7 @@ export class Color {
const h = (((parseFloat(match[1]) % 360) + 360) % 360) / 360,
s = parseFloat(match[2]) / (/%$/.test(match[2]) ? 100 : 1),
l = parseFloat(match[3]) / (/%$/.test(match[3]) ? 100 : 1);
let r, g, b;
let r: number, g: number, b: number;

if (s === 0) {
r = g = b = l;
Expand Down Expand Up @@ -358,17 +363,17 @@ export class Color {
* @return {Color}
*/
static fromHex(color: string): Color {
return Color.fromSource(Color.sourceFromHex(color));
return new Color(Color.sourceFromHex(color));
}

/**
* Returns array representation (ex: [100, 100, 200, 1]) of a color that's in HEX format
* @static
* @memberOf Color
* @param {String} color ex: FF5555 or FF5544CC (RGBa)
* @return {TColorAlphaSource | undefined} source
* @return {TRGBAColorSource | undefined} source
*/
static sourceFromHex(color: string): TColorAlphaSource | undefined {
static sourceFromHex(color: string): TRGBAColorSource | undefined {
if (color.match(reHex)) {
const value = color.slice(color.indexOf('#') + 1),
isShortNotation = value.length === 3 || value.length === 4,
Expand Down Expand Up @@ -396,17 +401,4 @@ export class Color {
];
}
}

/**
* Returns new color object, when given color in array representation (ex: [200, 100, 100, 0.5])
* @static
* @memberOf Color
* @param {TColorSource | TColorAlphaSource} source
* @return {Color}
*/
static fromSource(source: TColorSource | TColorAlphaSource): Color {
const oColor = new Color();
oColor.setSource(source);
return oColor;
}
}
5 changes: 1 addition & 4 deletions src/color/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/**
* @private
* @param {Number} p
* @param {Number} q
* @param {Number} t
Expand All @@ -25,9 +24,7 @@ export function hue2rgb(p: number, q: number, t: number): number {
}

/**
* Convert a [0, 255] value to hex
* @param value
* @returns
* Convert a value ∈ [0, 255] to hex
*/
export function hexify(value: number) {
const hexValue = value.toString(16).toUpperCase();
Expand Down
Loading