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

53 lines
1.4 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"
)
// TransformAnimation 变换动画
type TransformAnimation struct {
BaseAnimation
TransformType string // 变换类型scale, translate等
From string // 起始变换值
To string // 结束变换值
}
// NewTransformAnimation 创建一个变换动画
func NewTransformAnimation(targetID string, transformType string, from, to string, duration float64, repeatCount int) *TransformAnimation {
return &TransformAnimation{
BaseAnimation: BaseAnimation{
Type: Transform,
Duration: duration,
RepeatCount: repeatCount,
Delay: 0,
TargetID: targetID,
Attributes: make(map[string]string),
},
TransformType: transformType,
From: from,
To: to,
}
}
// GenerateSVG 生成变换动画的SVG代码
func (a *TransformAnimation) GenerateSVG() string {
// 创建一个animateTransform元素并将其添加到目标元素中
svg := fmt.Sprintf("<animateTransform href=\"#%s\" attributeName=\"transform\" attributeType=\"XML\" type=\"%s\" ", a.TargetID, a.TransformType)
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 += "additive=\"sum\" />\n"
return svg
}