Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ranges and globalCompositionOperation override to Siriwave Options #74

Merged
merged 3 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 33 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,39 @@ Create a div container and instantiate a new SiriWave object:

## Constructor options

| Key | Type | Description | Default | Required |
| ----------------- | ------------------ | ---------------------------------------------------------------------- | ---------- | -------- |
| `container` | DOMElement | The DOM container where the DOM canvas element will be added. | null | yes |
| `style` | "ios", "ios9" | The style of the wave. | "ios" | no |
| `ratio` | Number | Ratio of the display to use. Calculated by default. | calculated | no |
| `speed` | Number | The speed of the animation. | 0.2 | no |
| `amplitude` | Number | The amplitude of the complete wave-form. | 1 | no |
| `frequency` | Number | The frequency of the complete wave-form. Only available in style "ios" | 6 | no |
| `color` | String | Color of the wave. Only available in style "ios" | "#fff" | no |
| `cover` | Bool | The `canvas` covers the entire width or height of the container | false | no |
| `autostart` | Bool | Decide wether start the animation on boot. | false | no |
| `pixelDepth` | Number | Number of step (in pixels) used when drawed on canvas. | 0.02 | no |
| `lerpSpeed` | Number | Lerp speed to interpolate properties. | 0.01 | no |
| `curveDefinition` | ICurveDefinition[] | Override definition of the curves, check above for more details. | null | no |
| Key | Type | Description | Default | Required |
| -------------------------- | ------------------------ | ---------------------------------------------------------------------- | ---------- | -------- |
| `container` | DOMElement | The DOM container where the DOM canvas element will be added. | null | yes |
| `style` | "ios", "ios9" | The style of the wave. | "ios" | no |
| `ratio` | Number | Ratio of the display to use. Calculated by default. | calculated | no |
| `speed` | Number | The speed of the animation. | 0.2 | no |
| `amplitude` | Number | The amplitude of the complete wave-form. | 1 | no |
| `frequency` | Number | The frequency of the complete wave-form. Only available in style "ios" | 6 | no |
| `color` | String | Color of the wave. Only available in style "ios" | "#fff" | no |
| `cover` | Bool | The `canvas` covers the entire width or height of the container | false | no |
| `autostart` | Bool | Decide wether start the animation on boot. | false | no |
| `pixelDepth` | Number | Number of step (in pixels) used when drawed on canvas. | 0.02 | no |
| `lerpSpeed` | Number | Lerp speed to interpolate properties. | 0.01 | no |
| `curveDefinition` | ICurveDefinition[] | Override definition of the curves, check above for more details. | null | no |
| `ranges` | IiOS9Ranges | Override the default random ranges of the curves. | null | no |
| `globalCompositeOperation` | GlobalCompositeOperation | globalCompositeOperation of the canvas, controls wave overlap design. | "lighter" | no |

### Ranges

Each wave chooses a random parameter for each of these ranges that factors into their creation. You can override these ranges by passing a `ranges` object to the constructor.

Here is the type of the ranges object:

```ts
export type IiOS9Ranges = {
noOfCurves?: [number, number];
amplitude?: [number, number];
offset?: [number, number];
width?: [number, number];
speed?: [number, number];
despawnTimeout?: [number, number];
};
```

## API

Expand Down
30 changes: 16 additions & 14 deletions dist/siriwave.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ class iOS9Curve {
this.DEAD_PX = 2;
this.ATT_FACTOR = 4;
this.DESPAWN_FACTOR = 0.02;
this.NOOFCURVES_RANGES = [2, 5];
this.AMPLITUDE_RANGES = [0.3, 1];
this.OFFSET_RANGES = [-3, 3];
this.WIDTH_RANGES = [1, 3];
this.SPEED_RANGES = [0.5, 1];
this.DESPAWN_TIMEOUT_RANGES = [500, 2000];
this.DEFAULT_NOOFCURVES_RANGES = [2, 5];
this.DEFAULT_AMPLITUDE_RANGES = [0.3, 1];
this.DEFAULT_OFFSET_RANGES = [-3, 3];
this.DEFAULT_WIDTH_RANGES = [1, 3];
this.DEFAULT_SPEED_RANGES = [0.5, 1];
this.DEFAULT_DESPAWN_TIMEOUT_RANGES = [500, 2000];
this.ctrl = ctrl;
this.definition = definition;
this.noOfCurves = 0;
Expand All @@ -123,21 +123,23 @@ class iOS9Curve {
return e[0] + Math.random() * (e[1] - e[0]);
}
spawnSingle(ci) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
this.phases[ci] = 0;
this.amplitudes[ci] = 0;
this.despawnTimeouts[ci] = this.getRandomRange(this.DESPAWN_TIMEOUT_RANGES);
this.offsets[ci] = this.getRandomRange(this.OFFSET_RANGES);
this.speeds[ci] = this.getRandomRange(this.SPEED_RANGES);
this.finalAmplitudes[ci] = this.getRandomRange(this.AMPLITUDE_RANGES);
this.widths[ci] = this.getRandomRange(this.WIDTH_RANGES);
this.despawnTimeouts[ci] = this.getRandomRange((_b = (_a = this.ctrl.opt.ranges) === null || _a === void 0 ? void 0 : _a.despawnTimeout) !== null && _b !== void 0 ? _b : this.DEFAULT_DESPAWN_TIMEOUT_RANGES);
this.offsets[ci] = this.getRandomRange((_d = (_c = this.ctrl.opt.ranges) === null || _c === void 0 ? void 0 : _c.offset) !== null && _d !== void 0 ? _d : this.DEFAULT_OFFSET_RANGES);
this.speeds[ci] = this.getRandomRange((_f = (_e = this.ctrl.opt.ranges) === null || _e === void 0 ? void 0 : _e.speed) !== null && _f !== void 0 ? _f : this.DEFAULT_SPEED_RANGES);
this.finalAmplitudes[ci] = this.getRandomRange((_h = (_g = this.ctrl.opt.ranges) === null || _g === void 0 ? void 0 : _g.amplitude) !== null && _h !== void 0 ? _h : this.DEFAULT_AMPLITUDE_RANGES);
this.widths[ci] = this.getRandomRange((_k = (_j = this.ctrl.opt.ranges) === null || _j === void 0 ? void 0 : _j.width) !== null && _k !== void 0 ? _k : this.DEFAULT_WIDTH_RANGES);
this.verses[ci] = this.getRandomRange([-1, 1]);
}
getEmptyArray(count) {
return new Array(count);
}
spawn() {
var _a, _b;
this.spawnAt = Date.now();
this.noOfCurves = Math.floor(this.getRandomRange(this.NOOFCURVES_RANGES));
this.noOfCurves = Math.floor(this.getRandomRange((_b = (_a = this.ctrl.opt.ranges) === null || _a === void 0 ? void 0 : _a.noOfCurves) !== null && _b !== void 0 ? _b : this.DEFAULT_NOOFCURVES_RANGES));
this.phases = this.getEmptyArray(this.noOfCurves);
this.offsets = this.getEmptyArray(this.noOfCurves);
this.speeds = this.getEmptyArray(this.noOfCurves);
Expand Down Expand Up @@ -194,7 +196,7 @@ class iOS9Curve {
draw() {
const { ctx } = this.ctrl;
ctx.globalAlpha = 0.7;
ctx.globalCompositeOperation = "lighter";
ctx.globalCompositeOperation = this.ctrl.opt.globalCompositeOperation;
if (this.spawnAt === 0) {
this.spawn();
}
Expand Down Expand Up @@ -265,7 +267,7 @@ class SiriWave {
// Curves objects to animate
this.curves = [];
const csStyle = window.getComputedStyle(container);
this.opt = Object.assign({ container, style: "ios", ratio: window.devicePixelRatio || 1, speed: 0.2, amplitude: 1, frequency: 6, color: "#fff", cover: false, width: parseInt(csStyle.width.replace("px", ""), 10), height: parseInt(csStyle.height.replace("px", ""), 10), autostart: true, pixelDepth: 0.02, lerpSpeed: 0.1 }, rest);
this.opt = Object.assign({ container, style: "ios", ratio: window.devicePixelRatio || 1, speed: 0.2, amplitude: 1, frequency: 6, color: "#fff", cover: false, width: parseInt(csStyle.width.replace("px", ""), 10), height: parseInt(csStyle.height.replace("px", ""), 10), autostart: true, pixelDepth: 0.02, lerpSpeed: 0.1, globalCompositeOperation: "lighter" }, rest);
/**
* Actual speed of the animation. Is not safe to change this value directly, use `setSpeed` instead.
*/
Expand Down
2 changes: 1 addition & 1 deletion dist/siriwave.esm.min.js

Large diffs are not rendered by default.

30 changes: 16 additions & 14 deletions dist/siriwave.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@
this.DEAD_PX = 2;
this.ATT_FACTOR = 4;
this.DESPAWN_FACTOR = 0.02;
this.NOOFCURVES_RANGES = [2, 5];
this.AMPLITUDE_RANGES = [0.3, 1];
this.OFFSET_RANGES = [-3, 3];
this.WIDTH_RANGES = [1, 3];
this.SPEED_RANGES = [0.5, 1];
this.DESPAWN_TIMEOUT_RANGES = [500, 2000];
this.DEFAULT_NOOFCURVES_RANGES = [2, 5];
this.DEFAULT_AMPLITUDE_RANGES = [0.3, 1];
this.DEFAULT_OFFSET_RANGES = [-3, 3];
this.DEFAULT_WIDTH_RANGES = [1, 3];
this.DEFAULT_SPEED_RANGES = [0.5, 1];
this.DEFAULT_DESPAWN_TIMEOUT_RANGES = [500, 2000];
this.ctrl = ctrl;
this.definition = definition;
this.noOfCurves = 0;
Expand All @@ -141,21 +141,23 @@
return e[0] + Math.random() * (e[1] - e[0]);
};
iOS9Curve.prototype.spawnSingle = function (ci) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
this.phases[ci] = 0;
this.amplitudes[ci] = 0;
this.despawnTimeouts[ci] = this.getRandomRange(this.DESPAWN_TIMEOUT_RANGES);
this.offsets[ci] = this.getRandomRange(this.OFFSET_RANGES);
this.speeds[ci] = this.getRandomRange(this.SPEED_RANGES);
this.finalAmplitudes[ci] = this.getRandomRange(this.AMPLITUDE_RANGES);
this.widths[ci] = this.getRandomRange(this.WIDTH_RANGES);
this.despawnTimeouts[ci] = this.getRandomRange((_b = (_a = this.ctrl.opt.ranges) === null || _a === void 0 ? void 0 : _a.despawnTimeout) !== null && _b !== void 0 ? _b : this.DEFAULT_DESPAWN_TIMEOUT_RANGES);
this.offsets[ci] = this.getRandomRange((_d = (_c = this.ctrl.opt.ranges) === null || _c === void 0 ? void 0 : _c.offset) !== null && _d !== void 0 ? _d : this.DEFAULT_OFFSET_RANGES);
this.speeds[ci] = this.getRandomRange((_f = (_e = this.ctrl.opt.ranges) === null || _e === void 0 ? void 0 : _e.speed) !== null && _f !== void 0 ? _f : this.DEFAULT_SPEED_RANGES);
this.finalAmplitudes[ci] = this.getRandomRange((_h = (_g = this.ctrl.opt.ranges) === null || _g === void 0 ? void 0 : _g.amplitude) !== null && _h !== void 0 ? _h : this.DEFAULT_AMPLITUDE_RANGES);
this.widths[ci] = this.getRandomRange((_k = (_j = this.ctrl.opt.ranges) === null || _j === void 0 ? void 0 : _j.width) !== null && _k !== void 0 ? _k : this.DEFAULT_WIDTH_RANGES);
this.verses[ci] = this.getRandomRange([-1, 1]);
};
iOS9Curve.prototype.getEmptyArray = function (count) {
return new Array(count);
};
iOS9Curve.prototype.spawn = function () {
var _a, _b;
this.spawnAt = Date.now();
this.noOfCurves = Math.floor(this.getRandomRange(this.NOOFCURVES_RANGES));
this.noOfCurves = Math.floor(this.getRandomRange((_b = (_a = this.ctrl.opt.ranges) === null || _a === void 0 ? void 0 : _a.noOfCurves) !== null && _b !== void 0 ? _b : this.DEFAULT_NOOFCURVES_RANGES));
this.phases = this.getEmptyArray(this.noOfCurves);
this.offsets = this.getEmptyArray(this.noOfCurves);
this.speeds = this.getEmptyArray(this.noOfCurves);
Expand Down Expand Up @@ -212,7 +214,7 @@
iOS9Curve.prototype.draw = function () {
var ctx = this.ctrl.ctx;
ctx.globalAlpha = 0.7;
ctx.globalCompositeOperation = "lighter";
ctx.globalCompositeOperation = this.ctrl.opt.globalCompositeOperation;
if (this.spawnAt === 0) {
this.spawn();
}
Expand Down Expand Up @@ -286,7 +288,7 @@
// Curves objects to animate
this.curves = [];
var csStyle = window.getComputedStyle(container);
this.opt = __assign({ container: container, style: "ios", ratio: window.devicePixelRatio || 1, speed: 0.2, amplitude: 1, frequency: 6, color: "#fff", cover: false, width: parseInt(csStyle.width.replace("px", ""), 10), height: parseInt(csStyle.height.replace("px", ""), 10), autostart: true, pixelDepth: 0.02, lerpSpeed: 0.1 }, rest);
this.opt = __assign({ container: container, style: "ios", ratio: window.devicePixelRatio || 1, speed: 0.2, amplitude: 1, frequency: 6, color: "#fff", cover: false, width: parseInt(csStyle.width.replace("px", ""), 10), height: parseInt(csStyle.height.replace("px", ""), 10), autostart: true, pixelDepth: 0.02, lerpSpeed: 0.1, globalCompositeOperation: "lighter" }, rest);
/**
* Actual speed of the animation. Is not safe to change this value directly, use `setSpeed` instead.
*/
Expand Down
Loading