Skip to content

Commit

Permalink
create createAppOptions function and separate the code to utils.ts file
Browse files Browse the repository at this point in the history
  • Loading branch information
nainghtetlinn committed Nov 25, 2023
1 parent 7bcd3ca commit c70f6eb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
31 changes: 2 additions & 29 deletions src/library.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)

Expand Down
33 changes: 33 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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,
}
}

0 comments on commit c70f6eb

Please sign in to comment.