HandwriteGen.jsx 13.9 KB
Newer Older
fisherdaddy's avatar
fisherdaddy committed
1 2 3
import React, { useState, useEffect } from 'react';
import { usePageLoading } from '../hooks/usePageLoading';
import LoadingOverlay from './LoadingOverlay';
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
import '../styles/HandwriteGen.css';
import html2canvas from 'html2canvas';
import {
    Layout, Menu, Input, Select, Checkbox, Button, Slider, Typography, Row, Col
} from 'antd';

// 引入本地纸张背景图片
import Style1Img from '../data/handwrite/style1.png';
import Style2Img from '../data/handwrite/style2.png';
import Style3Img from '../data/handwrite/style3.png';

const { Header, Content, Sider } = Layout;
const { TextArea } = Input;
const { Option } = Select;
const { Title } = Typography;

function HandwritingGenerator() {
fisherdaddy's avatar
fisherdaddy committed
21
    const isLoading = usePageLoading();
22 23 24 25 26 27 28 29 30 31 32 33 34 35
    const [text, setText] = useState('');
    const [font, setFont] = useState("'XINYE'");
    const [paperType, setPaperType] = useState('Lined Paper'); // 默认值为横线纸
    const [paperBackground, setPaperBackground] = useState('None'); // 新增状态
    const [borderEnabled, setBorderEnabled] = useState(false);
    const [topMargin, setTopMargin] = useState(50);
    const [leftMargin, setLeftMargin] = useState(30);
    const [rightMargin, setRightMargin] = useState(30);
    const [fontSize, setFontSize] = useState(20);
    const [fontColor, setFontColor] = useState('#000080');
    const [textAlign, setTextAlign] = useState('left');
    const [lineSpacing, setLineSpacing] = useState(1.25);
    const [charSpacing, setCharSpacing] = useState(0);

fisherdaddy's avatar
fisherdaddy committed
36 37 38 39 40 41 42 43 44
    useEffect(() => {
        // Clear any loading states from previous navigation
        const clearLoadingState = () => {
            const event = new CustomEvent('clearLoadingState');
            window.dispatchEvent(event);
        };
        clearLoadingState();
    }, []);

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    const handleGenerate = () => {
        const previewElement = document.querySelector('.preview-area');
        html2canvas(previewElement, {
            useCORS: true,
            backgroundColor: null,
            scale: 2, // 提高图片清晰度
            onclone: (clonedDoc) => {
                // 重新设置背景,确保CSS渐变被正确捕获
                const clonedPreview = clonedDoc.querySelector('.preview-area');
                clonedPreview.style.backgroundImage = getPaperBackground();
                clonedPreview.style.backgroundSize = getBackgroundSize();
                clonedPreview.style.backgroundRepeat = getBackgroundRepeat();
            },
        }).then(canvas => {
            const link = document.createElement('a');
            link.download = 'handwriting.png';
            link.href = canvas.toDataURL();
            link.click();
        });
    };

    const getPaperBackground = () => {
        let backgrounds = [];

        // 根据纸张类型设置CSS渐变背景
        switch(paperType) {
            case 'Lined Paper':
                backgrounds.push(`linear-gradient(to bottom, transparent ${lineSpacing * fontSize - 1}px, #c0c0c0 1px)`);
                break;
            case 'Grid Paper':
                backgrounds.push(
                    `linear-gradient(to bottom, transparent ${lineSpacing * fontSize - 1}px, #c0c0c0 1px)`,
                    `linear-gradient(to right, transparent ${fontSize}px, #c0c0c0 1px)`
                );
                break;
            default:
                break;
        }

        // 如果用户选择了纸张背景,添加背景图片
        if (paperBackground !== 'None') {
            let backgroundImage;
            switch(paperBackground) {
                case 'Style1':
                    backgroundImage = `url(${Style1Img})`;
                    break;
                case 'Style2':
                    backgroundImage = `url(${Style2Img})`;
                    break;
                case 'Style3':
                    backgroundImage = `url(${Style3Img})`;
                    break;
                default:
                    break;
            }
            if (backgroundImage) {
                backgrounds.push(backgroundImage);
            }
        }

        if (backgrounds.length === 0) {
            return 'none';
        } else {
            return backgrounds.join(', ');
        }
    };

    const getBackgroundSize = () => {
        let sizes = [];
    
        switch(paperType) {
            case 'Lined Paper':
                sizes.push(`100% ${lineSpacing * fontSize}px`);
                break;
            case 'Grid Paper':
                sizes.push(`100% ${lineSpacing * fontSize}px`, `${lineSpacing * fontSize}px 100%`);
                break;
            default:
                break;
        }
    
        if (paperBackground !== 'None') {
            sizes.push('cover');
        }
    
        return sizes.join(', ');
    };
    

    const getBackgroundRepeat = () => {
        let repeats = [];

        // 对于纸张类型的CSS渐变背景,需要重复
        switch(paperType) {
            case 'Lined Paper':
                repeats.push('repeat-y');
                break;
            case 'Grid Paper':
                repeats.push('repeat-y', 'repeat-x');
                break;
            default:
                break;
        }

        // 对于纸张背景图片,设置为不重复或根据需要重复
        if (paperBackground !== 'None') {
            repeats.push('no-repeat');
        }

        return repeats.join(', ');
    };

    const backgroundOffset = -(lineSpacing * fontSize - fontSize);

    return (
fisherdaddy's avatar
fisherdaddy committed
160 161 162
        <>
            {isLoading && <LoadingOverlay />}
            <Layout className="min-h-screen pt-20">
163 164 165 166 167 168 169 170 171 172 173 174
                <Sider width={300} className="site-layout-background">
                    <div className="settings-section">
                        <h2 className="title-label">手写字体生成器</h2>
                        <div className="form-group">
                            <label>手写字体</label>
                            <Select value={font} onChange={setFont} style={{ width: '100%' }}>
                                <Option value="'XINYE'">新叶念体</Option>
                                <Option value="'cicada'">CC 字体</Option>
                                <Option value="'xiongdi'">兄弟字体</Option>
                                <Option value="'qishan-zhong'">Zhong Qi Shan 体</Option>
                            </Select>
                        </div>
175

176 177 178 179 180 181 182
                        <div className="form-group">
                            <label>纸张类型</label>
                            <Select value={paperType} onChange={setPaperType} style={{ width: '100%' }}>
                                <Option value="No Paper">无纸张</Option>
                                <Option value="Lined Paper">横线纸</Option>
                            </Select>
                        </div>
183

184 185 186 187 188 189 190 191 192 193
                        {/* 新增纸张背景选项 */}
                        <div className="form-group">
                            <label>纸张背景</label>
                            <Select value={paperBackground} onChange={setPaperBackground} style={{ width: '100%' }}>
                                <Option value="None">无背景</Option>
                                <Option value="Style1">样式1</Option>
                                <Option value="Style2">样式2</Option>
                                <Option value="Style3">样式3</Option>
                            </Select>
                        </div>
194

195 196 197 198 199
                        <div className="form-group">
                            <Checkbox checked={borderEnabled} onChange={() => setBorderEnabled(!borderEnabled)}>
                                边框
                            </Checkbox>
                        </div>
200

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
                        <div className="form-group">
                            <label>边距设置 (px)</label>
                            <Row gutter={8}>
                                <Col span={8}>
                                    <Input
                                        type="number"
                                        value={topMargin}
                                        onChange={(e) => setTopMargin(e.target.value)}
                                        placeholder="上"
                                    />
                                </Col>
                                <Col span={8}>
                                    <Input
                                        type="number"
                                        value={leftMargin}
                                        onChange={(e) => setLeftMargin(e.target.value)}
                                        placeholder="左"
                                    />
                                </Col>
                                <Col span={8}>
                                    <Input
                                        type="number"
                                        value={rightMargin}
                                        onChange={(e) => setRightMargin(e.target.value)}
                                        placeholder="右"
                                    />
                                </Col>
                            </Row>
                        </div>
230

231 232 233 234 235 236 237 238 239
                        <div className="form-group">
                            <label>字体大小 (px)</label>
                            <Slider
                                min={12}
                                max={72}
                                value={fontSize}
                                onChange={setFontSize}
                            />
                        </div>
240

241 242 243 244 245 246 247 248 249
                        <div className="form-group">
                            <label>字体颜色</label>
                            <Input
                                type="color"
                                value={fontColor}
                                onChange={(e) => setFontColor(e.target.value)}
                                style={{ width: '100%' }}
                            />
                        </div>
250

251 252 253 254 255 256 257 258
                        <div className="form-group">
                            <label>文字对齐</label>
                            <Select value={textAlign} onChange={setTextAlign} style={{ width: '100%' }}>
                                <Option value="left">居左</Option>
                                <Option value="center">居中</Option>
                                <Option value="right">居右</Option>
                            </Select>
                        </div>
259

260 261 262 263 264 265 266 267 268 269
                        <div className="form-group">
                            <label>行间距</label>
                            <Slider
                                min={1}
                                max={3}
                                step={0.1}
                                value={lineSpacing}
                                onChange={setLineSpacing}
                            />
                        </div>
270

271 272 273 274 275 276 277
                        <div className="form-group">
                            <label>字符间距 (px)</label>
                            <Slider
                                min={0}
                                max={10}
                                value={charSpacing}
                                onChange={setCharSpacing}
278
                            />
279
                        </div>
280

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
                        <Button type="primary" onClick={handleGenerate} style={{ width: '100%' }}>
                            生成图片
                        </Button>
                    </div>
                </Sider>
                <Layout>
                    <Content style={{ padding: '1rem' }}>
                        <Row gutter={24}>
                            <Col xs={24} lg={12}>
                                <div className="section-title">输入文本</div>
                                <TextArea
                                    rows={15}
                                    value={text}
                                    onChange={(e) => setText(e.target.value)}
                                    placeholder="请输入您的文本..."
                                    style={{ borderRadius: '16px', padding: '1rem' }}
                                />
                            </Col>
                            <Col xs={24} lg={12}>
                                <div className="section-title">预览</div>
                                <div
                                    className="preview-area"
                                    style={{
                                        fontFamily: font,
                                        fontSize: `${fontSize}px`,
                                        color: fontColor,
                                        textAlign: textAlign,
                                        paddingTop: `${topMargin}px`,
                                        paddingLeft: `${leftMargin}px`,
                                        paddingRight: `${rightMargin}px`,
                                        lineHeight: `${lineSpacing * fontSize}px`,
                                        letterSpacing: `${charSpacing}px`,
                                        border: borderEnabled ? '1px solid #000' : 'none',
                                        backgroundImage: getPaperBackground(),
                                        backgroundSize: getBackgroundSize(),
                                        backgroundRepeat: getBackgroundRepeat(),
                                        backgroundPosition: `left ${backgroundOffset}px`,
                                        minHeight: '400px',
                                    }}
                                >
                                    {text.split('\n').map((line, index) => (
                                        <p key={index} style={{ margin: 0 }}>{line}</p>
                                    ))}
                                </div>
                            </Col>
                        </Row>
                    </Content>
                </Layout>
329
            </Layout>
fisherdaddy's avatar
fisherdaddy committed
330
        </>
331 332 333 334
    );
}

export default HandwritingGenerator;