createVibration
Category Browser
Most modern mobile devices include vibration hardware, which lets software code provide physical feedback to the user by causing the device to shake.
The Vibration API offers Web apps the ability to access this hardware, if it exists, and does nothing if the device doesn't support it.
Demo
Your browser doesn't support the Vibration API :(
Usage
Vibration is described as a pattern of on-off pulses, which may be of varying lengths.
The pattern may consist of either a single integer, describing the number of milliseconds to vibrate, or an array of integers describing a pattern of vibrations and pauses.
import { createVibration } from '@sv-use/core';
// 1. Vibrates the device for 300ms
// 2. Pauses for 100ms
// 3. Vibrates the device again for 200ms
const vibration = createVibration({ pattern: [300, 100, 200] });
// Start the vibration
// It will automatically stop when the pattern is completed
vibration.vibrate();
// Or you can manually stop it
vibration.stop();
Type Definitions
import { type ConfigurableNavigator } from '../__internal__/configurable.js';
import type { MaybeGetter } from '../__internal__/types.js';
interface CreateVibrationOptions extends ConfigurableNavigator {
/**
* An array of values describes alternating periods in which the device is
* vibrating and not vibrating. Each value in the array is converted to an
* integer, then interpreted alternately as the number of milliseconds the
* device should vibrate and the number of milliseconds it should not be
* vibrating.
* @default []
*/
pattern?: MaybeGetter<VibratePattern>;
}
type CreateVibrationReturn = {
readonly isSupported: boolean;
vibrate: () => void;
stop: () => void;
};
/**
* Reactive vibrate.
* @param options Additional options to customize the behavior.
* @see https://svelte-librarian.github.io/sv-use/docs/core/create-vibration
*/
export declare function createVibration(options?: CreateVibrationOptions): CreateVibrationReturn;
export {};