@tremolo-ui/react 0.0.2 → 0.0.3
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/README.md +11 -0
- package/dist/cjs/index.d.ts +72 -7
- package/dist/cjs/index.js +320 -106
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/packages/react/tsconfig.tsbuildinfo +1 -0
- package/dist/cjs/types/packages/functions/components/NumberInput/type.d.ts +14 -0
- package/dist/cjs/types/packages/functions/components/NumberInput/type.d.ts.map +1 -0
- package/dist/cjs/types/packages/functions/components/Slider/type.d.ts +32 -0
- package/dist/cjs/types/packages/functions/components/Slider/type.d.ts.map +1 -0
- package/dist/cjs/types/{src → packages/functions}/math.d.ts +4 -0
- package/dist/cjs/types/packages/functions/math.d.ts.map +1 -0
- package/dist/cjs/types/packages/functions/types.d.ts.map +1 -0
- package/dist/cjs/types/packages/functions/util.d.ts +6 -0
- package/dist/cjs/types/packages/functions/util.d.ts.map +1 -0
- package/dist/cjs/types/packages/react/src/components/AnimationCanvas/canvas.d.ts.map +1 -0
- package/dist/cjs/types/packages/react/src/components/AnimationCanvas/index.d.ts.map +1 -0
- package/dist/cjs/types/{src → packages/react/src}/components/Knob/index.d.ts +1 -1
- package/dist/cjs/types/packages/react/src/components/Knob/index.d.ts.map +1 -0
- package/dist/{esm/types → cjs/types/packages/react}/src/components/NumberInput/index.d.ts +1 -1
- package/dist/cjs/types/packages/react/src/components/NumberInput/index.d.ts.map +1 -0
- package/dist/cjs/types/packages/react/src/components/Slider/Thumb.d.ts.map +1 -0
- package/dist/cjs/types/packages/react/src/components/Slider/Track.d.ts.map +1 -0
- package/dist/{esm/types → cjs/types/packages/react}/src/components/Slider/index.d.ts +3 -3
- package/dist/cjs/types/packages/react/src/components/Slider/index.d.ts.map +1 -0
- package/dist/cjs/types/packages/react/src/components/Slider/type.d.ts +6 -0
- package/dist/cjs/types/packages/react/src/components/Slider/type.d.ts.map +1 -0
- package/dist/cjs/types/packages/react/src/hooks/useCallbackRef.d.ts.map +1 -0
- package/dist/cjs/types/packages/react/src/hooks/useEventListener.d.ts.map +1 -0
- package/dist/cjs/types/packages/react/src/hooks/useRefCallbackEvent.d.ts.map +1 -0
- package/dist/cjs/types/packages/react/src/index.d.ts +7 -0
- package/dist/cjs/types/packages/react/src/index.d.ts.map +1 -0
- package/dist/cjs/types/packages/react/src/system/pseudo.d.ts.map +1 -0
- package/dist/esm/index.d.ts +72 -7
- package/dist/esm/index.js +259 -53
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/packages/react/tsconfig.tsbuildinfo +1 -0
- package/dist/esm/types/packages/functions/components/NumberInput/type.d.ts +14 -0
- package/dist/esm/types/packages/functions/components/NumberInput/type.d.ts.map +1 -0
- package/dist/esm/types/packages/functions/components/Slider/type.d.ts +32 -0
- package/dist/esm/types/packages/functions/components/Slider/type.d.ts.map +1 -0
- package/dist/esm/types/{src → packages/functions}/math.d.ts +4 -0
- package/dist/esm/types/packages/functions/math.d.ts.map +1 -0
- package/dist/esm/types/packages/functions/types.d.ts.map +1 -0
- package/dist/esm/types/packages/functions/util.d.ts +6 -0
- package/dist/esm/types/packages/functions/util.d.ts.map +1 -0
- package/dist/esm/types/packages/react/src/components/AnimationCanvas/canvas.d.ts.map +1 -0
- package/dist/esm/types/packages/react/src/components/AnimationCanvas/index.d.ts.map +1 -0
- package/dist/esm/types/{src → packages/react/src}/components/Knob/index.d.ts +1 -1
- package/dist/esm/types/packages/react/src/components/Knob/index.d.ts.map +1 -0
- package/dist/{cjs/types → esm/types/packages/react}/src/components/NumberInput/index.d.ts +1 -1
- package/dist/esm/types/packages/react/src/components/NumberInput/index.d.ts.map +1 -0
- package/dist/esm/types/packages/react/src/components/Slider/Thumb.d.ts.map +1 -0
- package/dist/esm/types/packages/react/src/components/Slider/Track.d.ts.map +1 -0
- package/dist/{cjs/types → esm/types/packages/react}/src/components/Slider/index.d.ts +3 -3
- package/dist/esm/types/packages/react/src/components/Slider/index.d.ts.map +1 -0
- package/dist/esm/types/packages/react/src/components/Slider/type.d.ts +6 -0
- package/dist/esm/types/packages/react/src/components/Slider/type.d.ts.map +1 -0
- package/dist/esm/types/packages/react/src/hooks/useCallbackRef.d.ts.map +1 -0
- package/dist/esm/types/packages/react/src/hooks/useEventListener.d.ts.map +1 -0
- package/dist/esm/types/packages/react/src/hooks/useRefCallbackEvent.d.ts.map +1 -0
- package/dist/esm/types/packages/react/src/index.d.ts +7 -0
- package/dist/esm/types/packages/react/src/index.d.ts.map +1 -0
- package/dist/esm/types/packages/react/src/system/pseudo.d.ts.map +1 -0
- package/package.json +9 -7
- package/dist/cjs/types/src/components/AnimationCanvas/canvas.d.ts.map +0 -1
- package/dist/cjs/types/src/components/AnimationCanvas/index.d.ts.map +0 -1
- package/dist/cjs/types/src/components/Knob/index.d.ts.map +0 -1
- package/dist/cjs/types/src/components/NumberInput/index.d.ts.map +0 -1
- package/dist/cjs/types/src/components/Slider/Thumb.d.ts.map +0 -1
- package/dist/cjs/types/src/components/Slider/Track.d.ts.map +0 -1
- package/dist/cjs/types/src/components/Slider/index.d.ts.map +0 -1
- package/dist/cjs/types/src/components/Slider/type.d.ts +0 -6
- package/dist/cjs/types/src/components/Slider/type.d.ts.map +0 -1
- package/dist/cjs/types/src/hooks/useCallbackRef.d.ts.map +0 -1
- package/dist/cjs/types/src/hooks/useEventListener.d.ts.map +0 -1
- package/dist/cjs/types/src/hooks/useRefCallbackEvent.d.ts.map +0 -1
- package/dist/cjs/types/src/index.d.ts +0 -5
- package/dist/cjs/types/src/index.d.ts.map +0 -1
- package/dist/cjs/types/src/math.d.ts.map +0 -1
- package/dist/cjs/types/src/system/pseudo.d.ts.map +0 -1
- package/dist/cjs/types/src/types.d.ts.map +0 -1
- package/dist/cjs/types/src/util.d.ts +0 -2
- package/dist/cjs/types/src/util.d.ts.map +0 -1
- package/dist/esm/types/src/components/AnimationCanvas/canvas.d.ts.map +0 -1
- package/dist/esm/types/src/components/AnimationCanvas/index.d.ts.map +0 -1
- package/dist/esm/types/src/components/Knob/index.d.ts.map +0 -1
- package/dist/esm/types/src/components/NumberInput/index.d.ts.map +0 -1
- package/dist/esm/types/src/components/Slider/Thumb.d.ts.map +0 -1
- package/dist/esm/types/src/components/Slider/Track.d.ts.map +0 -1
- package/dist/esm/types/src/components/Slider/index.d.ts.map +0 -1
- package/dist/esm/types/src/components/Slider/type.d.ts +0 -6
- package/dist/esm/types/src/components/Slider/type.d.ts.map +0 -1
- package/dist/esm/types/src/hooks/useCallbackRef.d.ts.map +0 -1
- package/dist/esm/types/src/hooks/useEventListener.d.ts.map +0 -1
- package/dist/esm/types/src/hooks/useRefCallbackEvent.d.ts.map +0 -1
- package/dist/esm/types/src/index.d.ts +0 -5
- package/dist/esm/types/src/index.d.ts.map +0 -1
- package/dist/esm/types/src/math.d.ts.map +0 -1
- package/dist/esm/types/src/system/pseudo.d.ts.map +0 -1
- package/dist/esm/types/src/types.d.ts.map +0 -1
- package/dist/esm/types/src/util.d.ts +0 -2
- package/dist/esm/types/src/util.d.ts.map +0 -1
- /package/dist/cjs/types/{src → packages/functions}/types.d.ts +0 -0
- /package/dist/cjs/types/{src → packages/react/src}/components/AnimationCanvas/canvas.d.ts +0 -0
- /package/dist/cjs/types/{src → packages/react/src}/components/AnimationCanvas/index.d.ts +0 -0
- /package/dist/cjs/types/{src → packages/react/src}/components/Slider/Thumb.d.ts +0 -0
- /package/dist/cjs/types/{src → packages/react/src}/components/Slider/Track.d.ts +0 -0
- /package/dist/cjs/types/{src → packages/react/src}/hooks/useCallbackRef.d.ts +0 -0
- /package/dist/cjs/types/{src → packages/react/src}/hooks/useEventListener.d.ts +0 -0
- /package/dist/cjs/types/{src → packages/react/src}/hooks/useRefCallbackEvent.d.ts +0 -0
- /package/dist/cjs/types/{src → packages/react/src}/system/pseudo.d.ts +0 -0
- /package/dist/esm/types/{src → packages/functions}/types.d.ts +0 -0
- /package/dist/esm/types/{src → packages/react/src}/components/AnimationCanvas/canvas.d.ts +0 -0
- /package/dist/esm/types/{src → packages/react/src}/components/AnimationCanvas/index.d.ts +0 -0
- /package/dist/esm/types/{src → packages/react/src}/components/Slider/Thumb.d.ts +0 -0
- /package/dist/esm/types/{src → packages/react/src}/components/Slider/Track.d.ts +0 -0
- /package/dist/esm/types/{src → packages/react/src}/hooks/useCallbackRef.d.ts +0 -0
- /package/dist/esm/types/{src → packages/react/src}/hooks/useEventListener.d.ts +0 -0
- /package/dist/esm/types/{src → packages/react/src}/hooks/useRefCallbackEvent.d.ts +0 -0
- /package/dist/esm/types/{src → packages/react/src}/system/pseudo.d.ts +0 -0
package/dist/cjs/index.js
CHANGED
|
@@ -1,11 +1,59 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var math = require('common/math');
|
|
4
3
|
var jsxRuntime = require('@emotion/react/jsx-runtime');
|
|
5
4
|
var react = require('@emotion/react');
|
|
6
5
|
var React = require('react');
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
|
|
7
|
+
function clamp(value, min, max) {
|
|
8
|
+
return Math.max(min, Math.min(value, max));
|
|
9
|
+
}
|
|
10
|
+
function normalizeValue(rawValue, min, max, skew = 1) {
|
|
11
|
+
if (min >= max)
|
|
12
|
+
throw new RangeError('requirements: min < max');
|
|
13
|
+
const v = clamp((rawValue - min) / (max - min), 0, 1);
|
|
14
|
+
return Math.pow(v, skew);
|
|
15
|
+
}
|
|
16
|
+
function rawValue(normalizedValue, min, max, skew = 1) {
|
|
17
|
+
if (min >= max)
|
|
18
|
+
throw new RangeError('requirements: min < max');
|
|
19
|
+
const v = skew == 1
|
|
20
|
+
? clamp(normalizedValue, 0, 1)
|
|
21
|
+
: Math.exp(Math.log(clamp(normalizedValue, 0, 1)) / skew);
|
|
22
|
+
return min + v * (max - min);
|
|
23
|
+
}
|
|
24
|
+
function skewWithCenterValue(centerValue, min, max) {
|
|
25
|
+
if (!(min <= centerValue && centerValue <= max))
|
|
26
|
+
throw new RangeError('requirements: min <= centerValue <= max');
|
|
27
|
+
return Math.log(0.5) / Math.log((centerValue - min) / (max - min));
|
|
28
|
+
}
|
|
29
|
+
function stepValue(value, step) {
|
|
30
|
+
var _a;
|
|
31
|
+
if (step <= 0)
|
|
32
|
+
throw new RangeError('requirements: step > 0');
|
|
33
|
+
const quotient = Math.floor(value / step);
|
|
34
|
+
const decimalDigits = (_a = decimalPart(step)) === null || _a === void 0 ? void 0 : _a.length;
|
|
35
|
+
const v = toFixed(quotient * step, decimalDigits);
|
|
36
|
+
const next = toFixed((quotient + 1) * step, decimalDigits);
|
|
37
|
+
return Math.abs(value - v) < Math.abs(value - next) ? v : next;
|
|
38
|
+
}
|
|
39
|
+
function toFixed(x, fractionDigits) {
|
|
40
|
+
return Number(x.toFixed(fractionDigits));
|
|
41
|
+
}
|
|
42
|
+
function decimalPart(x) {
|
|
43
|
+
return String(x).split('.')[1];
|
|
44
|
+
}
|
|
45
|
+
function radian(degree) {
|
|
46
|
+
return (Math.PI * degree) / 180;
|
|
47
|
+
}
|
|
48
|
+
function degree(radian) {
|
|
49
|
+
return (180 * radian) / Math.PI;
|
|
50
|
+
}
|
|
51
|
+
function dbToGain(db) {
|
|
52
|
+
return Math.pow(10, db / 20);
|
|
53
|
+
}
|
|
54
|
+
function gainToDb(gain) {
|
|
55
|
+
return 20 * (Math.log(gain) / Math.LN10);
|
|
56
|
+
}
|
|
9
57
|
|
|
10
58
|
/******************************************************************************
|
|
11
59
|
Copyright (c) Microsoft Corporation.
|
|
@@ -41,53 +89,6 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
|
|
|
41
89
|
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
42
90
|
};
|
|
43
91
|
|
|
44
|
-
/**
|
|
45
|
-
* This hook is user-land implementation of the experimental `useEffectEvent` hook.
|
|
46
|
-
* React docs: https://react.dev/learn/separating-events-from-effects#declaring-an-effect-event
|
|
47
|
-
*/
|
|
48
|
-
function useCallbackRef(callback, deps = []) {
|
|
49
|
-
const callbackRef = React.useRef(() => {
|
|
50
|
-
throw new Error('Cannot call an event handler while rendering.');
|
|
51
|
-
});
|
|
52
|
-
React.useInsertionEffect(() => {
|
|
53
|
-
callbackRef.current = callback;
|
|
54
|
-
});
|
|
55
|
-
return React.useCallback((...args) => { var _a; return (_a = callbackRef.current) === null || _a === void 0 ? void 0 : _a.call(callbackRef, ...args); }, deps);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function useEventListener(target, event, handler, options) {
|
|
59
|
-
const listener = useCallbackRef(handler);
|
|
60
|
-
React.useEffect(() => {
|
|
61
|
-
const node = typeof target === 'function' ? target() : (target !== null && target !== void 0 ? target : document);
|
|
62
|
-
if (!handler || !node)
|
|
63
|
-
return;
|
|
64
|
-
node.addEventListener(event, listener, options);
|
|
65
|
-
return () => {
|
|
66
|
-
node.removeEventListener(event, listener, options);
|
|
67
|
-
};
|
|
68
|
-
}, [event, target, options, listener, handler]);
|
|
69
|
-
return () => {
|
|
70
|
-
const node = typeof target === 'function' ? target() : (target !== null && target !== void 0 ? target : document);
|
|
71
|
-
node === null || node === void 0 ? void 0 : node.removeEventListener(event, listener, options);
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function useRefCallbackEvent(event, handler, options, deps = []) {
|
|
76
|
-
return React.useMemo(() => {
|
|
77
|
-
let cleanup;
|
|
78
|
-
return (node) => {
|
|
79
|
-
if (!node) {
|
|
80
|
-
cleanup === null || cleanup === void 0 ? void 0 : cleanup();
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
node.addEventListener(event, handler, options);
|
|
84
|
-
cleanup = () => {
|
|
85
|
-
node.removeEventListener(event, handler, options);
|
|
86
|
-
};
|
|
87
|
-
};
|
|
88
|
-
}, deps);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
92
|
// https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/save
|
|
92
93
|
const drawingState = [
|
|
93
94
|
'strokeStyle',
|
|
@@ -200,6 +201,53 @@ function AnimationCanvas(_a) {
|
|
|
200
201
|
return (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx("canvas", Object.assign({ width: relativeSize ? 0 : width, height: relativeSize ? 0 : height, css: react.css(style), ref: canvasRef }, props)), relativeSize && reduceFlickering && (jsxRuntime.jsx("canvas", { style: { display: 'none' }, width: relativeSize ? 0 : width, height: relativeSize ? 0 : height, ref: memoCanvasRef }))] }));
|
|
201
202
|
}
|
|
202
203
|
|
|
204
|
+
/**
|
|
205
|
+
* This hook is user-land implementation of the experimental `useEffectEvent` hook.
|
|
206
|
+
* React docs: https://react.dev/learn/separating-events-from-effects#declaring-an-effect-event
|
|
207
|
+
*/
|
|
208
|
+
function useCallbackRef(callback, deps = []) {
|
|
209
|
+
const callbackRef = React.useRef(() => {
|
|
210
|
+
throw new Error('Cannot call an event handler while rendering.');
|
|
211
|
+
});
|
|
212
|
+
React.useInsertionEffect(() => {
|
|
213
|
+
callbackRef.current = callback;
|
|
214
|
+
});
|
|
215
|
+
return React.useCallback((...args) => { var _a; return (_a = callbackRef.current) === null || _a === void 0 ? void 0 : _a.call(callbackRef, ...args); }, deps);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function useEventListener(target, event, handler, options) {
|
|
219
|
+
const listener = useCallbackRef(handler);
|
|
220
|
+
React.useEffect(() => {
|
|
221
|
+
const node = typeof target === 'function' ? target() : (target !== null && target !== void 0 ? target : document);
|
|
222
|
+
if (!handler || !node)
|
|
223
|
+
return;
|
|
224
|
+
node.addEventListener(event, listener, options);
|
|
225
|
+
return () => {
|
|
226
|
+
node.removeEventListener(event, listener, options);
|
|
227
|
+
};
|
|
228
|
+
}, [event, target, options, listener, handler]);
|
|
229
|
+
return () => {
|
|
230
|
+
const node = typeof target === 'function' ? target() : (target !== null && target !== void 0 ? target : document);
|
|
231
|
+
node === null || node === void 0 ? void 0 : node.removeEventListener(event, listener, options);
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function useRefCallbackEvent(event, handler, options, deps = []) {
|
|
236
|
+
return React.useMemo(() => {
|
|
237
|
+
let cleanup;
|
|
238
|
+
return (node) => {
|
|
239
|
+
if (!node) {
|
|
240
|
+
cleanup === null || cleanup === void 0 ? void 0 : cleanup();
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
node.addEventListener(event, handler, options);
|
|
244
|
+
cleanup = () => {
|
|
245
|
+
node.removeEventListener(event, handler, options);
|
|
246
|
+
};
|
|
247
|
+
};
|
|
248
|
+
}, deps);
|
|
249
|
+
}
|
|
250
|
+
|
|
203
251
|
const defaultOptions = {
|
|
204
252
|
active: '#7998ec',
|
|
205
253
|
inactive: '#eee',
|
|
@@ -240,9 +288,9 @@ function Knob(_a) {
|
|
|
240
288
|
const screenY = isTouch ? event.touches[0].screenY : event.screenY;
|
|
241
289
|
const delta = dragOffsetY.current - screenY;
|
|
242
290
|
dragOffsetY.current = screenY;
|
|
243
|
-
const n =
|
|
244
|
-
const v =
|
|
245
|
-
const v2 =
|
|
291
|
+
const n = normalizeValue(value, min, max, skew);
|
|
292
|
+
const v = rawValue(n + delta / 100, min, max, skew);
|
|
293
|
+
const v2 = clamp(stepValue(v, step), min, max);
|
|
246
294
|
if (onChange)
|
|
247
295
|
onChange(v2);
|
|
248
296
|
}
|
|
@@ -255,14 +303,14 @@ function Knob(_a) {
|
|
|
255
303
|
const x = event.deltaY > 0 ? -enableWheel[1] : enableWheel[1];
|
|
256
304
|
let v;
|
|
257
305
|
if (enableWheel[0] == 'normalized') {
|
|
258
|
-
const n =
|
|
259
|
-
v =
|
|
306
|
+
const n = normalizeValue(value, min, max, skew);
|
|
307
|
+
v = rawValue(n + x, min, max, skew);
|
|
260
308
|
}
|
|
261
309
|
else {
|
|
262
310
|
v = value + x;
|
|
263
311
|
}
|
|
264
312
|
if (onChange)
|
|
265
|
-
onChange(
|
|
313
|
+
onChange(clamp(stepValue(v, step), min, max));
|
|
266
314
|
}, { passive: false }, [enableWheel, value, min, max, skew, step, onChange]);
|
|
267
315
|
const touchMoveRefCallback = useRefCallbackEvent('touchmove', handleEvent, { passive: false }, [value, min, max, skew, step, onChange, bodyNoSelect]);
|
|
268
316
|
useEventListener(window, 'mousemove', handleEvent);
|
|
@@ -289,11 +337,13 @@ function Knob(_a) {
|
|
|
289
337
|
// TODO
|
|
290
338
|
// reduceFlickering={false}
|
|
291
339
|
draw: (ctx, _w, _h) => {
|
|
292
|
-
const p =
|
|
293
|
-
const startP =
|
|
340
|
+
const p = normalizeValue(value, min, max, skew);
|
|
341
|
+
const startP = normalizeValue(startValue, min, max, skew);
|
|
294
342
|
const w = _w.current, cx = w / 2;
|
|
295
343
|
const h = _h.current, cy = h / 2;
|
|
296
344
|
const r = Math.min(cx, cy);
|
|
345
|
+
if (r <= 0)
|
|
346
|
+
return;
|
|
297
347
|
// reset
|
|
298
348
|
ctx.clearRect(0, 0, w, h);
|
|
299
349
|
ctx.lineCap = 'butt';
|
|
@@ -307,20 +357,20 @@ function Knob(_a) {
|
|
|
307
357
|
// inactive rotary
|
|
308
358
|
ctx.beginPath();
|
|
309
359
|
ctx.strokeStyle = opts.inactive;
|
|
310
|
-
ctx.arc(cx, cy, r - 0.5 - opts.lineWeight / 2,
|
|
360
|
+
ctx.arc(cx, cy, r - 0.5 - opts.lineWeight / 2, radian(135), radian(45));
|
|
311
361
|
ctx.stroke();
|
|
312
362
|
// active rotary
|
|
313
363
|
ctx.beginPath();
|
|
314
364
|
ctx.strokeStyle = opts.active;
|
|
315
|
-
ctx.arc(cx, cy, r - 0.5 - opts.lineWeight / 2,
|
|
365
|
+
ctx.arc(cx, cy, r - 0.5 - opts.lineWeight / 2, radian(135 + 270 * startP), radian(135 + 270 * p), p < startP);
|
|
316
366
|
ctx.stroke();
|
|
317
367
|
// thumb
|
|
318
368
|
ctx.beginPath();
|
|
319
369
|
ctx.moveTo(cx, cy);
|
|
320
370
|
ctx.lineTo(cx +
|
|
321
|
-
Math.cos(
|
|
371
|
+
Math.cos(radian(135 + 270 * p)) *
|
|
322
372
|
(r - 0.5 - opts.thumbWeight / 2), cy +
|
|
323
|
-
Math.sin(
|
|
373
|
+
Math.sin(radian(135 + 270 * p)) *
|
|
324
374
|
(r - 0.5 - opts.thumbWeight / 2));
|
|
325
375
|
ctx.strokeStyle = opts.thumb;
|
|
326
376
|
ctx.lineWidth = opts.thumbWeight;
|
|
@@ -328,6 +378,163 @@ function Knob(_a) {
|
|
|
328
378
|
} })] })));
|
|
329
379
|
}
|
|
330
380
|
|
|
381
|
+
function selectUnit(units, value) {
|
|
382
|
+
if (typeof units == 'string')
|
|
383
|
+
return [units, 1];
|
|
384
|
+
let i = 0;
|
|
385
|
+
for (; i < units.length; i++) {
|
|
386
|
+
if (Math.abs(units[i][1]) > Math.abs(value))
|
|
387
|
+
break;
|
|
388
|
+
}
|
|
389
|
+
return units[Math.max(0, i - 1)];
|
|
390
|
+
}
|
|
391
|
+
function parseValue(inputString, units, digit) {
|
|
392
|
+
if (!units) {
|
|
393
|
+
return {
|
|
394
|
+
rawValue: Number(inputString.trim()) || 0,
|
|
395
|
+
formatValue: inputString.trim(),
|
|
396
|
+
unit: '',
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
else if (typeof units == 'string') {
|
|
400
|
+
return {
|
|
401
|
+
rawValue: Number(inputString.trim()) || 0,
|
|
402
|
+
formatValue: `${inputString.trim()}${units}`,
|
|
403
|
+
unit: units,
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
const unitList = units.map(([u, _s]) => u);
|
|
407
|
+
const scaleList = units.map(([_u, s]) => s);
|
|
408
|
+
let rawValue = 0;
|
|
409
|
+
let formatValue = `${scaleList[0]}`;
|
|
410
|
+
let unit = unitList[0];
|
|
411
|
+
const m = inputString.trim().match(/^(-?\d+(\.\d+)?)\s*(\w*)$/);
|
|
412
|
+
if (m) {
|
|
413
|
+
rawValue = Number(m[1]) || 0;
|
|
414
|
+
const inputUnit = m[3];
|
|
415
|
+
if (inputUnit != '' && unitList.indexOf(inputUnit) != -1) {
|
|
416
|
+
rawValue *= scaleList[unitList.indexOf(inputUnit)];
|
|
417
|
+
const s = selectUnit(units, rawValue);
|
|
418
|
+
const v = rawValue / s[1];
|
|
419
|
+
formatValue = `${digit != undefined ? toFixed(v, digit) : v}${s[0]}`;
|
|
420
|
+
unit = s[0];
|
|
421
|
+
}
|
|
422
|
+
else {
|
|
423
|
+
const [u, scale] = selectUnit(units, rawValue);
|
|
424
|
+
const v = rawValue / scale;
|
|
425
|
+
formatValue = `${digit != undefined ? toFixed(v, digit) : v}${u}`;
|
|
426
|
+
unit = u;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
return {
|
|
430
|
+
rawValue: rawValue,
|
|
431
|
+
formatValue: formatValue,
|
|
432
|
+
unit: unit,
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Input with some useful functions for entering numerical values.
|
|
438
|
+
*/
|
|
439
|
+
function NumberInput(_a) {
|
|
440
|
+
var { value, min, // TODO
|
|
441
|
+
max, // TODO
|
|
442
|
+
step, // TODO
|
|
443
|
+
units, disabled = false, placeholder, readOnly, tabIndex, strict = false, digit, selectWithFocus = 'none', blurWithEnter = true, className, style, onChange, onFocus, onBlur } = _a, pseudo = __rest(_a, ["value", "min", "max", "step", "units", "disabled", "placeholder", "readOnly", "tabIndex", "strict", "digit", "selectWithFocus", "blurWithEnter", "className", "style", "onChange", "onFocus", "onBlur"]);
|
|
444
|
+
const [showValue, setShowValue] = React.useState(parseValue(String(value), units, digit).formatValue);
|
|
445
|
+
const calculatedStrict = units ? false : strict;
|
|
446
|
+
const { _active, _focus, _hover } = pseudo;
|
|
447
|
+
return (jsxRuntime.jsx("input", { className: 'tremolo-slider' + (className ? ` ${className}` : ''), value: showValue, type: calculatedStrict ? 'number' : 'text', spellCheck: 'false', disabled: disabled, placeholder: placeholder, readOnly: readOnly, tabIndex: tabIndex, css: react.css(Object.assign(Object.assign({ display: 'inline-block', height: '2rem', font: 'inherit', margin: 0, paddingLeft: 10, paddingRight: 10, outlineColor: 'transparent', outlineStyle: 'solid', outlineWidth: 1, background: 'inherit', appearance: 'none', borderStyle: 'solid', borderWidth: 1, borderColor: '#bbb', borderRadius: 4, '&:hover': Object.assign({ outlineColor: '#bbb' }, _hover), '&:focus': Object.assign({ outlineColor: '#4e76e6', outlineWidth: 2 }, _focus) }, (_active && { '&:active': Object.assign({}, _active) })), style)), onChange: (event) => {
|
|
448
|
+
const v = event.currentTarget.value;
|
|
449
|
+
setShowValue(v);
|
|
450
|
+
if (onChange)
|
|
451
|
+
onChange(parseValue(v, units, digit).rawValue, event);
|
|
452
|
+
}, onFocus: (event) => {
|
|
453
|
+
const v = event.currentTarget.value;
|
|
454
|
+
const parsed = parseValue(v, units, digit);
|
|
455
|
+
if (selectWithFocus == 'all') {
|
|
456
|
+
event.currentTarget.setSelectionRange(0, v.length);
|
|
457
|
+
}
|
|
458
|
+
else if (selectWithFocus == 'number') {
|
|
459
|
+
event.currentTarget.setSelectionRange(0, parsed.formatValue.length - parsed.unit.length);
|
|
460
|
+
}
|
|
461
|
+
if (onFocus)
|
|
462
|
+
onFocus(parsed.rawValue, event);
|
|
463
|
+
}, onBlur: (event) => {
|
|
464
|
+
// update value
|
|
465
|
+
const v = event.currentTarget.value;
|
|
466
|
+
const parsed = parseValue(v, units, digit);
|
|
467
|
+
setShowValue(parsed.formatValue);
|
|
468
|
+
if (onBlur)
|
|
469
|
+
onBlur(parsed.rawValue, event);
|
|
470
|
+
}, onKeyDown: (event) => {
|
|
471
|
+
if (blurWithEnter && event.key == 'Enter') {
|
|
472
|
+
event.currentTarget.blur();
|
|
473
|
+
}
|
|
474
|
+
} }));
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
function isHorizontal(d) {
|
|
478
|
+
return d == 'right' || d == 'left';
|
|
479
|
+
}
|
|
480
|
+
function isVertical(d) {
|
|
481
|
+
return d == 'up' || d == 'down';
|
|
482
|
+
}
|
|
483
|
+
function isReversed(d) {
|
|
484
|
+
return d == 'left' || d == 'up';
|
|
485
|
+
}
|
|
486
|
+
function gradientDirection(d) {
|
|
487
|
+
if (d == 'left' || d == 'right')
|
|
488
|
+
return d;
|
|
489
|
+
if (d == 'up')
|
|
490
|
+
return 'top';
|
|
491
|
+
if (d == 'down')
|
|
492
|
+
return 'bottom';
|
|
493
|
+
}
|
|
494
|
+
function parseScaleOrderList(scale, min, max, step) {
|
|
495
|
+
var _a;
|
|
496
|
+
let scalesList = [];
|
|
497
|
+
if (!scale) ;
|
|
498
|
+
else if (typeof scale[0] == 'object') {
|
|
499
|
+
scalesList = scale;
|
|
500
|
+
}
|
|
501
|
+
else {
|
|
502
|
+
const per = scale[0] == 'step' ? step : scale[0];
|
|
503
|
+
const count = Math.floor(max / per) - Math.ceil(min / per) + 1;
|
|
504
|
+
for (let i = 0; i < count; i++) {
|
|
505
|
+
const at = toFixed(per * (Math.ceil(min / per) + i), (_a = decimalPart(per)) === null || _a === void 0 ? void 0 : _a.length);
|
|
506
|
+
scalesList.push({ at: at, type: scale[1] });
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
return scalesList;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
function styleHelper(value, op, influencer) {
|
|
513
|
+
if (op && influencer) {
|
|
514
|
+
if (typeof value == 'number') {
|
|
515
|
+
if (op == '+')
|
|
516
|
+
return `${value + influencer}px`;
|
|
517
|
+
if (op == '-')
|
|
518
|
+
return `${value - influencer}px`;
|
|
519
|
+
if (op == '*')
|
|
520
|
+
return `${value * influencer}px`;
|
|
521
|
+
if (op == '/')
|
|
522
|
+
return `${value / influencer}px`;
|
|
523
|
+
}
|
|
524
|
+
else {
|
|
525
|
+
return `calc(${value}px ${op} ${influencer})`;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
else {
|
|
529
|
+
if (typeof value == 'number') {
|
|
530
|
+
return `${value}px`;
|
|
531
|
+
}
|
|
532
|
+
else {
|
|
533
|
+
return value;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
|
|
331
538
|
function SliderThumb({ color, children, style, __css, }) {
|
|
332
539
|
return (jsxRuntime.jsx("div", { className: "tremolo-slider-thumb-wrapper", css: react.css(Object.assign({ width: 'fit-content', height: 'fit-content', position: 'absolute', translate: '-50% -50%', zIndex: 100 }, __css)), children: children ? (children) : (
|
|
333
540
|
// default slider thumb
|
|
@@ -348,10 +555,10 @@ function Slider(_a) {
|
|
|
348
555
|
var { value, min, max, step = 1, skew = 1, length = 140, thickness = 10, direction = 'right', scale, scaleOption, color = '#4e76e6', bg = '#eee', className, style, bodyNoSelect = true, enableWheel, onChange, children } = _a; __rest(_a, ["value", "min", "max", "step", "skew", "length", "thickness", "direction", "scale", "scaleOption", "color", "bg", "className", "style", "bodyNoSelect", "enableWheel", "onChange", "children"]);
|
|
349
556
|
// -- state and ref ---
|
|
350
557
|
const trackElementRef = React.useRef(null);
|
|
351
|
-
const percent =
|
|
352
|
-
const percentRev =
|
|
353
|
-
const scalesList =
|
|
354
|
-
if (
|
|
558
|
+
const percent = normalizeValue(value, min, max, skew) * 100;
|
|
559
|
+
const percentRev = isReversed(direction) ? 100 - percent : percent;
|
|
560
|
+
const scalesList = parseScaleOrderList(scale, min, max, step);
|
|
561
|
+
if (isReversed(direction))
|
|
355
562
|
scalesList.reverse();
|
|
356
563
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
357
564
|
let trackProps;
|
|
@@ -387,11 +594,11 @@ function Slider(_a) {
|
|
|
387
594
|
const { left: x1, top: y1, right: x2, bottom: y2, } = trackElementRef.current.getBoundingClientRect();
|
|
388
595
|
const mouseX = isTouch ? event.touches[0].clientX : event.clientX;
|
|
389
596
|
const mouseY = isTouch ? event.touches[0].clientY : event.clientY;
|
|
390
|
-
const n =
|
|
391
|
-
?
|
|
392
|
-
:
|
|
393
|
-
const v =
|
|
394
|
-
const v2 =
|
|
597
|
+
const n = isHorizontal(direction)
|
|
598
|
+
? normalizeValue(mouseX, x1, x2)
|
|
599
|
+
: normalizeValue(mouseY, y1, y2);
|
|
600
|
+
const v = rawValue(isReversed(direction) ? 1 - n : n, min, max, skew);
|
|
601
|
+
const v2 = clamp(stepValue(v, step), min, max);
|
|
395
602
|
if (onChange)
|
|
396
603
|
onChange(v2);
|
|
397
604
|
};
|
|
@@ -402,18 +609,18 @@ function Slider(_a) {
|
|
|
402
609
|
return;
|
|
403
610
|
event.preventDefault();
|
|
404
611
|
let x = event.deltaY > 0 ? enableWheel[1] : -enableWheel[1];
|
|
405
|
-
if (
|
|
612
|
+
if (isReversed(direction) || isHorizontal(direction))
|
|
406
613
|
x *= -1;
|
|
407
614
|
let v;
|
|
408
615
|
if (enableWheel[0] == 'normalized') {
|
|
409
|
-
const n =
|
|
410
|
-
v =
|
|
616
|
+
const n = normalizeValue(value, min, max, skew);
|
|
617
|
+
v = rawValue(n + x, min, max, skew);
|
|
411
618
|
}
|
|
412
619
|
else {
|
|
413
620
|
v = value + x;
|
|
414
621
|
}
|
|
415
622
|
if (onChange)
|
|
416
|
-
onChange(
|
|
623
|
+
onChange(clamp(stepValue(v, step), min, max));
|
|
417
624
|
}, {
|
|
418
625
|
passive: false,
|
|
419
626
|
}, [enableWheel, value, min, max, skew, step]);
|
|
@@ -428,7 +635,7 @@ function Slider(_a) {
|
|
|
428
635
|
return (jsxRuntime.jsxs("div", { className: 'tremolo-slider' + (className ? ` ${className}` : ''), ref: (div) => {
|
|
429
636
|
wheelRefCallback(div);
|
|
430
637
|
touchMoveRefCallback(div);
|
|
431
|
-
}, tabIndex: -1, role: "slider", "aria-valuenow": value, "aria-valuemax": max, "aria-valuemin": min, "aria-orientation":
|
|
638
|
+
}, tabIndex: -1, role: "slider", "aria-valuenow": value, "aria-valuemax": max, "aria-valuemin": min, "aria-orientation": isHorizontal(direction) ? 'horizontal' : 'vertical', css: react.css(Object.assign({ display: 'inline-block', boxSizing: 'border-box', margin: '0.7rem', padding: 0, cursor: 'pointer' }, style)), onPointerDown: (event) => {
|
|
432
639
|
thumbDragged.current = true;
|
|
433
640
|
handleValue(event);
|
|
434
641
|
}, children: [jsxRuntime.jsx(react.Global, { styles: {
|
|
@@ -437,54 +644,54 @@ function Slider(_a) {
|
|
|
437
644
|
},
|
|
438
645
|
} }), jsxRuntime.jsxs("div", { css: react.css({
|
|
439
646
|
display: 'flex',
|
|
440
|
-
flexDirection:
|
|
647
|
+
flexDirection: isHorizontal(direction) ? 'column' : 'row',
|
|
441
648
|
}), children: [jsxRuntime.jsxs("div", { className: "tremolo-slider-track-wrapper", ref: trackElementRef, css: react.css({ position: 'relative' }), children: [jsxRuntime.jsx(SliderTrack, Object.assign({ __css: {
|
|
442
|
-
background: `linear-gradient(to ${
|
|
443
|
-
borderRadius:
|
|
649
|
+
background: `linear-gradient(to ${gradientDirection(direction)}, ${color} ${percent}%, ${bg} ${percent}%)`,
|
|
650
|
+
borderRadius: styleHelper(thickness, '/', 2),
|
|
444
651
|
borderStyle: 'solid',
|
|
445
652
|
borderColor: '#ccc',
|
|
446
653
|
borderWidth: 2,
|
|
447
|
-
width:
|
|
448
|
-
height:
|
|
654
|
+
width: isHorizontal(direction) ? length : thickness,
|
|
655
|
+
height: isHorizontal(direction) ? thickness : length,
|
|
449
656
|
zIndex: 1,
|
|
450
657
|
} }, trackProps)), jsxRuntime.jsx(SliderThumb, Object.assign({ color: color, __css: {
|
|
451
|
-
top:
|
|
452
|
-
left:
|
|
453
|
-
} }, thumbProps))] }), scale && (jsxRuntime.jsx("div", { className: "tremolo-slider-scale-wrapper", css: react.css(Object.assign({ position: 'relative', width:
|
|
658
|
+
top: isHorizontal(direction) ? '50%' : `${percentRev}%`,
|
|
659
|
+
left: isHorizontal(direction) ? `${percentRev}%` : '50%',
|
|
660
|
+
} }, thumbProps))] }), scale && (jsxRuntime.jsx("div", { className: "tremolo-slider-scale-wrapper", css: react.css(Object.assign({ position: 'relative', width: isHorizontal(direction) ? length : undefined, height: isVertical(direction) ? length : undefined, marginLeft: isHorizontal(direction)
|
|
454
661
|
? 2
|
|
455
|
-
: ((_b = scaleOption === null || scaleOption === void 0 ? void 0 : scaleOption.gap) !== null && _b !== void 0 ? _b : `calc((1.4rem - ${
|
|
662
|
+
: ((_b = scaleOption === null || scaleOption === void 0 ? void 0 : scaleOption.gap) !== null && _b !== void 0 ? _b : `calc((1.4rem - ${styleHelper(thickness)}) / 2)`), marginTop: isVertical(direction)
|
|
456
663
|
? 2
|
|
457
|
-
: ((_c = scaleOption === null || scaleOption === void 0 ? void 0 : scaleOption.gap) !== null && _c !== void 0 ? _c : `calc((1.4rem - ${
|
|
664
|
+
: ((_c = scaleOption === null || scaleOption === void 0 ? void 0 : scaleOption.gap) !== null && _c !== void 0 ? _c : `calc((1.4rem - ${styleHelper(thickness)}) / 2)`) }, scaleOption === null || scaleOption === void 0 ? void 0 : scaleOption.style)), children: scalesList.map((item, index) => {
|
|
458
665
|
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
|
|
459
666
|
return (jsxRuntime.jsxs("div", { css: react.css({
|
|
460
667
|
display: 'flex',
|
|
461
|
-
flexDirection:
|
|
668
|
+
flexDirection: isHorizontal(direction) ? 'column' : 'row',
|
|
462
669
|
justifyContent: 'center',
|
|
463
670
|
alignItems: 'center',
|
|
464
671
|
position: 'absolute',
|
|
465
|
-
left:
|
|
466
|
-
? `${
|
|
467
|
-
? 100 -
|
|
468
|
-
:
|
|
672
|
+
left: isHorizontal(direction)
|
|
673
|
+
? `${isReversed(direction)
|
|
674
|
+
? 100 - normalizeValue(item.at, min, max, skew) * 100
|
|
675
|
+
: normalizeValue(item.at, min, max, skew) * 100}%`
|
|
469
676
|
: undefined,
|
|
470
|
-
top:
|
|
471
|
-
? `${
|
|
472
|
-
? 100 -
|
|
473
|
-
:
|
|
677
|
+
top: isVertical(direction)
|
|
678
|
+
? `${isReversed(direction)
|
|
679
|
+
? 100 - normalizeValue(item.at, min, max, skew) * 100
|
|
680
|
+
: normalizeValue(item.at, min, max, skew) * 100}%`
|
|
474
681
|
: undefined,
|
|
475
|
-
translate:
|
|
682
|
+
translate: isHorizontal(direction) ? '-50% 0' : '0 -50%',
|
|
476
683
|
zIndex: 10,
|
|
477
684
|
}), children: [((_b = (_a = item.type) !== null && _a !== void 0 ? _a : scaleOption === null || scaleOption === void 0 ? void 0 : scaleOption.defaultType) !== null && _b !== void 0 ? _b : 'mark-number') !=
|
|
478
685
|
'number' && (jsxRuntime.jsx("div", { className: "tremolo-slider-scale-mark", css: react.css({
|
|
479
|
-
width:
|
|
686
|
+
width: isHorizontal(direction)
|
|
480
687
|
? ((_d = (_c = item.style) === null || _c === void 0 ? void 0 : _c.thickness) !== null && _d !== void 0 ? _d : 1)
|
|
481
688
|
: ((_f = (_e = item.style) === null || _e === void 0 ? void 0 : _e.length) !== null && _f !== void 0 ? _f : '0.5rem'),
|
|
482
|
-
height:
|
|
689
|
+
height: isVertical(direction)
|
|
483
690
|
? ((_h = (_g = item.style) === null || _g === void 0 ? void 0 : _g.thickness) !== null && _h !== void 0 ? _h : 1)
|
|
484
691
|
: ((_k = (_j = item.style) === null || _j === void 0 ? void 0 : _j.length) !== null && _k !== void 0 ? _k : '0.5rem'),
|
|
485
692
|
backgroundColor: (_o = (_m = (_l = item.style) === null || _l === void 0 ? void 0 : _l.markColor) !== null && _m !== void 0 ? _m : scaleOption === null || scaleOption === void 0 ? void 0 : scaleOption.markColor) !== null && _o !== void 0 ? _o : '#222',
|
|
486
|
-
marginBottom:
|
|
487
|
-
marginRight:
|
|
693
|
+
marginBottom: isHorizontal(direction) ? 2 : undefined,
|
|
694
|
+
marginRight: isVertical(direction) ? 2 : undefined,
|
|
488
695
|
}) })), ((_q = (_p = item.type) !== null && _p !== void 0 ? _p : scaleOption === null || scaleOption === void 0 ? void 0 : scaleOption.defaultType) !== null && _q !== void 0 ? _q : 'mark-number') !=
|
|
489
696
|
'mark' && (jsxRuntime.jsx("div", { className: "tremolo-slider-scale-number", css: react.css({
|
|
490
697
|
color: (_s = (_r = item.style) === null || _r === void 0 ? void 0 : _r.labelColor) !== null && _s !== void 0 ? _s : scaleOption === null || scaleOption === void 0 ? void 0 : scaleOption.labelColor,
|
|
@@ -494,12 +701,19 @@ function Slider(_a) {
|
|
|
494
701
|
}) }))] })] }));
|
|
495
702
|
}
|
|
496
703
|
|
|
704
|
+
exports.AnimationCanvas = AnimationCanvas;
|
|
497
705
|
exports.Knob = Knob;
|
|
706
|
+
exports.NumberInput = NumberInput;
|
|
498
707
|
exports.Slider = Slider;
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
708
|
+
exports.clamp = clamp;
|
|
709
|
+
exports.dbToGain = dbToGain;
|
|
710
|
+
exports.decimalPart = decimalPart;
|
|
711
|
+
exports.degree = degree;
|
|
712
|
+
exports.gainToDb = gainToDb;
|
|
713
|
+
exports.normalizeValue = normalizeValue;
|
|
714
|
+
exports.radian = radian;
|
|
715
|
+
exports.rawValue = rawValue;
|
|
716
|
+
exports.skewWithCenterValue = skewWithCenterValue;
|
|
717
|
+
exports.stepValue = stepValue;
|
|
718
|
+
exports.toFixed = toFixed;
|
|
505
719
|
//# sourceMappingURL=index.js.map
|