frigame3 3.0.0 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Animation.d.ts +42 -0
- package/dist/Animation.js +189 -0
- package/dist/BaseSprite.d.ts +116 -0
- package/dist/BaseSprite.js +518 -0
- package/dist/Gradient.d.ts +31 -0
- package/dist/Gradient.js +89 -0
- package/dist/Playground.d.ts +48 -0
- package/dist/Playground.js +167 -0
- package/dist/Rect.d.ts +63 -0
- package/dist/Rect.js +155 -0
- package/dist/Rectangle.d.ts +31 -0
- package/dist/Rectangle.js +69 -0
- package/dist/Renderer.d.ts +23 -0
- package/dist/Renderer.js +1 -0
- package/dist/ResourceManager.d.ts +21 -0
- package/dist/ResourceManager.js +100 -0
- package/dist/Sprite.d.ts +56 -0
- package/dist/Sprite.js +437 -0
- package/dist/SpriteGroup.d.ts +39 -0
- package/dist/SpriteGroup.js +176 -0
- package/dist/canvasRenderer/renderGroup.d.ts +15 -0
- package/dist/canvasRenderer/renderGroup.js +134 -0
- package/dist/canvasRenderer/renderPlayground.d.ts +9 -0
- package/dist/canvasRenderer/renderPlayground.js +67 -0
- package/dist/canvasRenderer/renderRectangle.d.ts +15 -0
- package/dist/canvasRenderer/renderRectangle.js +287 -0
- package/dist/canvasRenderer/renderSprite.d.ts +4 -0
- package/dist/canvasRenderer/renderSprite.js +93 -0
- package/dist/canvasRenderer.d.ts +2 -0
- package/dist/canvasRenderer.js +19 -0
- package/dist/dummyRenderer.d.ts +2 -0
- package/dist/dummyRenderer.js +41 -0
- package/dist/plugins/GamepadTracker.d.ts +15 -0
- package/dist/plugins/GamepadTracker.js +74 -0
- package/dist/plugins/Tilemap.d.ts +26 -0
- package/dist/plugins/Tilemap.js +85 -0
- package/dist/plugins/Vec2.d.ts +69 -0
- package/dist/plugins/Vec2.js +296 -0
- package/dist/plugins/fx/Shaker.d.ts +46 -0
- package/dist/plugins/fx/Shaker.js +190 -0
- package/dist/plugins/fx/Tweener.d.ts +42 -0
- package/dist/plugins/fx/Tweener.js +166 -0
- package/dist/plugins/fx/easing.d.ts +32 -0
- package/dist/plugins/fx/easing.js +192 -0
- package/dist/plugins/isometric/ISOMixin.d.ts +138 -0
- package/dist/plugins/isometric/ISOMixin.js +322 -0
- package/dist/plugins/isometric/ISORectangle.d.ts +151 -0
- package/dist/plugins/isometric/ISORectangle.js +127 -0
- package/dist/plugins/isometric/ISOSprite.d.ts +157 -0
- package/dist/plugins/isometric/ISOSprite.js +176 -0
- package/dist/plugins/isometric/ISOSpriteGroup.d.ts +151 -0
- package/dist/plugins/isometric/ISOSpriteGroup.js +116 -0
- package/dist/plugins/isometric/ISOTilemap.d.ts +24 -0
- package/dist/plugins/isometric/ISOTilemap.js +85 -0
- package/dist/plugins/isometric/utils.d.ts +2 -0
- package/dist/plugins/isometric/utils.js +10 -0
- package/dist/plugins/keyTracker.d.ts +3 -0
- package/dist/plugins/keyTracker.js +23 -0
- package/dist/plugins/mixer.d.ts +72 -0
- package/dist/plugins/mixer.js +396 -0
- package/dist/plugins/sorted/SortedAnimation.d.ts +13 -0
- package/dist/plugins/sorted/SortedAnimation.js +27 -0
- package/dist/plugins/sorted/SortedGroup.d.ts +137 -0
- package/dist/plugins/sorted/SortedGroup.js +61 -0
- package/dist/plugins/sorted/SortedMixin.d.ts +122 -0
- package/dist/plugins/sorted/SortedMixin.js +153 -0
- package/dist/plugins/sorted/SortedRectangle.d.ts +127 -0
- package/dist/plugins/sorted/SortedRectangle.js +17 -0
- package/dist/plugins/sorted/SortedSprite.d.ts +130 -0
- package/dist/plugins/sorted/SortedSprite.js +35 -0
- package/dist/utils.d.ts +11 -0
- package/dist/utils.js +33 -0
- package/package.json +6 -2
- package/CHANGELOG.md +0 -13
- package/biome.json +0 -93
- package/demo/package-lock.json +0 -515
- package/demo/package.json +0 -19
- package/demo/snake/index.html +0 -16
- package/demo/snake/index.ts +0 -110
- package/demo/tetris/index.html +0 -16
- package/demo/tetris/index.ts +0 -536
- package/demo/tsconfig.json +0 -53
- package/src/Animation.ts +0 -266
- package/src/BaseSprite.ts +0 -682
- package/src/Gradient.ts +0 -120
- package/src/Playground.ts +0 -247
- package/src/Rect.ts +0 -237
- package/src/Rectangle.ts +0 -108
- package/src/Renderer.ts +0 -28
- package/src/ResourceManager.ts +0 -145
- package/src/Sprite.ts +0 -484
- package/src/SpriteGroup.ts +0 -258
- package/src/canvasRenderer/renderGroup.ts +0 -194
- package/src/canvasRenderer/renderPlayground.ts +0 -103
- package/src/canvasRenderer/renderRectangle.ts +0 -421
- package/src/canvasRenderer/renderSprite.ts +0 -148
- package/src/canvasRenderer.ts +0 -38
- package/src/dummyRenderer.ts +0 -62
- package/src/plugins/GamepadTracker.ts +0 -97
- package/src/plugins/Tilemap.ts +0 -123
- package/src/plugins/Vec2.ts +0 -420
- package/src/plugins/fx/Shaker.ts +0 -293
- package/src/plugins/fx/Tweener.ts +0 -252
- package/src/plugins/fx/easing.ts +0 -276
- package/src/plugins/isometric/ISOMixin.ts +0 -444
- package/src/plugins/isometric/ISORectangle.ts +0 -183
- package/src/plugins/isometric/ISOSprite.ts +0 -228
- package/src/plugins/isometric/ISOSpriteGroup.ts +0 -155
- package/src/plugins/isometric/ISOTilemap.ts +0 -122
- package/src/plugins/isometric/utils.ts +0 -19
- package/src/plugins/keyTracker.ts +0 -31
- package/src/plugins/mixer.ts +0 -523
- package/src/plugins/sorted/SortedAnimation.ts +0 -53
- package/src/plugins/sorted/SortedGroup.ts +0 -94
- package/src/plugins/sorted/SortedMixin.ts +0 -203
- package/src/plugins/sorted/SortedRectangle.ts +0 -35
- package/src/plugins/sorted/SortedSprite.ts +0 -56
- package/src/utils.ts +0 -53
- package/test/animation/index.html +0 -20
- package/test/animation/index.ts +0 -253
- package/test/animation/mh.png +0 -0
- package/test/animation/moh.png +0 -0
- package/test/animation/mov.png +0 -0
- package/test/animation/mv.png +0 -0
- package/test/animation/rebound.png +0 -0
- package/test/animation/reboundm.png +0 -0
- package/test/animation/sh.png +0 -0
- package/test/animation/soh.png +0 -0
- package/test/animation/sov.png +0 -0
- package/test/animation/sv.png +0 -0
- package/test/isometric1/beach.png +0 -0
- package/test/isometric1/foglie.png +0 -0
- package/test/isometric1/grass.png +0 -0
- package/test/isometric1/index.html +0 -17
- package/test/isometric1/index.ts +0 -68
- package/test/isometric1/tronco.png +0 -0
- package/test/isometric1/untitled.tmx +0 -25
- package/test/isometric1/water.png +0 -0
- package/test/isometric2/banner.png +0 -0
- package/test/isometric2/books.png +0 -0
- package/test/isometric2/candlestick.png +0 -0
- package/test/isometric2/ice.png +0 -0
- package/test/isometric2/index.html +0 -16
- package/test/isometric2/index.ts +0 -129
- package/test/isometric2/knight_se.png +0 -0
- package/test/isometric2/tile.png +0 -0
- package/test/isometric2/untitled.tmx +0 -36
- package/test/isometric2/wall.png +0 -0
- package/test/layers/boy.png +0 -0
- package/test/layers/catgirl.png +0 -0
- package/test/layers/fri1.svg +0 -8
- package/test/layers/grassblock.png +0 -0
- package/test/layers/horngirl.png +0 -0
- package/test/layers/index.html +0 -17
- package/test/layers/index.ts +0 -191
- package/test/layers/pinkgirl.png +0 -0
- package/test/layers/render_ruler.py +0 -57
- package/test/layers/ruler.png +0 -0
- package/test/layers/sh.png +0 -0
- package/test/mixer/dieci.opus +0 -0
- package/test/mixer/dieci.wav +0 -0
- package/test/mixer/index.html +0 -33
- package/test/mixer/index.ts +0 -206
- package/test/mixer/news_intro.mp3 +0 -0
- package/test/mixer/news_intro.ogg +0 -0
- package/test/mixer/prova.mp3 +0 -0
- package/test/mixer/prova.ogg +0 -0
- package/test/package-lock.json +0 -515
- package/test/package.json +0 -19
- package/test/sorted/Brown Block.png +0 -0
- package/test/sorted/index.html +0 -16
- package/test/sorted/index.ts +0 -38
- package/test/transformation1/index.html +0 -17
- package/test/transformation1/index.ts +0 -51
- package/test/transformation1/sh.png +0 -0
- package/test/transformation2/index.html +0 -20
- package/test/transformation2/index.ts +0 -71
- package/test/transformation2/sh.png +0 -0
- package/test/tsconfig.json +0 -53
- package/tsconfig.json +0 -51
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { lerp } from "../../utils.js";
|
|
2
|
+
import { linear } from "./easing.js";
|
|
3
|
+
export class Shaker extends EventTarget {
|
|
4
|
+
_nextId = 1; // Start from 1 to guarantee that shakeId is always truthy
|
|
5
|
+
_shake_queue = new Map();
|
|
6
|
+
_shaking = false;
|
|
7
|
+
_playground;
|
|
8
|
+
_callbackId = null;
|
|
9
|
+
constructor(playground) {
|
|
10
|
+
super();
|
|
11
|
+
this._playground = new WeakRef(playground);
|
|
12
|
+
this.registerCallback();
|
|
13
|
+
playground.addEventListener("clearCallbacks", this._onClearCallbacks);
|
|
14
|
+
}
|
|
15
|
+
_shakeStep = () => {
|
|
16
|
+
if (this._shaking) {
|
|
17
|
+
// Process all the shakes in the queue
|
|
18
|
+
for (const [shakeId, shake_obj] of this._shake_queue) {
|
|
19
|
+
const target_obj = shake_obj.target_obj;
|
|
20
|
+
shake_obj.current_step += 1;
|
|
21
|
+
if (shake_obj.current_step >= shake_obj.steps_per_sample) {
|
|
22
|
+
shake_obj.current_step = 0;
|
|
23
|
+
shake_obj.current_sample += 1;
|
|
24
|
+
if (shake_obj.current_sample >= shake_obj.num_sample) {
|
|
25
|
+
// This object has finished its shaking
|
|
26
|
+
// Restore every property to its initial value
|
|
27
|
+
for (const [property, shake_prop] of shake_obj.property_list) {
|
|
28
|
+
target_obj[property] = shake_prop.start_value;
|
|
29
|
+
}
|
|
30
|
+
// Call the complete callback
|
|
31
|
+
const callback = shake_obj.callback;
|
|
32
|
+
callback?.();
|
|
33
|
+
// Remove this shake
|
|
34
|
+
this._shake_queue.delete(shakeId);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
for (const [property, shake_prop] of shake_obj.property_list) {
|
|
38
|
+
// Set the properties to the current value
|
|
39
|
+
target_obj[property] = shake_prop.sample_value;
|
|
40
|
+
// Update the property list with a new sample
|
|
41
|
+
shake_prop.current_value = shake_prop.sample_value;
|
|
42
|
+
shake_prop.sample_value =
|
|
43
|
+
shake_prop.start_value +
|
|
44
|
+
lerp((Math.random() * 2 - 1) * shake_prop.amplitude, 0, shake_obj.decay(shake_obj.current_sample / shake_obj.num_sample));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
// Set the properties to the current value
|
|
50
|
+
for (const [property, shake_prop] of shake_obj.property_list) {
|
|
51
|
+
target_obj[property] = lerp(shake_prop.current_value, shake_prop.sample_value, shake_obj.current_step / shake_obj.steps_per_sample);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
// If there are no more shakes in the queue, this callback can be stopped
|
|
56
|
+
if (this._shake_queue.size === 0) {
|
|
57
|
+
this._shaking = false;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
registerCallback(options) {
|
|
62
|
+
if (this._callbackId === null) {
|
|
63
|
+
const playground = this._playground.deref();
|
|
64
|
+
if (playground !== undefined) {
|
|
65
|
+
this._callbackId = playground.registerCallback(this._shakeStep);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
if (typeof console !== "undefined" &&
|
|
69
|
+
options?.suppressWarning === false) {
|
|
70
|
+
console.warn("playground has been garbage collected");
|
|
71
|
+
console.trace();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
if (typeof console !== "undefined" &&
|
|
77
|
+
options?.suppressWarning === false) {
|
|
78
|
+
console.warn("Callback already registered");
|
|
79
|
+
console.trace();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return this;
|
|
83
|
+
}
|
|
84
|
+
removeCallback(options) {
|
|
85
|
+
const playground = this._playground.deref();
|
|
86
|
+
if (playground !== undefined) {
|
|
87
|
+
playground.removeCallback(this._callbackId, options);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
if (typeof console !== "undefined" &&
|
|
91
|
+
options?.suppressWarning === false) {
|
|
92
|
+
console.warn("playground has been garbage collected");
|
|
93
|
+
console.trace();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
this._callbackId = null;
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
_onClearCallbacks = () => {
|
|
100
|
+
if (this._callbackId !== null) {
|
|
101
|
+
this._callbackId = null;
|
|
102
|
+
this.registerCallback();
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
shake(target_obj, properties, options) {
|
|
106
|
+
const playground = this._playground.deref();
|
|
107
|
+
if (playground !== undefined) {
|
|
108
|
+
const duration = options?.duration || 1;
|
|
109
|
+
const frequency = options?.frequency || 60;
|
|
110
|
+
const decay = options?.decay ?? linear;
|
|
111
|
+
const steps_per_sample = playground.framesFromMs(1000 / frequency);
|
|
112
|
+
const num_sample = Math.ceil(playground.framesFromMs(duration) / steps_per_sample);
|
|
113
|
+
const shake_obj = {
|
|
114
|
+
target_obj: target_obj,
|
|
115
|
+
current_sample: 0,
|
|
116
|
+
num_sample: num_sample,
|
|
117
|
+
current_step: 0,
|
|
118
|
+
steps_per_sample: steps_per_sample,
|
|
119
|
+
decay: decay,
|
|
120
|
+
property_list: new Map(),
|
|
121
|
+
};
|
|
122
|
+
if (options?.callback !== undefined) {
|
|
123
|
+
shake_obj.callback = options.callback;
|
|
124
|
+
}
|
|
125
|
+
const property_list = shake_obj.property_list;
|
|
126
|
+
for (const [property, value] of Object.entries(properties)) {
|
|
127
|
+
const start_value = target_obj[property];
|
|
128
|
+
const amplitude = value;
|
|
129
|
+
property_list.set(property, {
|
|
130
|
+
amplitude: amplitude,
|
|
131
|
+
start_value: start_value,
|
|
132
|
+
current_value: start_value,
|
|
133
|
+
sample_value: start_value + (Math.random() * 2 - 1) * amplitude,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
const shakeId = this._nextId;
|
|
137
|
+
this._nextId += 1;
|
|
138
|
+
this._shake_queue.set(shakeId, shake_obj);
|
|
139
|
+
this._shaking = true;
|
|
140
|
+
return shakeId;
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
if (typeof console !== "undefined" &&
|
|
144
|
+
options?.suppressWarning === false) {
|
|
145
|
+
console.warn("playground has been garbage collected");
|
|
146
|
+
console.trace();
|
|
147
|
+
}
|
|
148
|
+
return -1;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
removeShake(shakeId, options) {
|
|
152
|
+
if (shakeId === null) {
|
|
153
|
+
if (typeof console !== "undefined" &&
|
|
154
|
+
options?.suppressWarning === false) {
|
|
155
|
+
console.warn("shakeId is null");
|
|
156
|
+
console.trace();
|
|
157
|
+
}
|
|
158
|
+
return this;
|
|
159
|
+
}
|
|
160
|
+
const shake_obj = this._shake_queue.get(shakeId);
|
|
161
|
+
if (shake_obj !== undefined) {
|
|
162
|
+
const target_obj = shake_obj.target_obj;
|
|
163
|
+
// Restore every property to its initial value
|
|
164
|
+
for (const [property, shake_prop] of shake_obj.property_list) {
|
|
165
|
+
target_obj[property] = shake_prop.start_value;
|
|
166
|
+
}
|
|
167
|
+
this._shake_queue.delete(shakeId);
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
if (typeof console !== "undefined" &&
|
|
171
|
+
options?.suppressWarning === false) {
|
|
172
|
+
console.warn("No shakes removed");
|
|
173
|
+
console.trace();
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return this;
|
|
177
|
+
}
|
|
178
|
+
clearShakes() {
|
|
179
|
+
for (const shake_obj of this._shake_queue.values()) {
|
|
180
|
+
const target_obj = shake_obj.target_obj;
|
|
181
|
+
// Restore every property to its initial value
|
|
182
|
+
for (const [property, shake_prop] of shake_obj.property_list) {
|
|
183
|
+
target_obj[property] = shake_prop.start_value;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
this._shake_queue.clear();
|
|
187
|
+
this.dispatchEvent(new Event("clearShakes"));
|
|
188
|
+
return this;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Playground } from "../../Playground.js";
|
|
2
|
+
export type Speed = "slow" | "fast";
|
|
3
|
+
export interface TweenOptions {
|
|
4
|
+
duration: Speed | number;
|
|
5
|
+
easing: (t: number) => number;
|
|
6
|
+
callback: () => void;
|
|
7
|
+
suppressWarning: boolean;
|
|
8
|
+
}
|
|
9
|
+
interface TweenProp {
|
|
10
|
+
start_value: number;
|
|
11
|
+
target_value: number;
|
|
12
|
+
}
|
|
13
|
+
interface TweenObj<T extends object> {
|
|
14
|
+
target_obj: T;
|
|
15
|
+
current_step: number;
|
|
16
|
+
num_step: number;
|
|
17
|
+
easing: (t: number) => number;
|
|
18
|
+
callback?: () => void;
|
|
19
|
+
property_list: Map<keyof T, TweenProp>;
|
|
20
|
+
}
|
|
21
|
+
export declare class Tweener extends EventTarget {
|
|
22
|
+
_nextId: number;
|
|
23
|
+
_tween_queue: Map<number, TweenObj<Record<string, number>>>;
|
|
24
|
+
_tweening: boolean;
|
|
25
|
+
_playground: WeakRef<Playground>;
|
|
26
|
+
_callbackId: number | null;
|
|
27
|
+
constructor(playground: Playground);
|
|
28
|
+
_tweenStep: () => void;
|
|
29
|
+
registerCallback(options?: {
|
|
30
|
+
suppressWarning?: boolean;
|
|
31
|
+
}): this;
|
|
32
|
+
removeCallback(options?: {
|
|
33
|
+
suppressWarning?: boolean;
|
|
34
|
+
}): this;
|
|
35
|
+
_onClearCallbacks: () => void;
|
|
36
|
+
tween<T extends object>(target_obj: T, properties: Partial<Record<keyof T, number>>, options?: Partial<TweenOptions>): number;
|
|
37
|
+
removeTween(tweenId: number | null, options?: {
|
|
38
|
+
suppressWarning?: boolean;
|
|
39
|
+
}): this;
|
|
40
|
+
clearTweens(): this;
|
|
41
|
+
}
|
|
42
|
+
export {};
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { lerp } from "../../utils.js";
|
|
2
|
+
import { swing } from "./easing.js";
|
|
3
|
+
const speeds = {
|
|
4
|
+
slow: 600,
|
|
5
|
+
fast: 200,
|
|
6
|
+
_default: 400,
|
|
7
|
+
};
|
|
8
|
+
export class Tweener extends EventTarget {
|
|
9
|
+
_nextId = 1; // Start from 1 to guarantee that tweenId is always truthy
|
|
10
|
+
_tween_queue = new Map();
|
|
11
|
+
_tweening = false;
|
|
12
|
+
_playground;
|
|
13
|
+
_callbackId = null;
|
|
14
|
+
constructor(playground) {
|
|
15
|
+
super();
|
|
16
|
+
this._playground = new WeakRef(playground);
|
|
17
|
+
this.registerCallback();
|
|
18
|
+
playground.addEventListener("clearCallbacks", this._onClearCallbacks);
|
|
19
|
+
}
|
|
20
|
+
_tweenStep = () => {
|
|
21
|
+
if (this._tweening) {
|
|
22
|
+
// Process all the tweens in the queue
|
|
23
|
+
for (const [tweenId, tween_obj] of this._tween_queue) {
|
|
24
|
+
const target_obj = tween_obj.target_obj;
|
|
25
|
+
tween_obj.current_step += 1;
|
|
26
|
+
if (tween_obj.current_step >= tween_obj.num_step) {
|
|
27
|
+
// This object has finished its tweening
|
|
28
|
+
// Set every property to its target value
|
|
29
|
+
for (const [property, tween_prop] of tween_obj.property_list) {
|
|
30
|
+
target_obj[property] = tween_prop.target_value;
|
|
31
|
+
}
|
|
32
|
+
// Call the complete callback
|
|
33
|
+
const callback = tween_obj.callback;
|
|
34
|
+
callback?.();
|
|
35
|
+
// Remove this tween
|
|
36
|
+
this._tween_queue.delete(tweenId);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
// Set the properties to the current value
|
|
40
|
+
for (const [property, tween_prop] of tween_obj.property_list) {
|
|
41
|
+
target_obj[property] = lerp(tween_prop.start_value, tween_prop.target_value, tween_obj.easing(tween_obj.current_step / tween_obj.num_step));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
// If there are no more tweens in the queue, this callback can be stopped
|
|
46
|
+
if (this._tween_queue.size === 0) {
|
|
47
|
+
this._tweening = false;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
registerCallback(options) {
|
|
52
|
+
if (this._callbackId === null) {
|
|
53
|
+
const playground = this._playground.deref();
|
|
54
|
+
if (playground !== undefined) {
|
|
55
|
+
this._callbackId = playground.registerCallback(this._tweenStep);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
if (typeof console !== "undefined" &&
|
|
59
|
+
options?.suppressWarning === false) {
|
|
60
|
+
console.warn("playground has been garbage collected");
|
|
61
|
+
console.trace();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
if (typeof console !== "undefined" &&
|
|
67
|
+
options?.suppressWarning === false) {
|
|
68
|
+
console.warn("Callback already registered");
|
|
69
|
+
console.trace();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
removeCallback(options) {
|
|
75
|
+
const playground = this._playground.deref();
|
|
76
|
+
if (playground !== undefined) {
|
|
77
|
+
playground.removeCallback(this._callbackId, options);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
if (typeof console !== "undefined" &&
|
|
81
|
+
options?.suppressWarning === false) {
|
|
82
|
+
console.warn("playground has been garbage collected");
|
|
83
|
+
console.trace();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
this._callbackId = null;
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
_onClearCallbacks = () => {
|
|
90
|
+
if (this._callbackId !== null) {
|
|
91
|
+
this._callbackId = null;
|
|
92
|
+
this.registerCallback();
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
tween(target_obj, properties, options) {
|
|
96
|
+
const playground = this._playground.deref();
|
|
97
|
+
if (playground !== undefined) {
|
|
98
|
+
const duration = (() => {
|
|
99
|
+
const duration = options?.duration;
|
|
100
|
+
if (typeof duration === "string") {
|
|
101
|
+
return speeds[duration];
|
|
102
|
+
}
|
|
103
|
+
else if (duration === 0) {
|
|
104
|
+
return 0;
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
return duration || speeds._default;
|
|
108
|
+
}
|
|
109
|
+
})();
|
|
110
|
+
const easing = options?.easing ?? swing;
|
|
111
|
+
const tween_obj = {
|
|
112
|
+
target_obj: target_obj,
|
|
113
|
+
current_step: 0,
|
|
114
|
+
num_step: playground.framesFromMs(duration),
|
|
115
|
+
easing: easing,
|
|
116
|
+
property_list: new Map(),
|
|
117
|
+
};
|
|
118
|
+
if (options?.callback !== undefined) {
|
|
119
|
+
tween_obj.callback = options.callback;
|
|
120
|
+
}
|
|
121
|
+
const property_list = tween_obj.property_list;
|
|
122
|
+
for (const [property, value] of Object.entries(properties)) {
|
|
123
|
+
property_list.set(property, {
|
|
124
|
+
start_value: target_obj[property],
|
|
125
|
+
target_value: value,
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
const tweenId = this._nextId;
|
|
129
|
+
this._nextId += 1;
|
|
130
|
+
this._tween_queue.set(tweenId, tween_obj);
|
|
131
|
+
this._tweening = true;
|
|
132
|
+
return tweenId;
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
if (typeof console !== "undefined" &&
|
|
136
|
+
options?.suppressWarning === false) {
|
|
137
|
+
console.warn("playground has been garbage collected");
|
|
138
|
+
console.trace();
|
|
139
|
+
}
|
|
140
|
+
return -1;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
removeTween(tweenId, options) {
|
|
144
|
+
if (tweenId === null) {
|
|
145
|
+
if (typeof console !== "undefined" &&
|
|
146
|
+
options?.suppressWarning === false) {
|
|
147
|
+
console.warn("tweenId is null");
|
|
148
|
+
console.trace();
|
|
149
|
+
}
|
|
150
|
+
return this;
|
|
151
|
+
}
|
|
152
|
+
const deleted = this._tween_queue.delete(tweenId);
|
|
153
|
+
if (!deleted &&
|
|
154
|
+
typeof console !== "undefined" &&
|
|
155
|
+
options?.suppressWarning === false) {
|
|
156
|
+
console.warn("No tweens removed");
|
|
157
|
+
console.trace();
|
|
158
|
+
}
|
|
159
|
+
return this;
|
|
160
|
+
}
|
|
161
|
+
clearTweens() {
|
|
162
|
+
this._tween_queue.clear();
|
|
163
|
+
this.dispatchEvent(new Event("clearTweens"));
|
|
164
|
+
return this;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export declare function linear(t: number): number;
|
|
2
|
+
export declare function swing(t: number): number;
|
|
3
|
+
export declare function easeInQuad(t: number): number;
|
|
4
|
+
export declare function easeOutQuad(t: number): number;
|
|
5
|
+
export declare function easeInOutQuad(t: number): number;
|
|
6
|
+
export declare function easeInCubic(t: number): number;
|
|
7
|
+
export declare function easeOutCubic(t: number): number;
|
|
8
|
+
export declare function easeInOutCubic(t: number): number;
|
|
9
|
+
export declare function easeInQuart(t: number): number;
|
|
10
|
+
export declare function easeOutQuart(t: number): number;
|
|
11
|
+
export declare function easeInOutQuart(t: number): number;
|
|
12
|
+
export declare function easeInQuint(t: number): number;
|
|
13
|
+
export declare function easeOutQuint(t: number): number;
|
|
14
|
+
export declare function easeInOutQuint(t: number): number;
|
|
15
|
+
export declare function easeInSine(t: number): number;
|
|
16
|
+
export declare function easeOutSine(t: number): number;
|
|
17
|
+
export declare function easeInOutSine(t: number): number;
|
|
18
|
+
export declare function easeInExpo(t: number): number;
|
|
19
|
+
export declare function easeOutExpo(t: number): number;
|
|
20
|
+
export declare function easeInOutExpo(t: number): number;
|
|
21
|
+
export declare function easeInCirc(t: number): number;
|
|
22
|
+
export declare function easeOutCirc(t: number): number;
|
|
23
|
+
export declare function easeInOutCirc(t: number): number;
|
|
24
|
+
export declare function easeInElastic(t: number): number;
|
|
25
|
+
export declare function easeOutElastic(t: number): number;
|
|
26
|
+
export declare function easeInOutElastic(t: number): number;
|
|
27
|
+
export declare function easeInBack(t: number): number;
|
|
28
|
+
export declare function easeOutBack(t: number): number;
|
|
29
|
+
export declare function easeInOutBack(t: number): number;
|
|
30
|
+
export declare function easeInBounce(t: number): number;
|
|
31
|
+
export declare function easeOutBounce(t: number): number;
|
|
32
|
+
export declare function easeInOutBounce(t: number): number;
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
// biome-ignore-all lint/style/noParameterAssign: reason
|
|
2
|
+
export function linear(t) {
|
|
3
|
+
return t;
|
|
4
|
+
}
|
|
5
|
+
export function swing(t) {
|
|
6
|
+
return 0.5 - Math.cos(t * Math.PI) / 2;
|
|
7
|
+
}
|
|
8
|
+
export function easeInQuad(t) {
|
|
9
|
+
return t * t;
|
|
10
|
+
}
|
|
11
|
+
export function easeOutQuad(t) {
|
|
12
|
+
return -(t * (t - 2));
|
|
13
|
+
}
|
|
14
|
+
export function easeInOutQuad(t) {
|
|
15
|
+
t *= 2;
|
|
16
|
+
if (t < 1) {
|
|
17
|
+
return (t * t) / 2;
|
|
18
|
+
}
|
|
19
|
+
t -= 1;
|
|
20
|
+
return -(t * (t - 2) - 1) / 2;
|
|
21
|
+
}
|
|
22
|
+
export function easeInCubic(t) {
|
|
23
|
+
return t * t * t;
|
|
24
|
+
}
|
|
25
|
+
export function easeOutCubic(t) {
|
|
26
|
+
t -= 1;
|
|
27
|
+
return t * t * t + 1;
|
|
28
|
+
}
|
|
29
|
+
export function easeInOutCubic(t) {
|
|
30
|
+
t *= 2;
|
|
31
|
+
if (t < 1) {
|
|
32
|
+
return (t * t * t) / 2;
|
|
33
|
+
}
|
|
34
|
+
t -= 2;
|
|
35
|
+
return (t * t * t + 2) / 2;
|
|
36
|
+
}
|
|
37
|
+
export function easeInQuart(t) {
|
|
38
|
+
return t * t * t * t;
|
|
39
|
+
}
|
|
40
|
+
export function easeOutQuart(t) {
|
|
41
|
+
t -= 1;
|
|
42
|
+
return -(t * t * t * t - 1);
|
|
43
|
+
}
|
|
44
|
+
export function easeInOutQuart(t) {
|
|
45
|
+
t *= 2;
|
|
46
|
+
if (t < 1) {
|
|
47
|
+
return (t * t * t * t) / 2;
|
|
48
|
+
}
|
|
49
|
+
t -= 2;
|
|
50
|
+
return -(t * t * t * t - 2) / 2;
|
|
51
|
+
}
|
|
52
|
+
export function easeInQuint(t) {
|
|
53
|
+
return t * t * t * t * t;
|
|
54
|
+
}
|
|
55
|
+
export function easeOutQuint(t) {
|
|
56
|
+
t -= 1;
|
|
57
|
+
return t * t * t * t * t + 1;
|
|
58
|
+
}
|
|
59
|
+
export function easeInOutQuint(t) {
|
|
60
|
+
t *= 2;
|
|
61
|
+
if (t < 1) {
|
|
62
|
+
return (t * t * t * t * t) / 2;
|
|
63
|
+
}
|
|
64
|
+
t -= 2;
|
|
65
|
+
return (t * t * t * t * t + 2) / 2;
|
|
66
|
+
}
|
|
67
|
+
export function easeInSine(t) {
|
|
68
|
+
return -Math.cos(t * (Math.PI / 2)) + 1;
|
|
69
|
+
}
|
|
70
|
+
export function easeOutSine(t) {
|
|
71
|
+
return Math.sin(t * (Math.PI / 2));
|
|
72
|
+
}
|
|
73
|
+
export function easeInOutSine(t) {
|
|
74
|
+
return -(Math.cos(Math.PI * t) - 1) / 2;
|
|
75
|
+
}
|
|
76
|
+
export function easeInExpo(t) {
|
|
77
|
+
return t === 0 ? 0 : Math.pow(2, 10 * (t - 1));
|
|
78
|
+
}
|
|
79
|
+
export function easeOutExpo(t) {
|
|
80
|
+
return t === 1 ? 1 : -Math.pow(2, -10 * t) + 1;
|
|
81
|
+
}
|
|
82
|
+
export function easeInOutExpo(t) {
|
|
83
|
+
if (t === 0) {
|
|
84
|
+
return 0;
|
|
85
|
+
}
|
|
86
|
+
if (t === 1) {
|
|
87
|
+
return 1;
|
|
88
|
+
}
|
|
89
|
+
t *= 2;
|
|
90
|
+
if (t < 1) {
|
|
91
|
+
return Math.pow(2, 10 * (t - 1)) / 2;
|
|
92
|
+
}
|
|
93
|
+
t -= 1;
|
|
94
|
+
return (-Math.pow(2, -10 * t) + 2) / 2;
|
|
95
|
+
}
|
|
96
|
+
export function easeInCirc(t) {
|
|
97
|
+
return -(Math.sqrt(1 - t * t) - 1);
|
|
98
|
+
}
|
|
99
|
+
export function easeOutCirc(t) {
|
|
100
|
+
t -= 1;
|
|
101
|
+
return Math.sqrt(1 - t * t);
|
|
102
|
+
}
|
|
103
|
+
export function easeInOutCirc(t) {
|
|
104
|
+
t *= 2;
|
|
105
|
+
if (t < 1) {
|
|
106
|
+
return -(Math.sqrt(1 - t * t) - 1) / 2;
|
|
107
|
+
}
|
|
108
|
+
t -= 2;
|
|
109
|
+
return (Math.sqrt(1 - t * t) + 1) / 2;
|
|
110
|
+
}
|
|
111
|
+
export function easeInElastic(t) {
|
|
112
|
+
const p = 0.3;
|
|
113
|
+
const s = p / 4;
|
|
114
|
+
if (t === 0) {
|
|
115
|
+
return 0;
|
|
116
|
+
}
|
|
117
|
+
if (t === 1) {
|
|
118
|
+
return 1;
|
|
119
|
+
}
|
|
120
|
+
t -= 1;
|
|
121
|
+
return -(Math.pow(2, 10 * t) * Math.sin(((t - s) * (2 * Math.PI)) / p));
|
|
122
|
+
}
|
|
123
|
+
export function easeOutElastic(t) {
|
|
124
|
+
const p = 0.3;
|
|
125
|
+
const s = p / 4;
|
|
126
|
+
if (t === 0) {
|
|
127
|
+
return 0;
|
|
128
|
+
}
|
|
129
|
+
if (t === 1) {
|
|
130
|
+
return 1;
|
|
131
|
+
}
|
|
132
|
+
return Math.pow(2, -10 * t) * Math.sin(((t - s) * (2 * Math.PI)) / p) + 1;
|
|
133
|
+
}
|
|
134
|
+
export function easeInOutElastic(t) {
|
|
135
|
+
const p = 0.45;
|
|
136
|
+
const s = p / 4;
|
|
137
|
+
if (t === 0) {
|
|
138
|
+
return 0;
|
|
139
|
+
}
|
|
140
|
+
t *= 2;
|
|
141
|
+
if (t === 2) {
|
|
142
|
+
return 1;
|
|
143
|
+
}
|
|
144
|
+
if (t < 1) {
|
|
145
|
+
t -= 1;
|
|
146
|
+
return -(Math.pow(2, 10 * t) * Math.sin(((t - s) * (2 * Math.PI)) / p)) / 2;
|
|
147
|
+
}
|
|
148
|
+
t -= 1;
|
|
149
|
+
return ((Math.pow(2, -10 * t) * Math.sin(((t - s) * (2 * Math.PI)) / p)) / 2 + 1);
|
|
150
|
+
}
|
|
151
|
+
export function easeInBack(t) {
|
|
152
|
+
const s = 1.70158;
|
|
153
|
+
return t * t * ((s + 1) * t - s);
|
|
154
|
+
}
|
|
155
|
+
export function easeOutBack(t) {
|
|
156
|
+
const s = 1.70158;
|
|
157
|
+
t -= 1;
|
|
158
|
+
return t * t * ((s + 1) * t + s) + 1;
|
|
159
|
+
}
|
|
160
|
+
export function easeInOutBack(t) {
|
|
161
|
+
const s = 1.70158 * 1.525;
|
|
162
|
+
t *= 2;
|
|
163
|
+
if (t < 1) {
|
|
164
|
+
return (t * t * ((s + 1) * t - s)) / 2;
|
|
165
|
+
}
|
|
166
|
+
t -= 2;
|
|
167
|
+
return (t * t * ((s + 1) * t + s) + 2) / 2;
|
|
168
|
+
}
|
|
169
|
+
export function easeInBounce(t) {
|
|
170
|
+
return 1 - easeOutBounce(1 - t);
|
|
171
|
+
}
|
|
172
|
+
export function easeOutBounce(t) {
|
|
173
|
+
if (t < 1 / 2.75) {
|
|
174
|
+
return 7.5625 * t * t;
|
|
175
|
+
}
|
|
176
|
+
if (t < 2 / 2.75) {
|
|
177
|
+
t -= 1.5 / 2.75;
|
|
178
|
+
return 7.5625 * (t * t) + 0.75;
|
|
179
|
+
}
|
|
180
|
+
if (t < 2.5 / 2.75) {
|
|
181
|
+
t -= 2.25 / 2.75;
|
|
182
|
+
return 7.5625 * (t * t) + 0.9375;
|
|
183
|
+
}
|
|
184
|
+
t -= 2.625 / 2.75;
|
|
185
|
+
return 7.5625 * (t * t) + 0.984375;
|
|
186
|
+
}
|
|
187
|
+
export function easeInOutBounce(t) {
|
|
188
|
+
if (t < 0.5) {
|
|
189
|
+
return easeInBounce(t * 2) / 2;
|
|
190
|
+
}
|
|
191
|
+
return easeOutBounce(t * 2 - 1) / 2 + 0.5;
|
|
192
|
+
}
|