From c70f6eb46c0ac5410bf1a24c37a487f264e76c49 Mon Sep 17 00:00:00 2001 From: nainghtetlinn Date: Sat, 25 Nov 2023 13:14:45 +0630 Subject: [PATCH] create createAppOptions function and separate the code to utils.ts file --- src/library.ts | 31 ++----------------------------- src/utils.ts | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 29 deletions(-) create mode 100644 src/utils.ts diff --git a/src/library.ts b/src/library.ts index 012dd15..3f03d43 100644 --- a/src/library.ts +++ b/src/library.ts @@ -1,14 +1,5 @@ -import Color from "color" import { OtherPropsType, OptionsType, AppOptionsType } from "./types" - -const defaultOptions = { - fontSize: 30, - color: "rgba(255, 255, 255, 1)", - bgColor: "rgba(0, 0, 0, 1)", - wordCounts: { min: 5, max: 20 }, - rainSpeed: { min: 1, max: 3 }, - switchInterval: { min: 500, max: 1500 }, -} +import { createAppOptions } from "./utils" export function animateMatrixEffect(id: string, ...rest: OtherPropsType) { const parentElement = document.getElementById(id) @@ -32,25 +23,7 @@ export function animateMatrixEffect(id: string, ...rest: OtherPropsType) { rest[1] && typeof rest[1] === "function" && rest[1]() } - const appOptions: AppOptionsType = { - fontSize: options.fontSize || defaultOptions.fontSize, - color: new Color(options.color || defaultOptions.color).rgb().string(), - bgColor: new Color(options.bgColor || defaultOptions.bgColor) - .rgb() - .string(), - bgFade: new Color(options.bgColor || defaultOptions.bgColor) - .alpha(0.2) - .rgb() - .string(), - firstWordColor: new Color( - options.firstWordColor || options.color || defaultOptions.color - ) - .rgb() - .string(), - wordCounts: options.wordCounts || defaultOptions.wordCounts, - rainSpeed: options.rainSpeed || defaultOptions.rainSpeed, - switchInterval: options.switchInterval || defaultOptions.switchInterval, - } + const appOptions: AppOptionsType = createAppOptions(options) let effect = new MatrixEffect(canvas.width, canvas.height, appOptions) diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..45d080b --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,33 @@ +import Color from "color" +import { OptionsType, AppOptionsType } from "./types" + +const defaultOptions = { + fontSize: 30, + color: "rgba(255, 255, 255, 1)", + bgColor: "rgba(0, 0, 0, 1)", + wordCounts: { min: 5, max: 20 }, + rainSpeed: { min: 1, max: 3 }, + switchInterval: { min: 500, max: 1500 }, +} + +export function createAppOptions(options: OptionsType): AppOptionsType { + return { + fontSize: options.fontSize || defaultOptions.fontSize, + color: new Color(options.color || defaultOptions.color).rgb().string(), + bgColor: new Color(options.bgColor || defaultOptions.bgColor) + .rgb() + .string(), + bgFade: new Color(options.bgColor || defaultOptions.bgColor) + .alpha(0.2) + .rgb() + .string(), + firstWordColor: new Color( + options.firstWordColor || options.color || defaultOptions.color + ) + .rgb() + .string(), + wordCounts: options.wordCounts || defaultOptions.wordCounts, + rainSpeed: options.rainSpeed || defaultOptions.rainSpeed, + switchInterval: options.switchInterval || defaultOptions.switchInterval, + } +}