Skip to content

Commit 7c02336

Browse files
committed
Fixed control ref; type changes to anim
1 parent a45a270 commit 7c02336

File tree

6 files changed

+74
-34
lines changed

6 files changed

+74
-34
lines changed

demo/components/AnimationControls.vue

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@
1919
<input
2020
type="string"
2121
:value="reverseCSSTime(animation.options.delay)"
22-
@change="
23-
(e) =>
24-
(animation.options.delay = parseCSSTime(e.target.value))
25-
"
22+
@change="(e) => animation.updateDelay(e.target.value)"
2623
/>
2724

2825
<label>Iteration Count</label>
@@ -106,8 +103,11 @@
106103
xmlns="http://www.w3.org/2000/svg"
107104
></svg>
108105

109-
<label class="preset-label" @click="copyToClipboard">{{ cubicBezierPreset }}<div ref="copyTextEl" class="info">Copied!</div>
110-
<font-awesome-icon :icon="['fas', 'clipboard']" /></label>
106+
<label class="preset-label" @click="copyToClipboard"
107+
>{{ cubicBezierPreset }}
108+
<div ref="copyTextEl" class="info">Copied!</div>
109+
<font-awesome-icon :icon="['fas', 'clipboard']"
110+
/></label>
111111

112112
<input
113113
@input="updateTimingFunction"
@@ -148,9 +148,9 @@
148148
class="slider"
149149
type="range"
150150
:min="0"
151-
:disabled="!animation.paused"
152151
:max="animation.options.duration"
153152
@input="sliderUpdate"
153+
:disabled="!animation.paused"
154154
v-model.number="animation.t"
155155
/>
156156
<div class="timing">
@@ -209,7 +209,7 @@
209209
</template>
210210

211211
<script setup lang="ts">
212-
import { computed, onMounted } from "vue";
212+
import { computed, onMounted, reactive } from "vue";
213213
import {
214214
Animation,
215215
CSSKeyframesToString,
@@ -238,7 +238,7 @@ let timingFunctionsAnd = {
238238
...timingFunctions,
239239
};
240240
241-
const { animation, isGrouped } = defineProps({
241+
const { animation: tmpAnimation, isGrouped } = defineProps({
242242
isGrouped: {
243243
type: Boolean,
244244
required: false,
@@ -250,6 +250,8 @@ const { animation, isGrouped } = defineProps({
250250
},
251251
});
252252
253+
const animation = $ref(tmpAnimation);
254+
253255
const emit = defineEmits<{
254256
(
255257
e: "sliderUpdate",
@@ -291,31 +293,38 @@ let jumpTerm = $ref("jump-none");
291293
let steps = $ref(10);
292294
293295
let cubicBezierPreset = $ref("ease");
294-
let cubicBezierValues = $ref(bezierPresets[cubicBezierPreset]);
296+
let cubicBezierValues = $ref(
297+
bezierPresets[cubicBezierPreset] as [number, number, number, number]
298+
);
295299
const svgCubicBezierEl = $ref(null);
296300
297301
const updateTimingFunction = () => {
298302
let timingFunction = timingFunctions[timingFunctionKey];
299303
if (timingFunctionKey === "steps") {
300304
timingFunction = timingFunctions[timingFunctionKey](steps, jumpTerm);
301305
} else if (timingFunctionKey === "cubicBezier") {
302-
timingFunction = CSSBezier(...cubicBezierValues);
306+
timingFunction = CSSBezier(
307+
...(cubicBezierValues as [number, number, number, number])
308+
);
303309
cubicBezierPreset = `cubic-bezier(${cubicBezierValues
304310
.map((v) => v.toFixed(2))
305311
.join(",")})`;
312+
306313
const path = svgCubicBezier(
307314
cubicBezierValues[0],
308315
cubicBezierValues[1],
309316
cubicBezierValues[2],
310317
cubicBezierValues[3]
311318
);
319+
312320
svgCubicBezierEl.innerHTML = path;
313321
}
314322
315323
setTimingFunction(timingFunction);
316324
};
317325
318326
let prevT = $ref(0);
327+
319328
const toggle = () => {
320329
if (!animation.started && !isGrouped) {
321330
animation.play();
@@ -370,8 +379,8 @@ function onKeyDown(e: KeyboardEvent) {
370379
}
371380
372381
const parseCSSKeyframesStringEl = debounce((event: Event) => {
382+
// @ts-ignore
373383
const s: string = event.target.innerText;
374-
console.log(event.key);
375384
376385
const p = CSSAnimationKeyframes.Values.parse(s);
377386
@@ -508,7 +517,6 @@ input[type="range"] {
508517
grid-template-columns: auto auto;
509518
align-items: center;
510519
511-
512520
label {
513521
overflow: hidden;
514522
white-space: pre;
@@ -519,7 +527,6 @@ input[type="range"] {
519527
input {
520528
grid-column: span 2;
521529
margin: 0;
522-
523530
524531
background: linear-gradient(
525532
to right,

demo/components/AnimationControlsGroup.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const { animations } = defineProps<{
4646
}>();
4747
4848
const selectedAnimation = ref("");
49+
4950
const emit = defineEmits<{
5051
(e: "selectedAnimation", val: string): void;
5152
}>();

package-lock.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"scripts": {
1919
"dev": "vite --port 8080 --mode production",
2020
"build": "vite build --mode production",
21-
"gh-pages": "vite build --mode gh-pages"
21+
"gh-pages": "vite build --mode gh-pages",
22+
"test": "vitest"
2223
},
2324
"repository": {
2425
"type": "git",

src/animation.ts

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ export type Vars<T = any> = {
2929
};
3030

3131
type TransformFunction<V extends Vars> = (t: number, v: V) => void;
32-
type TimingFunction = (t: number) => number | keyof typeof timingFunctions;
32+
type TimingFunction = (t: number) => number;
33+
type TimingFunctionNames = keyof typeof timingFunctions;
3334

3435
export type Keyframe<V extends Vars> = [
3536
vars: V,
@@ -140,12 +141,12 @@ export function parseTemplateFrame<V extends Vars>(
140141
}
141142

142143
type AnimationOptions = {
143-
duration: number | string;
144+
duration: number;
144145
delay: number;
145146
iterationCount: number;
146147
direction: "normal" | "reverse" | "alternate" | "alternate-reverse";
147148
fillMode: "none" | "forwards" | "backwards" | "both";
148-
timingFunction: keyof typeof timingFunctions | TimingFunction;
149+
timingFunction: TimingFunction;
149150
};
150151

151152
const defaultOptions: AnimationOptions = {
@@ -157,14 +158,24 @@ const defaultOptions: AnimationOptions = {
157158
timingFunction: easeInOutCubic,
158159
};
159160

161+
type InputAnimationOptions = {
162+
duration: number | string;
163+
delay: number | string;
164+
iterationCount: number | "infinite";
165+
direction: typeof defaultOptions.direction;
166+
fillMode: typeof defaultOptions.fillMode;
167+
timingFunction: TimingFunction | TimingFunctionNames;
168+
};
169+
160170
const getTimingFunction = (
161-
timingFunction: TimingFunction
162-
): TimingFunction | typeof timingFunction | undefined => {
171+
timingFunction: TimingFunction | TimingFunctionNames | undefined
172+
): TimingFunction | undefined => {
163173
if (typeof timingFunction === "string") {
164-
return timingFunctions[timingFunction];
174+
return timingFunctions[timingFunction] as TimingFunction | undefined;
165175
} else if (timingFunction == null) {
166176
return undefined;
167177
}
178+
168179
return timingFunction;
169180
};
170181

@@ -193,21 +204,19 @@ export class Animation<V extends Vars> {
193204
paused: boolean = false;
194205

195206
constructor(
196-
options: Partial<AnimationOptions>,
207+
options: Partial<InputAnimationOptions>,
197208
public target: HTMLElement = document.documentElement
198209
) {
199-
this.options = { ...defaultOptions, ...options };
200-
201-
this.updateTimingFunction(this.options.timingFunction);
202-
this.updateDuration(this.options.duration);
203-
this.updateIterationCount(this.options.iterationCount);
210+
this.options = { ...defaultOptions, ...options } as AnimationOptions;
211+
212+
this.parseOptions(options);
204213
}
205214

206215
frame<K extends V>(
207216
start: number | string,
208217
vars: Partial<K>,
209218
transform?: TransformFunction<K>,
210-
timingFunction?: TimingFunction
219+
timingFunction?: TimingFunction | TimingFunctionNames
211220
): Animation<K> {
212221
start = typeof start === "number" ? String(start) + "%" : start;
213222
const parsedStart = CSSValueUnit.Value.tryParse(start)!;
@@ -266,7 +275,7 @@ export class Animation<V extends Vars> {
266275
return this;
267276
}
268277

269-
updateTimingFunction(timingFunction: TimingFunction) {
278+
updateTimingFunction(timingFunction: TimingFunction | TimingFunctionNames) {
270279
this.options.timingFunction = getTimingFunction(timingFunction);
271280
return this;
272281
}
@@ -301,6 +310,23 @@ export class Animation<V extends Vars> {
301310
return this;
302311
}
303312

313+
updateDelay(delay: number | string) {
314+
if (typeof delay === "string") {
315+
delay = parseCSSTime(delay);
316+
}
317+
this.options.delay = delay;
318+
return this;
319+
}
320+
321+
parseOptions(options: Partial<InputAnimationOptions>) {
322+
this.updateTimingFunction(options.timingFunction);
323+
this.updateDuration(options.duration);
324+
this.updateIterationCount(options.iterationCount);
325+
this.updateDelay(options.delay);
326+
327+
return this;
328+
}
329+
304330
parse() {
305331
this.transformVars().parseFrames();
306332
return this;
@@ -511,8 +537,11 @@ export class CSSKeyframesAnimation<V extends Vars> {
511537
targets: HTMLElement[];
512538
animation: Animation<V>;
513539

514-
constructor(options: Partial<AnimationOptions> = {}, ...targets: HTMLElement[]) {
515-
this.options = { ...defaultOptions, ...options };
540+
constructor(
541+
options: Partial<InputAnimationOptions> = {},
542+
...targets: HTMLElement[]
543+
) {
544+
this.options = { ...defaultOptions, ...options } as AnimationOptions;
516545
this.targets = targets;
517546
}
518547

src/units.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ export class ValueUnit<T = number> {
143143
if (this.unit !== other.unit) {
144144
const [left, right] = collapseNumericType(this, other, target);
145145
const value = lerp(t, left.value, right.value);
146+
146147
return new ValueUnit(value, left.unit, left.superType);
147148
} else if (this.unit === "color") {
148149
const value = lerpColor(
@@ -169,6 +170,7 @@ function lerpMany<T>(
169170
for (let i = 0; i < minLength; i++) {
170171
const l = left[i];
171172
const r = right[i];
173+
172174
arr.push(l.lerp(t, r, target));
173175
}
174176
return arr;
@@ -268,7 +270,7 @@ export function transformObject(input: any): TransformedVars {
268270
return p;
269271
}
270272
} else {
271-
return input;
273+
return input as ValueArray | FunctionValue;
272274
}
273275
};
274276

@@ -289,7 +291,6 @@ export function reverseTransformObject(
289291
return original;
290292
}
291293

292-
293294
for (let i = 0; i < keys.length; i++) {
294295
const k = keys[i];
295296
if (values !== undefined && i === keys.length - 1) {

0 commit comments

Comments
 (0)