🎉 Initial commit
This commit is contained in:
59
examples/01_basic_usage.go
Normal file
59
examples/01_basic_usage.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/landaiqing/go-pixelnebula"
|
||||
"github.com/landaiqing/go-pixelnebula/style"
|
||||
)
|
||||
|
||||
// 基本用法示例
|
||||
// 展示如何创建简单的PixelNebula头像
|
||||
func main() {
|
||||
// 创建一个新的PixelNebula实例
|
||||
pn := pixelnebula.NewPixelNebula()
|
||||
|
||||
// 设置风格 - 这里使用默认的AfrohairStyle风格
|
||||
pn.WithStyle(style.AfrohairStyle)
|
||||
|
||||
// 设置主题索引 - 每种风格有多个主题可选
|
||||
pn.WithTheme(0)
|
||||
|
||||
// 设置头像尺寸 (宽度, 高度)
|
||||
pn.WithSize(300, 300)
|
||||
|
||||
// 生成SVG - 需要提供唯一ID和是否生成无环境模式的参数
|
||||
// 第一个参数:唯一标识符,用于生成不同的头像
|
||||
// 第二个参数:是否为无环境模式,true表示不生成背景环境
|
||||
svg, err := pn.Generate("my-unique-id-123", false).ToSVG()
|
||||
if err != nil {
|
||||
fmt.Printf("生成SVG失败: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// 保存到文件
|
||||
err = os.WriteFile("basic_avatar.svg", []byte(svg), 0644)
|
||||
if err != nil {
|
||||
fmt.Printf("保存文件失败: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println("成功生成基本头像: basic_avatar.svg")
|
||||
|
||||
// 再生成一个无环境模式的头像
|
||||
svgNoEnv, err := pn.Generate("my-unique-id-123", true).ToSVG()
|
||||
if err != nil {
|
||||
fmt.Printf("生成无环境SVG失败: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// 保存到文件
|
||||
err = os.WriteFile("basic_avatar_no_env.svg", []byte(svgNoEnv), 0644)
|
||||
if err != nil {
|
||||
fmt.Printf("保存文件失败: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println("成功生成无环境头像: basic_avatar_no_env.svg")
|
||||
}
|
83
examples/02_styles_and_themes.go
Normal file
83
examples/02_styles_and_themes.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/landaiqing/go-pixelnebula"
|
||||
"github.com/landaiqing/go-pixelnebula/style"
|
||||
)
|
||||
|
||||
// 风格和主题示例
|
||||
// 展示如何使用不同的风格和主题生成多个头像
|
||||
func main() {
|
||||
// 创建一个新的PixelNebula实例
|
||||
pn := pixelnebula.NewPixelNebula()
|
||||
|
||||
// 定义要展示的风格数组
|
||||
styles := []style.StyleType{
|
||||
style.AteamStyle,
|
||||
style.GirlStyle,
|
||||
style.CountryStyle,
|
||||
style.GeeknotStyle,
|
||||
style.PunkStyle,
|
||||
// 可以添加更多内置风格
|
||||
}
|
||||
|
||||
// 为每种风格生成不同主题的头像
|
||||
for styleIndex, styleType := range styles {
|
||||
// 设置当前风格
|
||||
pn.WithStyle(styleType)
|
||||
|
||||
// 获取风格名称用于文件命名
|
||||
var styleName string
|
||||
switch styleType {
|
||||
case style.AteamStyle:
|
||||
styleName = "ateam"
|
||||
case style.GirlStyle:
|
||||
styleName = "girl"
|
||||
case style.CountryStyle:
|
||||
styleName = "country"
|
||||
case style.GeeknotStyle:
|
||||
styleName = "geeknot"
|
||||
case style.PunkStyle:
|
||||
styleName = "punk"
|
||||
default:
|
||||
styleName = "unknown"
|
||||
}
|
||||
|
||||
// 对每种风格,生成3个不同主题的头像
|
||||
for themeIndex := 0; themeIndex < 3; themeIndex++ {
|
||||
// 设置主题
|
||||
pn.WithTheme(themeIndex)
|
||||
|
||||
// 设置尺寸
|
||||
pn.WithSize(200, 200)
|
||||
|
||||
// 生成唯一ID - 这里使用风格和主题索引组合
|
||||
uniqueID := "style-" + strconv.Itoa(styleIndex) + "-theme-" + strconv.Itoa(themeIndex)
|
||||
|
||||
// 生成SVG
|
||||
svg, err := pn.Generate(uniqueID, false).ToSVG()
|
||||
if err != nil {
|
||||
fmt.Printf("生成风格%s主题%d的SVG失败: %v\n", styleName, themeIndex, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// 文件名
|
||||
filename := fmt.Sprintf("%s_theme_%d.svg", styleName, themeIndex)
|
||||
|
||||
// 保存到文件
|
||||
err = os.WriteFile(filename, []byte(svg), 0644)
|
||||
if err != nil {
|
||||
fmt.Printf("保存文件%s失败: %v\n", filename, err)
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Printf("成功生成头像: %s\n", filename)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("所有风格和主题头像生成完成!")
|
||||
}
|
120
examples/03_custom_theme_and_style.go
Normal file
120
examples/03_custom_theme_and_style.go
Normal file
@@ -0,0 +1,120 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/landaiqing/go-pixelnebula"
|
||||
"github.com/landaiqing/go-pixelnebula/style"
|
||||
"github.com/landaiqing/go-pixelnebula/theme"
|
||||
)
|
||||
|
||||
// 自定义主题和风格示例
|
||||
// 展示如何创建自定义主题和风格
|
||||
func main() {
|
||||
// 创建一个新的PixelNebula实例
|
||||
pn := pixelnebula.NewPixelNebula()
|
||||
|
||||
// 1. 自定义主题示例
|
||||
// 创建自定义主题 - 每个主题包含各部分的颜色设置
|
||||
customThemes := []theme.Theme{
|
||||
{
|
||||
theme.ThemePart{
|
||||
// 环境部分颜色
|
||||
"env": []string{"#FF5733", "#C70039"},
|
||||
// 头部颜色
|
||||
"head": []string{"#FFC300", "#FF5733"},
|
||||
// 衣服颜色
|
||||
"clo": []string{"#2E86C1", "#1A5276"},
|
||||
// 眼睛颜色
|
||||
"eyes": []string{"#000000", "#FFFFFF"},
|
||||
// 嘴巴颜色
|
||||
"mouth": []string{"#E74C3C"},
|
||||
// 头顶装饰颜色
|
||||
"top": []string{"#884EA0", "#7D3C98"},
|
||||
},
|
||||
theme.ThemePart{
|
||||
// 另一个主题配色
|
||||
"env": []string{"#3498DB", "#2874A6"},
|
||||
"head": []string{"#F5CBA7", "#F0B27A"},
|
||||
"clo": []string{"#27AE60", "#196F3D"},
|
||||
"eyes": []string{"#2C3E50", "#FDFEFE"},
|
||||
"mouth": []string{"#CB4335"},
|
||||
"top": []string{"#D35400", "#BA4A00"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// 应用自定义主题
|
||||
pn.WithCustomizeTheme(customThemes)
|
||||
|
||||
// 生成使用自定义主题的头像
|
||||
pn.WithSize(250, 250)
|
||||
pn.WithTheme(0)
|
||||
|
||||
// 生成第一个自定义主题的头像
|
||||
svg1, err := pn.Generate("custom-theme-1", false).SetTheme(0).ToSVG()
|
||||
if err != nil {
|
||||
fmt.Printf("生成自定义主题1的SVG失败: %v\n", err)
|
||||
} else {
|
||||
// 保存到文件
|
||||
err = os.WriteFile("custom_theme_1.svg", []byte(svg1), 0644)
|
||||
if err != nil {
|
||||
fmt.Printf("保存自定义主题1文件失败: %v\n", err)
|
||||
} else {
|
||||
fmt.Println("成功生成自定义主题1头像: custom_theme_1.svg")
|
||||
}
|
||||
}
|
||||
|
||||
// 生成第二个自定义主题的头像
|
||||
svg2, err := pn.Generate("custom-theme-2", false).SetTheme(1).ToSVG()
|
||||
if err != nil {
|
||||
fmt.Printf("生成自定义主题2的SVG失败: %v\n", err)
|
||||
} else {
|
||||
// 保存到文件
|
||||
err = os.WriteFile("custom_theme_2.svg", []byte(svg2), 0644)
|
||||
if err != nil {
|
||||
fmt.Printf("保存自定义主题2文件失败: %v\n", err)
|
||||
} else {
|
||||
fmt.Println("成功生成自定义主题2头像: custom_theme_2.svg")
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 自定义风格示例
|
||||
// 创建一个新的PixelNebula实例,用于自定义风格
|
||||
pn2 := pixelnebula.NewPixelNebula()
|
||||
|
||||
// 创建自定义风格 - 每种风格包含不同形状部件的SVG路径
|
||||
// 注意:这里仅作示例,实际使用中需要提供完整的SVG路径数据
|
||||
customStyles := []style.StyleSet{
|
||||
{
|
||||
// 第一种自定义风格
|
||||
style.TypeEnv: `<circle cx="50%" cy="50%" r="48%" fill="#FILL0;"></circle>`,
|
||||
style.TypeHead: `<circle cx="50%" cy="50%" r="35%" fill="#FILL0;"></circle>`,
|
||||
style.TypeClo: `<rect x="25%" y="65%" width="50%" height="30%" fill="#FILL0;"></rect>`,
|
||||
style.TypeEyes: `<circle cx="40%" cy="45%" r="5%" fill="#FILL0;"></circle><circle cx="60%" cy="45%" r="5%" fill="#FILL1;"></circle>`,
|
||||
style.TypeMouth: `<path d="M 40% 60% Q 50% 70% 60% 60%" stroke="#FILL0;" stroke-width="2" fill="none"></path>`,
|
||||
style.TypeTop: `<path d="M 30% 30% L 50% 10% L 70% 30%" stroke="#FILL0;" stroke-width="4" fill="#FILL1;"></path>`,
|
||||
},
|
||||
}
|
||||
|
||||
// 应用自定义风格
|
||||
pn2.WithCustomizeStyle(customStyles)
|
||||
pn2.WithSize(250, 250)
|
||||
|
||||
// 使用自定义风格生成头像
|
||||
svg3, err := pn2.Generate("custom-style", false).SetStyleByIndex(0).ToSVG()
|
||||
if err != nil {
|
||||
fmt.Printf("生成自定义风格的SVG失败: %v\n", err)
|
||||
} else {
|
||||
// 保存到文件
|
||||
err = os.WriteFile("custom_style.svg", []byte(svg3), 0644)
|
||||
if err != nil {
|
||||
fmt.Printf("保存自定义风格文件失败: %v\n", err)
|
||||
} else {
|
||||
fmt.Println("成功生成自定义风格头像: custom_style.svg")
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("自定义主题和风格示例完成!")
|
||||
}
|
68
examples/04_all_animations.go
Normal file
68
examples/04_all_animations.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/landaiqing/go-pixelnebula"
|
||||
"github.com/landaiqing/go-pixelnebula/style"
|
||||
)
|
||||
|
||||
// 所有动画效果示例
|
||||
// 展示PixelNebula支持的所有动画类型
|
||||
func main() {
|
||||
// 创建一个新的PixelNebula实例
|
||||
pn := pixelnebula.NewPixelNebula()
|
||||
|
||||
// 设置风格和尺寸
|
||||
pn.WithStyle(style.AfrohairStyle)
|
||||
pn.WithTheme(0)
|
||||
pn.WithSize(300, 300)
|
||||
|
||||
// 1. 旋转动画 - 让环境和头部旋转
|
||||
pn.WithRotateAnimation("env", 0, 360, 10, -1) // 无限循环旋转环境
|
||||
pn.WithRotateAnimation("head", 0, 360, 15, -1) // 无限循环旋转头部
|
||||
|
||||
// 2. 渐变动画 - 给环境添加渐变色
|
||||
pn.WithGradientAnimation("env", []string{"#3498db", "#2ecc71", "#f1c40f", "#e74c3c", "#9b59b6"}, 8, -1, true)
|
||||
|
||||
// 3. 淡入淡出动画 - 让眼睛闪烁
|
||||
pn.WithFadeAnimation("eyes", "1", "0.3", 2, -1)
|
||||
|
||||
// 4. 变换动画 - 让嘴巴缩放
|
||||
pn.WithTransformAnimation("mouth", "scale", "1 1", "1.2 1.2", 1, -1)
|
||||
|
||||
// 5. 颜色变换动画 - 让头顶装饰变色
|
||||
pn.WithColorAnimation("top", "fill", "#9b59b6", "#e74c3c", 3, -1)
|
||||
|
||||
// 6. 弹跳动画 - 让整个头像上下弹跳
|
||||
pn.WithBounceAnimation("head", "translateY", "0", "-10", 3, 5, -1)
|
||||
|
||||
// 7. 波浪动画 - 让衣服产生波浪效果
|
||||
pn.WithWaveAnimation("clo", 5, 0.2, "horizontal", 4, -1)
|
||||
|
||||
// 8. 闪烁动画 - 让头顶装饰闪烁
|
||||
pn.WithBlinkAnimation("top", 0.3, 1.0, 4, 6, -1)
|
||||
|
||||
// 9. 路径动画 - 让眼睛沿着路径移动
|
||||
pn.WithPathAnimation("eyes", "M 0,0 C 10,-10 -10,-10 0,0", 3, -1)
|
||||
|
||||
// 10. 带旋转的路径动画 - 让眼睛在移动的同时旋转
|
||||
pn.WithPathAnimationRotate("mouth", "M 0,0 C 5,5 -5,5 0,0", "auto", 4, -1)
|
||||
|
||||
// 生成SVG
|
||||
svg, err := pn.Generate("all-animations-example", false).ToSVG()
|
||||
if err != nil {
|
||||
fmt.Printf("生成SVG失败: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// 保存到文件
|
||||
err = os.WriteFile("all_animations.svg", []byte(svg), 0644)
|
||||
if err != nil {
|
||||
fmt.Printf("保存文件失败: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println("成功生成包含所有动画效果的头像: all_animations.svg")
|
||||
}
|
108
examples/05_svg_builder_chain.go
Normal file
108
examples/05_svg_builder_chain.go
Normal file
@@ -0,0 +1,108 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/landaiqing/go-pixelnebula"
|
||||
"github.com/landaiqing/go-pixelnebula/style"
|
||||
)
|
||||
|
||||
// SVG构建器链式调用示例
|
||||
// 展示如何使用链式调用API创建头像
|
||||
func main() {
|
||||
// 创建一个新的PixelNebula实例
|
||||
pn := pixelnebula.NewPixelNebula().WithDefaultCache()
|
||||
|
||||
// 示例1: 基本链式调用
|
||||
// 使用链式调用创建并保存头像
|
||||
svg1, err := pn.Generate("chain-example-1", false).
|
||||
SetStyle(style.AfrohairStyle).
|
||||
SetTheme(0).
|
||||
SetSize(200, 200).
|
||||
ToSVG()
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("生成基本链式调用SVG失败: %v\n", err)
|
||||
} else {
|
||||
// 保存到文件
|
||||
err = os.WriteFile("basic_chain.svg", []byte(svg1), 0644)
|
||||
if err != nil {
|
||||
fmt.Printf("保存基本链式调用SVG文件失败: %v\n", err)
|
||||
} else {
|
||||
fmt.Println("成功生成基本链式调用头像: basic_chain.svg")
|
||||
}
|
||||
}
|
||||
|
||||
// 示例2: 带动画的链式调用
|
||||
// 使用链式调用添加多种动画效果
|
||||
svg2, err := pn.Generate("chain-example-2", false).
|
||||
SetStyle(style.GirlStyle).
|
||||
SetTheme(1).
|
||||
SetSize(300, 300).
|
||||
// 添加旋转动画
|
||||
SetRotateAnimation("env", 0, 360, 10, -1).
|
||||
// 添加淡入淡出动画
|
||||
SetFadeAnimation("eyes", "1", "0.3", 2, -1).
|
||||
// 添加变换动画
|
||||
SetTransformAnimation("mouth", "scale", "1 1", "1.2 1.2", 1, -1).
|
||||
// 添加颜色变换动画
|
||||
SetColorAnimation("top", "fill", "#9b59b6", "#e74c3c", 3, -1).
|
||||
// 构建并获取SVG
|
||||
ToSVG()
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("生成带动画的链式调用SVG失败: %v\n", err)
|
||||
} else {
|
||||
// 保存到文件
|
||||
err = os.WriteFile("animated_chain.svg", []byte(svg2), 0644)
|
||||
if err != nil {
|
||||
fmt.Printf("保存带动画的链式调用SVG文件失败: %v\n", err)
|
||||
} else {
|
||||
fmt.Println("成功生成带动画的链式调用头像: animated_chain.svg")
|
||||
}
|
||||
}
|
||||
|
||||
// 示例3: 直接保存到文件的链式调用
|
||||
err = pn.Generate("chain-example-3", false).
|
||||
SetStyle(style.BlondStyle).
|
||||
SetTheme(2).
|
||||
SetSize(250, 250).
|
||||
// 添加波浪动画
|
||||
SetWaveAnimation("clo", 5, 0.2, "horizontal", 4, -1).
|
||||
// 添加闪烁动画
|
||||
SetBlinkAnimation("top", 0.3, 1.0, 4, 6, -1).
|
||||
// 构建并直接保存到文件
|
||||
Build().
|
||||
ToFile("direct_file_chain.svg")
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("直接保存到文件的链式调用失败: %v\n", err)
|
||||
} else {
|
||||
fmt.Println("成功生成并直接保存头像到文件: direct_file_chain.svg")
|
||||
}
|
||||
|
||||
// 示例4: 转换为Base64的链式调用
|
||||
base64, err := pn.Generate("chain-example-4", false).
|
||||
SetStyle(style.BlondStyle).
|
||||
SetTheme(0).
|
||||
SetSize(200, 200).
|
||||
// 添加旋转动画
|
||||
SetRotateAnimation("head", 0, 360, 15, -1).
|
||||
// 构建并转换为Base64
|
||||
ToBase64()
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("转换为Base64的链式调用失败: %v\n", err)
|
||||
} else {
|
||||
// 保存Base64编码到文件
|
||||
err = os.WriteFile("base64_avatar.txt", []byte(base64), 0644)
|
||||
if err != nil {
|
||||
fmt.Printf("保存Base64编码到文件失败: %v\n", err)
|
||||
} else {
|
||||
fmt.Println("成功生成Base64编码头像并保存到文件: base64_avatar.txt")
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("SVG构建器链式调用示例完成!")
|
||||
}
|
223
examples/06_cache_system.go
Normal file
223
examples/06_cache_system.go
Normal file
@@ -0,0 +1,223 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/landaiqing/go-pixelnebula"
|
||||
"github.com/landaiqing/go-pixelnebula/cache"
|
||||
"github.com/landaiqing/go-pixelnebula/style"
|
||||
)
|
||||
|
||||
// 缓存系统示例
|
||||
// 展示如何使用PixelNebula的缓存功能
|
||||
func main() {
|
||||
// 1. 使用默认缓存
|
||||
fmt.Println("=== 使用默认缓存示例 ===")
|
||||
defaultCacheExample()
|
||||
|
||||
// 2. 使用自定义缓存
|
||||
fmt.Println("\n=== 使用自定义缓存示例 ===")
|
||||
customCacheExample()
|
||||
|
||||
// 3. 使用带监控的缓存
|
||||
fmt.Println("\n=== 使用带监控的缓存示例 ===")
|
||||
monitoredCacheExample()
|
||||
|
||||
// 4. 使用压缩缓存
|
||||
fmt.Println("\n=== 使用压缩缓存示例 ===")
|
||||
compressedCacheExample()
|
||||
}
|
||||
|
||||
// 使用默认缓存示例
|
||||
func defaultCacheExample() {
|
||||
// 创建一个带默认缓存的PixelNebula实例
|
||||
pn := pixelnebula.NewPixelNebula().WithDefaultCache()
|
||||
|
||||
// 设置基本属性
|
||||
pn.WithStyle(style.AfrohairStyle)
|
||||
pn.WithTheme(0)
|
||||
pn.WithSize(200, 200)
|
||||
|
||||
// 第一次生成头像 - 会存入缓存
|
||||
startTime1 := time.Now()
|
||||
_, err := pn.Generate("default-cache-example", false).ToSVG()
|
||||
if err != nil {
|
||||
fmt.Printf("生成SVG失败: %v\n", err)
|
||||
return
|
||||
}
|
||||
duration1 := time.Since(startTime1)
|
||||
|
||||
// 第二次生成相同头像 - 应该从缓存中获取
|
||||
startTime2 := time.Now()
|
||||
svg2, err := pn.Generate("default-cache-example", false).ToSVG()
|
||||
if err != nil {
|
||||
fmt.Printf("从缓存生成SVG失败: %v\n", err)
|
||||
return
|
||||
}
|
||||
duration2 := time.Since(startTime2)
|
||||
|
||||
// 保存第二次生成的头像
|
||||
err = os.WriteFile("default_cache.svg", []byte(svg2), 0644)
|
||||
if err != nil {
|
||||
fmt.Printf("保存缓存生成的SVG文件失败: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("第一次生成耗时: %v\n", duration1)
|
||||
fmt.Printf("第二次生成耗时: %v (使用缓存)\n", duration2)
|
||||
fmt.Printf("性能提升: %.2f倍\n", float64(duration1)/float64(duration2))
|
||||
fmt.Println("成功生成带默认缓存的头像: default_cache.svg")
|
||||
}
|
||||
|
||||
// 使用自定义缓存示例
|
||||
func customCacheExample() {
|
||||
// 创建自定义缓存选项
|
||||
customCacheOptions := cache.CacheOptions{
|
||||
Enabled: true,
|
||||
Size: 100, // 最大缓存条目数
|
||||
Expiration: 1 * time.Hour, // 缓存有效期
|
||||
}
|
||||
|
||||
// 创建一个带自定义缓存的PixelNebula实例
|
||||
pn := pixelnebula.NewPixelNebula().WithCache(customCacheOptions)
|
||||
|
||||
// 设置基本属性
|
||||
pn.WithStyle(style.GirlStyle)
|
||||
pn.WithTheme(1)
|
||||
pn.WithSize(200, 200)
|
||||
|
||||
// 第一次生成头像 - 会存入缓存
|
||||
startTime1 := time.Now()
|
||||
_, err := pn.Generate("custom-cache-example", false).ToSVG()
|
||||
if err != nil {
|
||||
fmt.Printf("生成SVG失败: %v\n", err)
|
||||
return
|
||||
}
|
||||
duration1 := time.Since(startTime1)
|
||||
|
||||
// 第二次生成相同头像 - 应该从缓存中获取
|
||||
startTime2 := time.Now()
|
||||
svg2, err := pn.Generate("custom-cache-example", false).ToSVG()
|
||||
if err != nil {
|
||||
fmt.Printf("从缓存生成SVG失败: %v\n", err)
|
||||
return
|
||||
}
|
||||
duration2 := time.Since(startTime2)
|
||||
|
||||
// 保存第二次生成的头像
|
||||
err = os.WriteFile("custom_cache.svg", []byte(svg2), 0644)
|
||||
if err != nil {
|
||||
fmt.Printf("保存缓存生成的SVG文件失败: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("第一次生成耗时: %v\n", duration1)
|
||||
fmt.Printf("第二次生成耗时: %v (使用缓存)\n", duration2)
|
||||
fmt.Printf("性能提升: %.2f倍\n", float64(duration1)/float64(duration2))
|
||||
fmt.Println("成功生成带自定义缓存的头像: custom_cache.svg")
|
||||
}
|
||||
|
||||
// 使用带监控的缓存示例
|
||||
func monitoredCacheExample() {
|
||||
// 创建带监控的缓存选项
|
||||
monitorOptions := cache.MonitorOptions{
|
||||
Enabled: true,
|
||||
SampleInterval: 5 * time.Second,
|
||||
}
|
||||
|
||||
// 创建一个带默认缓存和监控的PixelNebula实例
|
||||
pn := pixelnebula.NewPixelNebula().WithDefaultCache().WithMonitoring(monitorOptions)
|
||||
|
||||
// 设置基本属性
|
||||
pn.WithStyle(style.AsianStyle)
|
||||
pn.WithTheme(0)
|
||||
pn.WithSize(200, 200)
|
||||
|
||||
// 生成多个头像以展示监控效果
|
||||
for i := 0; i < 5; i++ {
|
||||
uniqueID := fmt.Sprintf("monitor-example-%d", i)
|
||||
svg, err := pn.Generate(uniqueID, false).ToSVG()
|
||||
if err != nil {
|
||||
fmt.Printf("生成SVG失败: %v\n", err)
|
||||
continue
|
||||
}
|
||||
|
||||
// 重复生成相同头像以测试缓存命中
|
||||
for j := 0; j < 3; j++ {
|
||||
_, err = pn.Generate(uniqueID, false).ToSVG()
|
||||
if err != nil {
|
||||
fmt.Printf("从缓存生成SVG失败: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 保存最后一个头像
|
||||
if i == 4 {
|
||||
err = os.WriteFile("monitored_cache.svg", []byte(svg), 0644)
|
||||
if err != nil {
|
||||
fmt.Printf("保存带监控缓存的SVG文件失败: %v\n", err)
|
||||
} else {
|
||||
fmt.Println("成功生成带监控缓存的头像: monitored_cache.svg")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 等待监控报告生成
|
||||
fmt.Println("等待监控报告生成...")
|
||||
time.Sleep(6 * time.Second)
|
||||
}
|
||||
|
||||
// 使用压缩缓存示例
|
||||
func compressedCacheExample() {
|
||||
// 创建压缩选项
|
||||
compressOptions := cache.CompressOptions{
|
||||
Enabled: true,
|
||||
Level: 6,
|
||||
MinSizeBytes: 100, // 最小压缩大小 (字节)
|
||||
}
|
||||
|
||||
// 创建一个带默认缓存和压缩的PixelNebula实例
|
||||
pn := pixelnebula.NewPixelNebula().WithDefaultCache().WithCompression(compressOptions)
|
||||
|
||||
// 设置基本属性
|
||||
pn.WithStyle(style.AfrohairStyle)
|
||||
pn.WithTheme(0)
|
||||
pn.WithSize(300, 300)
|
||||
|
||||
// 添加一些动画以增加SVG大小
|
||||
pn.WithRotateAnimation("env", 0, 360, 10, -1)
|
||||
pn.WithGradientAnimation("env", []string{"#3498db", "#2ecc71", "#f1c40f", "#e74c3c", "#9b59b6"}, 8, -1, true)
|
||||
pn.WithFadeAnimation("eyes", "1", "0.3", 2, -1)
|
||||
pn.WithTransformAnimation("mouth", "scale", "1 1", "1.2 1.2", 1, -1)
|
||||
|
||||
// 第一次生成头像 - 会存入压缩缓存
|
||||
startTime1 := time.Now()
|
||||
_, err := pn.Generate("compress-cache-example", false).ToSVG()
|
||||
if err != nil {
|
||||
fmt.Printf("生成SVG失败: %v\n", err)
|
||||
return
|
||||
}
|
||||
duration1 := time.Since(startTime1)
|
||||
|
||||
// 第二次生成相同头像 - 应该从压缩缓存中获取
|
||||
startTime2 := time.Now()
|
||||
svg2, err := pn.Generate("compress-cache-example", false).ToSVG()
|
||||
if err != nil {
|
||||
fmt.Printf("从压缩缓存生成SVG失败: %v\n", err)
|
||||
return
|
||||
}
|
||||
duration2 := time.Since(startTime2)
|
||||
|
||||
// 保存第二次生成的头像
|
||||
err = os.WriteFile("compressed_cache.svg", []byte(svg2), 0644)
|
||||
if err != nil {
|
||||
fmt.Printf("保存压缩缓存的SVG文件失败: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("第一次生成耗时: %v\n", duration1)
|
||||
fmt.Printf("第二次生成耗时: %v (使用压缩缓存)\n", duration2)
|
||||
fmt.Printf("性能提升: %.2f倍\n", float64(duration1)/float64(duration2))
|
||||
fmt.Println("成功生成带压缩缓存的头像: compressed_cache.svg")
|
||||
}
|
55
examples/07_format_conversion.go
Normal file
55
examples/07_format_conversion.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/landaiqing/go-pixelnebula"
|
||||
"github.com/landaiqing/go-pixelnebula/style"
|
||||
)
|
||||
|
||||
// 格式转换示例
|
||||
// 展示如何将SVG转换为其他格式
|
||||
func main() {
|
||||
// 创建一个新的PixelNebula实例
|
||||
pn := pixelnebula.NewPixelNebula()
|
||||
|
||||
// 设置基本属性
|
||||
pn.WithStyle(style.AfrohairStyle)
|
||||
pn.WithTheme(0)
|
||||
pn.WithSize(500, 500) // 使用较大尺寸以便转换后的图像清晰
|
||||
|
||||
// 添加一些动画效果
|
||||
pn.WithRotateAnimation("env", 0, 360, 10, -1)
|
||||
pn.WithGradientAnimation("env", []string{"#3498db", "#2ecc71", "#f1c40f"}, 5, -1, true)
|
||||
|
||||
// 1. 生成并保存SVG文件
|
||||
svgData, err := pn.Generate("format-conversion", false).ToSVG()
|
||||
if err != nil {
|
||||
fmt.Printf("生成SVG失败: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// 保存SVG文件
|
||||
err = os.WriteFile("format_conversion.svg", []byte(svgData), 0644)
|
||||
if err != nil {
|
||||
fmt.Printf("保存SVG文件失败: %v\n", err)
|
||||
} else {
|
||||
fmt.Println("成功生成SVG文件: format_conversion.svg")
|
||||
}
|
||||
|
||||
// 2. 转换为Base64格式
|
||||
base64Data, err := pn.Generate("format-conversion", false).ToBase64()
|
||||
if err != nil {
|
||||
fmt.Printf("转换为Base64失败: %v\n", err)
|
||||
} else {
|
||||
// 保存Base64数据到文件
|
||||
err = os.WriteFile("format_conversion.base64.txt", []byte(base64Data), 0644)
|
||||
if err != nil {
|
||||
fmt.Printf("保存Base64文件失败: %v\n", err)
|
||||
} else {
|
||||
fmt.Println("成功生成Base64编码文件: format_conversion.base64.txt")
|
||||
}
|
||||
}
|
||||
fmt.Println("格式转换示例完成!")
|
||||
}
|
33
examples/08_random_avatar_generator.go
Normal file
33
examples/08_random_avatar_generator.go
Normal file
@@ -0,0 +1,33 @@
|
||||
// 08_random_avatar_generator.go
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/landaiqing/go-pixelnebula"
|
||||
"math/rand"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
savePath = "random_generated_avatars.svg"
|
||||
)
|
||||
|
||||
// 随机生成头像
|
||||
// Note: 随机传入id即可随机生成头像,固定的id会生成相同的头像
|
||||
func main() {
|
||||
|
||||
// 创建随机数
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
randomInt := rand.Intn(100)
|
||||
|
||||
pixelNebula := pixelnebula.NewPixelNebula().WithDefaultCache()
|
||||
svg, err := pixelNebula.Generate(strconv.Itoa(randomInt), false).ToSVG()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// 保存图片
|
||||
os.WriteFile(savePath, []byte(svg), 0644)
|
||||
defer os.Remove(savePath)
|
||||
}
|
117
examples/README.md
Normal file
117
examples/README.md
Normal file
@@ -0,0 +1,117 @@
|
||||
# PixelNebula 示例
|
||||
|
||||
[英文版](README_EN.md) | 中文版
|
||||
|
||||
## 示例文件
|
||||
|
||||
这个目录包含了多个示例,展示了如何使用 PixelNebula 库的各种功能。每个示例都是独立的,你可以单独运行它们来了解特定的功能。
|
||||
|
||||
| 文件名 | 描述 |
|
||||
|--------|------|
|
||||
| [01_basic_usage.go](01_basic_usage.go) | 演示基本的头像生成,包括常规头像和无环境头像 |
|
||||
| [02_styles_and_themes.go](02_styles_and_themes.go) | 展示如何使用不同的样式和主题 |
|
||||
| [03_custom_theme_and_style.go](03_custom_theme_and_style.go) | 演示如何创建和使用自定义主题和样式 |
|
||||
| [04_all_animations.go](04_all_animations.go) | 展示所有支持的动画效果 |
|
||||
| [05_svg_builder_chain.go](05_svg_builder_chain.go) | 演示如何使用链式API生成SVG |
|
||||
| [06_cache_system.go](06_cache_system.go) | 展示缓存系统的功能,包括默认缓存、自定义缓存和监控 |
|
||||
| [07_format_conversion.go](07_format_conversion.go) | 展示如何将SVG转换为其他格式 |
|
||||
| [08_random_avatar_generator.go](08_random_avatar_generator.go) | 交互式的随机头像生成器,支持多种样式、主题和输出格式 |
|
||||
|
||||
## 如何运行示例
|
||||
|
||||
确保您已正确安装 Go 环境并设置了 GOPATH。然后,按照以下步骤运行示例:
|
||||
|
||||
### 运行单个示例
|
||||
|
||||
```bash
|
||||
# 例如,运行基本用法示例
|
||||
go run 01_basic_usage.go
|
||||
```
|
||||
|
||||
### 运行所有示例
|
||||
|
||||
```bash
|
||||
for file in *_*.go; do
|
||||
echo "🚀 运行示例: $file"
|
||||
go run $file
|
||||
echo "------------------------"
|
||||
done
|
||||
```
|
||||
|
||||
## 示例说明
|
||||
|
||||
### 01_basic_usage.go
|
||||
|
||||
这个示例展示了 PixelNebula 的基本功能,包括:
|
||||
|
||||
- 创建一个基本的头像
|
||||
- 生成一个无环境的头像
|
||||
- 处理错误和保存文件
|
||||
|
||||
### 02_styles_and_themes.go
|
||||
|
||||
这个示例展示了如何使用不同的样式和主题:
|
||||
|
||||
- 使用预定义的样式生成头像
|
||||
- 应用不同的主题
|
||||
- 组合样式和主题
|
||||
|
||||
### 03_custom_theme_and_style.go
|
||||
|
||||
这个示例展示了如何创建和使用自定义主题和样式:
|
||||
|
||||
- 创建自定义颜色主题
|
||||
- 定义自定义样式
|
||||
- 组合自定义主题和样式
|
||||
|
||||
### 04_all_animations.go
|
||||
|
||||
这个示例展示了所有支持的动画效果:
|
||||
|
||||
- 旋转动画
|
||||
- 渐变动画
|
||||
- 淡入淡出效果
|
||||
- 变换动画
|
||||
- 颜色变换
|
||||
- 弹跳效果
|
||||
- 波浪动画
|
||||
- 闪烁效果
|
||||
- 路径动画
|
||||
|
||||
### 05_svg_builder_chain.go
|
||||
|
||||
这个示例展示了如何使用链式API:
|
||||
|
||||
- 使用链式调用创建简单的SVG
|
||||
- 添加动画效果
|
||||
- 直接保存到文件
|
||||
- 转换为Base64
|
||||
|
||||
### 06_cache_system.go
|
||||
|
||||
这个示例展示了缓存系统的功能:
|
||||
|
||||
- 使用默认缓存
|
||||
- 配置自定义缓存
|
||||
- 监控缓存性能
|
||||
- 使用压缩缓存
|
||||
|
||||
### 07_format_conversion.go
|
||||
|
||||
这个示例展示了格式转换功能:
|
||||
|
||||
- 转换为Base64
|
||||
- 其他格式暂未找到完美解决方案,欢迎 PR
|
||||
|
||||
### 08_random_avatar_generator.go
|
||||
|
||||
这个示例是一个交互式的随机头像生成器:
|
||||
|
||||
- 随机生成不同样式和主题的头像
|
||||
|
||||
## 提示
|
||||
|
||||
- 每个示例文件顶部都有详细的注释,解释了该示例所展示的功能
|
||||
- 如果遇到任何问题,请检查文件中的错误处理部分
|
||||
- 生成的头像会保存在示例代码指定的位置
|
||||
- 有些示例可能需要创建目录来保存生成的文件
|
117
examples/README_EN.md
Normal file
117
examples/README_EN.md
Normal file
@@ -0,0 +1,117 @@
|
||||
# PixelNebula Examples
|
||||
|
||||
[中文版](README.md) | English
|
||||
|
||||
## Example Files
|
||||
|
||||
This directory contains multiple examples showcasing various features of the PixelNebula library. Each example is standalone and can be run separately to understand specific functionalities.
|
||||
|
||||
| Filename | Description |
|
||||
|----------|-------------|
|
||||
| [01_basic_usage.go](01_basic_usage.go) | Demonstrates basic avatar generation, including regular and no-environment avatars |
|
||||
| [02_styles_and_themes.go](02_styles_and_themes.go) | Shows how to use different styles and themes |
|
||||
| [03_custom_theme_and_style.go](03_custom_theme_and_style.go) | Demonstrates how to create and use custom themes and styles |
|
||||
| [04_all_animations.go](04_all_animations.go) | Showcases all supported animation effects |
|
||||
| [05_svg_builder_chain.go](05_svg_builder_chain.go) | Demonstrates how to use the chainable API to generate SVGs |
|
||||
| [06_cache_system.go](06_cache_system.go) | Shows the cache system functionality, including default, custom, and monitored caching |
|
||||
| [07_format_conversion.go](07_format_conversion.go) | Shows how to convert SVGs to other formats |
|
||||
| [08_random_avatar_generator.go](08_random_avatar_generator.go) | Interactive random avatar generator with support for multiple styles, themes, and output formats |
|
||||
|
||||
## How to Run Examples
|
||||
|
||||
Ensure you have Go properly installed and GOPATH set correctly. Then, follow these steps to run the examples:
|
||||
|
||||
### Run a Single Example
|
||||
|
||||
```bash
|
||||
# For example, run the basic usage example
|
||||
go run 01_basic_usage.go
|
||||
```
|
||||
|
||||
### Run All Examples
|
||||
|
||||
```bash
|
||||
for file in *_*.go; do
|
||||
echo "🚀 Running example: $file"
|
||||
go run $file
|
||||
echo "------------------------"
|
||||
done
|
||||
```
|
||||
|
||||
## Example Details
|
||||
|
||||
### 01_basic_usage.go
|
||||
|
||||
This example demonstrates the basic functionality of PixelNebula, including:
|
||||
|
||||
- Creating a basic avatar
|
||||
- Generating a no-environment avatar
|
||||
- Handling errors and saving files
|
||||
|
||||
### 02_styles_and_themes.go
|
||||
|
||||
This example shows how to use different styles and themes:
|
||||
|
||||
- Using predefined styles to generate avatars
|
||||
- Applying different themes
|
||||
- Combining styles and themes
|
||||
|
||||
### 03_custom_theme_and_style.go
|
||||
|
||||
This example shows how to create and use custom themes and styles:
|
||||
|
||||
- Creating custom color themes
|
||||
- Defining custom styles
|
||||
- Combining custom themes and styles
|
||||
|
||||
### 04_all_animations.go
|
||||
|
||||
This example showcases all supported animation effects:
|
||||
|
||||
- Rotation animation
|
||||
- Gradient animation
|
||||
- Fade-in/out effects
|
||||
- Transform animation
|
||||
- Color transformation
|
||||
- Bounce effects
|
||||
- Wave animation
|
||||
- Blink effects
|
||||
- Path animation
|
||||
|
||||
### 05_svg_builder_chain.go
|
||||
|
||||
This example shows how to use the chainable API:
|
||||
|
||||
- Using chain calls to create simple SVGs
|
||||
- Adding animation effects
|
||||
- Saving directly to file
|
||||
- Converting to Base64
|
||||
|
||||
### 06_cache_system.go
|
||||
|
||||
This example demonstrates the cache system functionality:
|
||||
|
||||
- Using the default cache
|
||||
- Configuring custom caches
|
||||
- Monitoring cache performance
|
||||
- Using compressed caching
|
||||
|
||||
### 07_format_conversion.go
|
||||
|
||||
This example shows the format conversion capabilities:
|
||||
|
||||
- Converting to Base64
|
||||
- We haven't found a perfect solution for other formats yet, please feel free to make a PR
|
||||
|
||||
### 08_random_avatar_generator.go
|
||||
|
||||
This example is an interactive random avatar generator:
|
||||
|
||||
- Randomly generating avatars with different styles and themes
|
||||
|
||||
## Tips
|
||||
|
||||
- Each example file has detailed comments at the top explaining the functionality it demonstrates
|
||||
- Check the error handling sections in the files if you encounter any issues
|
||||
- Generated avatars will be saved in the locations specified in the example code
|
||||
- Some examples may require creating directories to save generated files
|
Reference in New Issue
Block a user