@@ -312,8 +312,30 @@ const Home = () => {
312312
313313 const images = slugs . map ( ( slug ) => `https://cdn.simpleicons.org/${ slug } /${ slug } ` )
314314
315+ // 使用 useRef 来引用 SVG 元素
316+ const svgRef = useRef ( null )
317+ const saveSvgAsFile = ( ) => {
318+ if ( svgRef . current ) {
319+ // 获取 SVG 元素的 outerHTML
320+ const svgContent = svgRef . current . outerHTML
321+ // 创建 Blob 对象
322+ const blob = new Blob ( [ svgContent ] , { type : 'image/svg+xml' } )
323+ // 创建下载链接
324+ const url = URL . createObjectURL ( blob )
325+ // 创建 <a> 元素
326+ const a = document . createElement ( 'a' )
327+ a . href = url
328+ a . download = 'mySvg.svg'
329+ // 模拟点击下载链接
330+ a . click ( )
331+ // 释放临时 URL
332+ URL . revokeObjectURL ( url )
333+ }
334+ }
335+
315336 return (
316337 < FixTabPanel ref = { scrollRef } >
338+ < button onClick = { saveSvgAsFile } > 保存 SVG</ button >
317339 < section className = { styles . avatar } style = { { margin : '10px 0' , fontSize : 24 } } >
318340 < ColorfulText text = { `React version: ${ version } ` } />
319341 </ section >
@@ -505,7 +527,7 @@ const Home = () => {
505527 >
506528 < section className = "relative p-4" >
507529 < div className = { styles . itemCircle } />
508- < svg style = { { height : '10px' , width : '100%' } } >
530+ < svg ref = { svgRef } style = { { height : '10px' , width : '100%' } } >
509531 < path
510532 d = "M 0 0 L 5000 0"
511533 stroke-miterlimit = "10"
@@ -526,31 +548,6 @@ const Home = () => {
526548 </ svg >
527549 < div className = { styles . itemCircleTail } />
528550 </ section >
529-
530- < svg width = "100%" height = "100%" viewBox = "0 0 620 445" fill = "none" xmlns = "http://www.w3.org/2000/svg" >
531- < linearGradient id = "paint0_linear" x1 = "277" y1 = "0" x2 = "277" y2 = "202" gradientUnits = "userSpaceOnUse" >
532- < stop stop-color = "#FF7383" > </ stop >
533- < stop offset = "1" stop-color = "#2F48EA" > </ stop >
534- </ linearGradient >
535- < rect
536- x = "3"
537- y = "3"
538- width = "610"
539- height = "400"
540- stroke = "url(#paint0_linear)"
541- stroke-width = "5"
542- stroke-linecap = "round"
543- stroke-dasharray = "31 31"
544- rx = "25"
545- > </ rect >
546- < animate
547- attributeName = "stroke-dashoffset"
548- values = "370;0"
549- dur = "3s"
550- calcMode = "linear"
551- repeatCount = "indefinite"
552- > </ animate >
553- </ svg >
554551 </ section >
555552 < section className = { styles . line } />
556553 < section className = { styles . linear } />
0 commit comments