Skip to content

Commit eb6bcf4

Browse files
Added text padding option
1 parent 9d03ac7 commit eb6bcf4

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

src/app.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const hexToRgb = (hex: string): [number, number, number] => {
1919
export default function App() {
2020
const DEFAULT_TEXT_SIZE = 120;
2121
const DEFAULT_BG_DIM = 0.4;
22+
const DEFAULT_TEXT_PADDING = 0.05;
2223

2324
const camvasSizes: Record<
2425
"cover" | "poster",
@@ -47,6 +48,7 @@ export default function App() {
4748
const [dimColor, setDimColor] = useState("#000000");
4849
const [textAlign, setTextAlign] = useState<TextAlign>("center");
4950
const [textBaseline, setTextBaseline] = useState<TextBaseLine>("middle");
51+
const [textPadding, setTextPadding] = useState(DEFAULT_TEXT_PADDING);
5052

5153
const getCanvasWidth = () => camvasSizes[imageType].width;
5254
const getCanvasHeight = () => camvasSizes[imageType].height;
@@ -75,8 +77,9 @@ export default function App() {
7577
ctx.textAlign = align;
7678

7779
let x: number = getCanvasWidth() / 2;
78-
if (align === "left") x = maxWidth * 0.05;
79-
else if (align === "right") x = getCanvasWidth() - maxWidth * 0.05;
80+
if (align === "left") x = maxWidth * textPadding;
81+
else if (align === "right")
82+
x = getCanvasWidth() - maxWidth * textPadding;
8083

8184
const words = text.split(" ");
8285
const lines: string[] = [];
@@ -96,7 +99,7 @@ export default function App() {
9699

97100
const totalHeight = lines.length * lineHeight;
98101

99-
const padding = getCanvasHeight() * 0.05;
102+
const padding = getCanvasHeight() * textPadding;
100103
let y: number;
101104
if (baseline === "top") {
102105
y = padding;
@@ -208,6 +211,7 @@ export default function App() {
208211
dimColor,
209212
textAlign,
210213
textBaseline,
214+
textPadding,
211215
]);
212216

213217
useEffect(() => {
@@ -264,6 +268,9 @@ export default function App() {
264268
setTextAlign={setTextAlign}
265269
textBaseline={textBaseline}
266270
setTextBaseline={setTextBaseline}
271+
defaultTextPadding={DEFAULT_TEXT_PADDING}
272+
textPadding={textPadding}
273+
setTextPadding={setTextPadding}
267274
/>
268275

269276
<div

src/components/OptionsDisplay.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ interface OptionsDisplayProps {
3838
setTextAlign: (align: TextAlign) => void;
3939
textBaseline: TextBaseLine;
4040
setTextBaseline: (baseline: TextBaseLine) => void;
41+
defaultTextPadding: number;
42+
textPadding: number;
43+
setTextPadding: (padding: number) => void;
4144
}
4245

4346
const OptionsDisplay: FC<OptionsDisplayProps> = ({
@@ -63,6 +66,9 @@ const OptionsDisplay: FC<OptionsDisplayProps> = ({
6366
setTextAlign,
6467
textBaseline,
6568
setTextBaseline,
69+
defaultTextPadding,
70+
textPadding,
71+
setTextPadding,
6672
}) => (
6773
<div className="flex flex-col gap-5 w-1/3 mb-5">
6874
<div className="flex gap-3 items-end">
@@ -153,6 +159,25 @@ const OptionsDisplay: FC<OptionsDisplayProps> = ({
153159
/>
154160
</div>
155161

162+
<div className="flex flex-col">
163+
<span className="text-sm text-muted-foreground mb-1">
164+
Text Padding:
165+
</span>
166+
<SliderInput
167+
value={textPadding}
168+
min={0}
169+
max={0.5}
170+
decimalPlaces={2}
171+
onChange={(v) => setTextPadding(v)}
172+
step={0.01}
173+
showPlusMinusButtons
174+
displayValue
175+
displayUnit="%"
176+
defaultValue={defaultTextPadding}
177+
disabled={textAlign == "center" && textBaseline == "middle"}
178+
/>
179+
</div>
180+
156181
<div className="flex flex-col">
157182
<span className="text-sm text-muted-foreground mb-1">
158183
Text Size:

src/components/SliderInput.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ interface SliderInputProps {
1414
displayUnit?: string;
1515
decimalPlaces?: number;
1616
className?: string;
17+
disabled?: boolean;
1718
}
1819

1920
const SliderInput: FC<SliderInputProps> = ({
@@ -29,6 +30,7 @@ const SliderInput: FC<SliderInputProps> = ({
2930
displayUnit,
3031
decimalPlaces = 0,
3132
className = "",
33+
disabled = false,
3234
}) => {
3335
const sliderRef = useRef<HTMLInputElement>(null);
3436

@@ -51,7 +53,11 @@ const SliderInput: FC<SliderInputProps> = ({
5153
};
5254

5355
return (
54-
<div className={"flex gap-3 items-center justify-between " + className}>
56+
<div
57+
className={
58+
"relative flex gap-3 items-center justify-between " + className
59+
}
60+
>
5561
{showPlusMinusButtons && (
5662
<button
5763
className="btn-ghost"
@@ -74,6 +80,7 @@ const SliderInput: FC<SliderInputProps> = ({
7480
defaultValue={defaultValue}
7581
onInput={handleInput}
7682
ref={sliderRef}
83+
disabled={disabled}
7784
/>
7885

7986
{showPlusMinusButtons && (
@@ -82,7 +89,7 @@ const SliderInput: FC<SliderInputProps> = ({
8289
onClick={() =>
8390
onChange(Math.min(max, value + (buttonStep || step)))
8491
}
85-
disabled={value >= max}
92+
disabled={value >= max || disabled}
8693
>
8794
<Plus />
8895
</button>
@@ -110,12 +117,16 @@ const SliderInput: FC<SliderInputProps> = ({
110117
<button
111118
className="btn-ghost"
112119
onClick={() => onChange(defaultValue)}
113-
disabled={value === defaultValue}
120+
disabled={value === defaultValue || disabled}
114121
title="Reset to default value"
115122
>
116123
<Undo2 />
117124
</button>
118125
)}
126+
127+
{disabled && (
128+
<div className="absolute inset-0 bg-black opacity-50 pointer-events-none"></div>
129+
)}
119130
</div>
120131
);
121132
};

0 commit comments

Comments
 (0)