@hapticjs/core 0.1.0 → 0.2.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/index.d.ts CHANGED
@@ -284,24 +284,48 @@ declare class NoopAdapter implements HapticAdapter {
284
284
  /**
285
285
  * Web Vibration API adapter.
286
286
  * Uses navigator.vibrate() — supported on Android browsers.
287
- * Intensity is simulated via PWM (rapid on/off) since the API only supports on/off.
287
+ * Android Vibration API is on/off only no intensity control.
288
+ * We ignore intensity and just vibrate for the requested duration.
288
289
  */
289
290
  declare class WebVibrationAdapter implements HapticAdapter {
290
291
  readonly name = "web-vibration";
291
292
  readonly supported: boolean;
292
- private _cancelled;
293
293
  constructor();
294
294
  capabilities(): AdapterCapabilities;
295
295
  pulse(_intensity: number, duration: number): Promise<void>;
296
296
  playSequence(steps: HapticStep[]): Promise<void>;
297
297
  cancel(): void;
298
298
  dispose(): void;
299
- /** Convert steps to Vibration API pattern array */
300
- private _toVibrationPattern;
301
- /** Check if all steps can be played with native pattern (no intensity variation) */
302
- private _canUseNativePattern;
303
- /** Simulate lower intensity via pulse-width modulation */
304
- private _pwmVibrate;
299
+ }
300
+
301
+ /**
302
+ * iOS Audio workaround adapter.
303
+ * Uses AudioContext to generate short, low-frequency oscillator tones (20-60 Hz)
304
+ * that create a subtle physical sensation through device speakers.
305
+ * This is a fallback for iOS Safari, which does not support the Vibration API.
306
+ */
307
+ declare class IoSAudioAdapter implements HapticAdapter {
308
+ readonly name = "ios-audio";
309
+ readonly supported: boolean;
310
+ private _audioCtx;
311
+ private _activeOscillator;
312
+ private _activeGain;
313
+ private _cancelled;
314
+ constructor();
315
+ capabilities(): AdapterCapabilities;
316
+ pulse(intensity: number, duration: number): Promise<void>;
317
+ playSequence(steps: HapticStep[]): Promise<void>;
318
+ cancel(): void;
319
+ dispose(): void;
320
+ private _detectSupport;
321
+ /** Get or create the AudioContext, resuming if suspended */
322
+ private _getAudioContext;
323
+ /** Map intensity (0-1) to frequency in the 20-60 Hz sub-bass range */
324
+ private _intensityToFrequency;
325
+ /** Play a single oscillator tone for the given duration */
326
+ private _playTone;
327
+ /** Immediately stop the active oscillator if any */
328
+ private _stopOscillator;
305
329
  }
306
330
 
307
331
  declare class HPLParserError extends Error {
@@ -357,6 +381,61 @@ interface ValidationResult {
357
381
  }
358
382
  declare function validateHPL(input: string): ValidationResult;
359
383
 
384
+ /** Portable JSON-serializable format for sharing haptic patterns */
385
+ interface HapticPatternExport {
386
+ version: 1;
387
+ name: string;
388
+ description?: string;
389
+ /** Original HPL string if pattern was created from HPL */
390
+ hpl?: string;
391
+ /** Compiled haptic steps */
392
+ steps: HapticStep[];
393
+ metadata?: {
394
+ /** Total duration in milliseconds */
395
+ duration: number;
396
+ author?: string;
397
+ tags?: string[];
398
+ /** ISO date string */
399
+ createdAt?: string;
400
+ };
401
+ }
402
+ /** Options for exporting a pattern */
403
+ interface ExportOptions {
404
+ name?: string;
405
+ description?: string;
406
+ author?: string;
407
+ tags?: string[];
408
+ }
409
+ /** Input types accepted by exportPattern */
410
+ type ExportInput = HapticPattern | HapticStep[] | string;
411
+ /**
412
+ * Export a haptic pattern as a portable JSON-serializable object.
413
+ *
414
+ * Accepts a HapticPattern, HapticStep[], or HPL string.
415
+ */
416
+ declare function exportPattern(input: ExportInput, options?: ExportOptions): HapticPatternExport;
417
+ /**
418
+ * Import a pattern from a HapticPatternExport object or JSON string.
419
+ * Validates the data and returns a HapticPattern.
420
+ */
421
+ declare function importPattern(data: HapticPatternExport | string): HapticPattern;
422
+ /**
423
+ * Serialize a pattern to a pretty-printed JSON string.
424
+ */
425
+ declare function patternToJSON(input: ExportInput, options?: ExportOptions): string;
426
+ /**
427
+ * Parse a JSON string into a HapticPattern.
428
+ */
429
+ declare function patternFromJSON(json: string): HapticPattern;
430
+ /**
431
+ * Encode a pattern as a data: URL for easy sharing via links.
432
+ */
433
+ declare function patternToDataURL(input: ExportInput, options?: ExportOptions): string;
434
+ /**
435
+ * Decode a data: URL back to a HapticPattern.
436
+ */
437
+ declare function patternFromDataURL(url: string): HapticPattern;
438
+
360
439
  /** Platform detection utilities */
361
440
  interface PlatformInfo {
362
441
  isWeb: boolean;
@@ -381,4 +460,4 @@ declare function detectPlatform(): PlatformInfo;
381
460
  */
382
461
  declare const haptic: HapticEngine;
383
462
 
384
- export { type AdapterCapabilities, AdaptiveEngine, type EasingFunction, type FallbackConfig, FallbackManager, type HPLNode, type HPLNodeType, HPLParser, HPLParserError, type HPLToken, type HPLTokenType, HPLTokenizerError, type HapticAdapter, type HapticConfig, HapticEngine, type HapticPattern, type HapticStep, type ImpactStyle, NoopAdapter, type NotificationType, PatternComposer, type PlatformInfo, type SemanticEffect, type ValidationResult, type VisualFallbackStyle, WebVibrationAdapter, compile, detectAdapter, detectPlatform, haptic, optimizeSteps, parseHPL, tokenize, validateHPL };
463
+ export { type AdapterCapabilities, AdaptiveEngine, type EasingFunction, type ExportOptions, type FallbackConfig, FallbackManager, type HPLNode, type HPLNodeType, HPLParser, HPLParserError, type HPLToken, type HPLTokenType, HPLTokenizerError, type HapticAdapter, type HapticConfig, HapticEngine, type HapticPattern, type HapticPatternExport, type HapticStep, type ImpactStyle, IoSAudioAdapter, NoopAdapter, type NotificationType, PatternComposer, type PlatformInfo, type SemanticEffect, type ValidationResult, type VisualFallbackStyle, WebVibrationAdapter, compile, detectAdapter, detectPlatform, exportPattern, haptic, importPattern, optimizeSteps, parseHPL, patternFromDataURL, patternFromJSON, patternToDataURL, patternToJSON, tokenize, validateHPL };