Files
go-pixelnebula/animation/fade.go
2025-03-19 21:10:19 +08:00

51 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package animation
import (
"fmt"
)
// FadeAnimation 淡入淡出动画
type FadeAnimation struct {
BaseAnimation
From string // 起始透明度
To string // 结束透明度
}
// NewFadeAnimation 创建一个淡入淡出动画
func NewFadeAnimation(targetID string, from, to string, duration float64, repeatCount int) *FadeAnimation {
return &FadeAnimation{
BaseAnimation: BaseAnimation{
Type: Fade,
Duration: duration,
RepeatCount: repeatCount,
Delay: 0,
TargetID: targetID,
Attributes: make(map[string]string),
},
From: from,
To: to,
}
}
// GenerateSVG 生成淡入淡出动画的SVG代码
func (a *FadeAnimation) GenerateSVG() string {
// 创建一个animate元素并将其添加到目标元素中
svg := fmt.Sprintf("<animate href=\"#%s\" attributeName=\"opacity\" ", a.TargetID)
svg += fmt.Sprintf("from=\"%s\" to=\"%s\" ", a.From, a.To)
svg += fmt.Sprintf("dur=\"%gs\" ", a.Duration)
if a.RepeatCount < 0 {
svg += "repeatCount=\"indefinite\" "
} else if a.RepeatCount > 0 {
svg += fmt.Sprintf("repeatCount=\"%d\" ", a.RepeatCount)
}
if a.Delay > 0 {
svg += fmt.Sprintf("begin=\"%gs\" ", a.Delay)
}
svg += "/>\n"
return svg
}