Skip to content

Commit

Permalink
feat: moved twinkle options to twinkle updater package
Browse files Browse the repository at this point in the history
  • Loading branch information
matteobruni committed Jun 6, 2022
1 parent 85abd01 commit d6389d4
Show file tree
Hide file tree
Showing 16 changed files with 99 additions and 43 deletions.
@@ -1,18 +1,18 @@
import type { Container, ITwinkle, ITwinkleValues } from "tsparticles-engine";
import { EditorGroup, EditorType } from "object-gui";
import type { Container } from "tsparticles-engine";
import { EditorBase } from "../../../../EditorBase";

export class TwinkleOptionsEditor extends EditorBase {
group!: EditorGroup;
private options!: ITwinkle;
private options!: unknown;

constructor(particles: Container) {
super(particles);
}

addToGroup(parent: EditorGroup, options?: unknown): void {
this.group = parent.addGroup("twinkle", "Twinkle", true, options);
this.options = this.group.data as ITwinkle;
this.options = this.group.data as unknown;

this.addTwinkle();
}
Expand All @@ -24,7 +24,7 @@ export class TwinkleOptionsEditor extends EditorBase {

private addTwinkleValues(group: EditorGroup): void {
const particles = this.particles;
const options = group.data as ITwinkleValues;
const options = group.data as { color: string | undefined | { value: unknown } };
const color = typeof options.color === "string" ? options.color : options.color?.value;

group.addProperty("color", "Color", EditorType.color, color, false).change(async (value: unknown) => {
Expand Down
@@ -1,18 +1,18 @@
import type { Container, IWobble } from "tsparticles-engine";
import { EditorGroup, EditorType } from "object-gui";
import type { Container } from "tsparticles-engine";
import { EditorBase } from "../../../../EditorBase";

export class WobbleOptionsEditor extends EditorBase {
group!: EditorGroup;
private options!: IWobble;
private options!: unknown;

constructor(particles: Container) {
super(particles);
}

addToGroup(parent: EditorGroup): void {
this.group = parent.addGroup("wobble", "Wobble");
this.options = this.group.data as IWobble;
this.options = this.group.data as unknown;

this.addProperties();
}
Expand Down
4 changes: 0 additions & 4 deletions engine/src/Options/Classes/Particles/ParticlesOptions.ts
Expand Up @@ -25,7 +25,6 @@ import type { SingleOrMultiple } from "../../../Types/SingleOrMultiple";
import { Size } from "./Size/Size";
import { Stroke } from "./Stroke";
import { Tilt } from "./Tilt/Tilt";
import { Twinkle } from "./Twinkle/Twinkle";
import { ZIndex } from "./ZIndex/ZIndex";
import { deepExtend } from "../../../Utils/Utils";

Expand Down Expand Up @@ -56,7 +55,6 @@ export class ParticlesOptions implements IParticlesOptions, IOptionLoader<IParti
shadow;
stroke: SingleOrMultiple<Stroke>;
tilt;
twinkle;
zIndex;

/**
Expand Down Expand Up @@ -124,7 +122,6 @@ export class ParticlesOptions implements IParticlesOptions, IOptionLoader<IParti
this.size = new Size();
this.stroke = new Stroke();
this.tilt = new Tilt();
this.twinkle = new Twinkle();
this.zIndex = new ZIndex();
}

Expand Down Expand Up @@ -171,7 +168,6 @@ export class ParticlesOptions implements IParticlesOptions, IOptionLoader<IParti
this.size.load(data.size);
this.shadow.load(data.shadow);
this.tilt.load(data.tilt);
this.twinkle.load(data.twinkle);
this.zIndex.load(data.zIndex);

const collisions = data.move?.collisions ?? data.move?.bounce;
Expand Down
2 changes: 0 additions & 2 deletions engine/src/Options/Interfaces/Particles/IParticlesOptions.ts
Expand Up @@ -22,7 +22,6 @@ import type { IShape } from "./Shape/IShape";
import type { ISize } from "./Size/ISize";
import type { IStroke } from "./IStroke";
import type { ITilt } from "./Tilt/ITilt";
import type { ITwinkle } from "./Twinkle/ITwinkle";
import type { IZIndex } from "./ZIndex/IZIndex";
import type { ParticlesGroups } from "../../../Types/ParticlesGroups";
import type { RecursivePartial } from "../../../Types/RecursivePartial";
Expand Down Expand Up @@ -65,7 +64,6 @@ export interface IParticlesOptions {
size: ISize;
stroke: SingleOrMultiple<IStroke>;
tilt: ITilt;
twinkle: ITwinkle;
zIndex: IZIndex;

[name: string]: unknown;
Expand Down
2 changes: 0 additions & 2 deletions engine/src/bundle.ts
Expand Up @@ -120,8 +120,6 @@ export * from "./Options/Classes/Particles/Size/Size";
export * from "./Options/Classes/Particles/Size/SizeAnimation";
export * from "./Options/Classes/Particles/Tilt/Tilt";
export * from "./Options/Classes/Particles/Tilt/TiltAnimation";
export * from "./Options/Classes/Particles/Twinkle/Twinkle";
export * from "./Options/Classes/Particles/Twinkle/TwinkleValues";
export * from "./Options/Classes/Particles/ZIndex/ZIndex";
export * from "./Options/Classes/Responsive";
export * from "./Options/Classes/Theme/Theme";
Expand Down
4 changes: 0 additions & 4 deletions engine/src/index.ts
Expand Up @@ -178,8 +178,6 @@ export * from "./Options/Classes/Particles/Size/Size";
export * from "./Options/Classes/Particles/Size/SizeAnimation";
export * from "./Options/Classes/Particles/Tilt/Tilt";
export * from "./Options/Classes/Particles/Tilt/TiltAnimation";
export * from "./Options/Classes/Particles/Twinkle/Twinkle";
export * from "./Options/Classes/Particles/Twinkle/TwinkleValues";
export * from "./Options/Classes/Particles/ZIndex/ZIndex";
export * from "./Options/Classes/Responsive";
export * from "./Options/Classes/Theme/Theme";
Expand Down Expand Up @@ -274,8 +272,6 @@ export * from "./Options/Interfaces/Particles/Size/ISize";
export * from "./Options/Interfaces/Particles/Size/ISizeAnimation";
export * from "./Options/Interfaces/Particles/Tilt/ITilt";
export * from "./Options/Interfaces/Particles/Tilt/ITiltAnimation";
export * from "./Options/Interfaces/Particles/Twinkle/ITwinkle";
export * from "./Options/Interfaces/Particles/Twinkle/ITwinkleValues";
export * from "./Options/Interfaces/Particles/ZIndex/IZIndex";
export * from "./Options/Interfaces/Theme/ITheme";
export * from "./Options/Interfaces/Theme/IThemeDefault";
Expand Down
8 changes: 7 additions & 1 deletion engine/tests/Fixture/TestContainer.ts
Expand Up @@ -4,6 +4,10 @@ import { Options } from "../../src";
import { RecursivePartial } from "../../src";
import { tsParticles } from "../../src";

const Window = require("window");

const gThis = globalThis;

declare global {
interface Window {
SVGPathSeg: unknown;
Expand All @@ -16,6 +20,8 @@ export class TestContainer {
container: Container;

constructor(options?: RecursivePartial<IOptions>) {
gThis.window = new Window();

window.SVGPathSeg = {} as any;
tsParticles.init();

Expand All @@ -40,7 +46,7 @@ export class TestContainer {
}

this.container = new Container(tsParticles, this.id, this.options);
this.container.actualOptions = new Options(tsParticles);
this.container.actualOptions = new Options(tsParticles, this.container);
this.container.actualOptions.load(this.container.options);
this.container.init();
}
Expand Down
11 changes: 7 additions & 4 deletions engine/tests/Options.ts
Expand Up @@ -13,11 +13,14 @@ import {
} from "../src";
import type { IParticlesOptions, RecursivePartial } from "../src";
import { describe, it } from "mocha";
import { TestContainer } from "./Fixture/TestContainer";
import { expect } from "chai";

const testContainer = new TestContainer({});

describe("Options tests", () => {
it("checking default options", () => {
const options = new Options(tsParticles);
const options = new Options(tsParticles, testContainer.container);

/* background */
expect(options.background.color).to.include({ value: "" });
Expand Down Expand Up @@ -154,7 +157,7 @@ describe("Options tests", () => {
});

it("check default preset options", () => {
const options = new Options(tsParticles);
const options = new Options(tsParticles, testContainer.container);
const preset = {
background: {
color: "#0d47a1",
Expand Down Expand Up @@ -328,7 +331,7 @@ describe("Options tests", () => {
});

it("check test preset options", () => {
const options = new Options(tsParticles);
const options = new Options(tsParticles, testContainer.container);
const preset = {
background: {
color: "#0d47a1",
Expand Down Expand Up @@ -519,7 +522,7 @@ describe("Options tests", () => {
});

it("check particlesOptions override", () => {
const particlesOptions = new ParticlesOptions();
const particlesOptions = new ParticlesOptions(tsParticles, testContainer.container);

const generalOptions: RecursivePartial<IParticlesOptions> = {
number: {
Expand Down
5 changes: 4 additions & 1 deletion engine/tests/Themes.ts
@@ -1,7 +1,10 @@
import { ClickMode, HoverMode, MoveDirection, Options, OutMode, RotateDirection, ThemeMode, tsParticles } from "../src";
import { describe, it } from "mocha";
import { TestContainer } from "./Fixture/TestContainer";
import { expect } from "chai";

const testContainer = new TestContainer({});

describe("Themes", () => {
const sourceOptions = {
themes: [
Expand Down Expand Up @@ -93,7 +96,7 @@ describe("Themes", () => {
detectRetina: true,
};

const options = new Options(tsParticles);
const options = new Options(tsParticles, testContainer.container);

options.load(sourceOptions);

Expand Down
15 changes: 12 additions & 3 deletions interactions/particles/links/src/LinkInstance.ts
@@ -1,4 +1,4 @@
import type { Container, IContainerPlugin, IRgb, Particle } from "tsparticles-engine";
import type { Container, IContainerPlugin, IRangeColor, IRgb, Particle, RangeValue } from "tsparticles-engine";
import { drawLinkLine, drawLinkTriangle } from "./Utils";
import { getDistance, getLinkColor, getRangeValue, rangeColorToRgb } from "tsparticles-engine";
import type { ILink } from "./ILink";
Expand Down Expand Up @@ -145,9 +145,18 @@ export class LinkInstance implements IContainerPlugin {
* from those two for the connecting line color
*/

const twinkle = p1.options.twinkle.lines;
const twinkle = (
p1.options.twinkle as {
lines: {
enable: boolean;
frequency: number;
color: IRangeColor;
opacity: RangeValue;
};
}
)?.lines;

if (twinkle.enable) {
if (twinkle?.enable) {
const twinkleFreq = twinkle.frequency,
twinkleRgb = rangeColorToRgb(twinkle.color),
twinkling = Math.random() < twinkleFreq;
Expand Down
@@ -1,6 +1,5 @@
import type { IOptionLoader } from "../../../Interfaces/IOptionLoader";
import type { ITwinkle } from "../../../Interfaces/Particles/Twinkle/ITwinkle";
import type { RecursivePartial } from "../../../../Types/RecursivePartial";
import type { IOptionLoader, RecursivePartial } from "tsparticles-engine";
import type { ITwinkle } from "../Interfaces/ITwinkle";
import { TwinkleValues } from "./TwinkleValues";

/**
Expand Down
@@ -1,9 +1,6 @@
import type { IOptionLoader } from "../../../Interfaces/IOptionLoader";
import type { ITwinkleValues } from "../../../Interfaces/Particles/Twinkle/ITwinkleValues";
import { OptionsColor } from "../../OptionsColor";
import type { RangeValue } from "../../../../Types/RangeValue";
import type { RecursivePartial } from "../../../../Types/RecursivePartial";
import { setRangeValue } from "../../../../Utils/NumberUtils";
import type { IOptionLoader, RangeValue, RecursivePartial } from "tsparticles-engine";
import { OptionsColor, setRangeValue } from "tsparticles-engine";
import type { ITwinkleValues } from "../Interfaces/ITwinkleValues";

/**
* @category Options
Expand Down
@@ -1,5 +1,4 @@
import type { IOptionsColor } from "../../IOptionsColor";
import type { RangeValue } from "../../../../Types/RangeValue";
import type { IOptionsColor, RangeValue } from "tsparticles-engine";

/**
* @category Options
Expand Down
59 changes: 55 additions & 4 deletions updaters/twinkle/src/TwinkleUpdater.ts
@@ -1,5 +1,26 @@
import type { IParticleColorStyle, IParticleUpdater, Particle } from "tsparticles-engine";
import type {
IParticleColorStyle,
IParticleUpdater,
IParticlesOptions,
Particle,
ParticlesOptions,
RecursivePartial,
} from "tsparticles-engine";
import { getRangeValue, getStyleFromHsl, rangeColorToHsl } from "tsparticles-engine";
import { ITwinkle } from "./Options/Interfaces/ITwinkle";
import { Twinkle } from "./Options/Classes/Twinkle";

type TwinkeParticle = Particle & {
options: TwinkleParticlesOptions;
};

type ITwinkleParticlesOptions = IParticlesOptions & {
twinkle?: ITwinkle;
};

type TwinkleParticlesOptions = ParticlesOptions & {
twinkle?: Twinkle;
};

export class TwinkleUpdater implements IParticleUpdater {
getColorStyles(
Expand All @@ -9,7 +30,13 @@ export class TwinkleUpdater implements IParticleUpdater {
opacity: number
): IParticleColorStyle {
const pOptions = particle.options,
twinkle = pOptions.twinkle.particles,
twinkleOptions = pOptions.twinkle as Twinkle;

if (!twinkleOptions) {
return {};
}

const twinkle = twinkleOptions.particles,
twinkling = twinkle.enable && Math.random() < twinkle.frequency,
zIndexOptions = particle.options.zIndex,
zOpacityFactor = (1 - particle.zIndexFactor) ** zIndexOptions.opacityRate,
Expand All @@ -29,11 +56,35 @@ export class TwinkleUpdater implements IParticleUpdater {
// do nothing
}

isEnabled(particle: Particle): boolean {
return particle.options.twinkle.particles.enable;
isEnabled(particle: TwinkeParticle): boolean {
const pOptions = particle.options,
twinkleOptions = pOptions.twinkle as Twinkle;

if (!twinkleOptions) {
return false;
}

return twinkleOptions.particles.enable;
}

update(): void {
// do nothing
}

loadOptions(
options: TwinkleParticlesOptions,
...sources: (RecursivePartial<ITwinkleParticlesOptions> | undefined)[]
): void {
for (const source of sources) {
if (!source?.twinkle) {
continue;
}

if (!options.twinkle) {
options.twinkle = new Twinkle();
}

options.twinkle.load(source.twinkle);
}
}
}
1 change: 1 addition & 0 deletions utils/webpack/common/getExternals.js
Expand Up @@ -71,6 +71,7 @@ const getExternals = (bundle) => {
"tsparticles-updater-roll",
"tsparticles-updater-size",
"tsparticles-updater-tilt",
"tsparticles-updater-twinkle",
"tsparticles-updater-wobble");
};

Expand Down

0 comments on commit d6389d4

Please sign in to comment.