import React, { useState, useEffect } from 'react'; import { usePageLoading } from '../hooks/usePageLoading'; import LoadingOverlay from './LoadingOverlay'; 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() { const isLoading = usePageLoading(); 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); useEffect(() => { // Clear any loading states from previous navigation const clearLoadingState = () => { const event = new CustomEvent('clearLoadingState'); window.dispatchEvent(event); }; clearLoadingState(); }, []); 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 ( <> {isLoading && } 手写字体生成器 手写字体 新叶念体 CC 字体 兄弟字体 Zhong Qi Shan 体 纸张类型 无纸张 横线纸 {/* 新增纸张背景选项 */} 纸张背景 无背景 样式1 样式2 样式3 setBorderEnabled(!borderEnabled)}> 边框 边距设置 (px) setTopMargin(e.target.value)} placeholder="上" /> setLeftMargin(e.target.value)} placeholder="左" /> setRightMargin(e.target.value)} placeholder="右" /> 字体大小 (px) 字体颜色 setFontColor(e.target.value)} style={{ width: '100%' }} /> 文字对齐 居左 居中 居右 行间距 字符间距 (px) 生成图片 输入文本 setText(e.target.value)} placeholder="请输入您的文本..." style={{ borderRadius: '16px', padding: '1rem' }} /> 预览 {text.split('\n').map((line, index) => ( {line} ))} > ); } export default HandwritingGenerator;
{line}