今天是父亲节,祝福天下父亲们节日快乐。 这篇文章同时也为了孩子的童话故事,故事中爸爸是超人,在这童话的世界扮演英雄。孩子们纯真的眼神是每个爸爸奋斗的动力。 下面我用R做了2个礼物分享给大伙伴(奶爸)们。
献给我,也献给你们。当今适合,卷娃不如卷自己!鸡娃不如鸡自己!
下图动画可以自行更改文字。
setwd("D:/聂个人文件/我的公众号/SCI绘图/父亲节动画")
#---------1五角星超人-------------------
library(ggplot2)
library(gganimate)
library(magick)
library(dplyr)
# 1. 读取图片
img_path <- "D:/聂个人文件/我的公众号/SCI绘图/父亲节动画/爸爸超人2.png"
##素材来自https://588ku.com/gif/babachaoren.html,搜索爸爸超人
img <- image_read(img_path)
img_info <- image_info(img)
img_width <- img_info$width
img_height <- img_info$height
# 2. 生成星星动画数据
set.seed(123)
stars <- tibble(
x = runif(20, 0, img_width),
y = runif(20, img_height * 0.75, img_height * 0.95)
)
stars_anim <- expand.grid(frame = 1:10, id = 1:nrow(stars)) %>%
left_join(stars %>% mutate(id = row_number()), by = "id") %>%
mutate(size = ifelse(frame %% sample(2:3, n(), replace = TRUE),
runif(n(), 6, 12), runif(n(), 1, 3)))
# 3. 创建图层(星星 + 中文祝福文字)
p <- ggplot(stars_anim, aes(x = x, y = y, size = size)) +
# 五角星闪烁
geom_point(shape = 8, color = "gold") +
scale_size_identity() +
# 中文祝福文字(居中)
annotate("text",
x = img_width / 2,
y = img_height * 0.42,
label = "祝爸爸超人们节日快乐!",
size = 14,
family = "YaHei", # macOS 推荐字体
color = "gold",
fontface = "bold") +
coord_fixed(xlim = c(0, img_width), ylim = c(0, img_height)) +
theme_void() +
transition_manual(frame)
# 4. 渲染帧图
frames <- animate(p,
nframes = 10,
width = img_width,
height = img_height,
bg = "transparent",
renderer = magick_renderer())
# 5. 合成原图 + 每帧动画
base_list <- replicate(length(frames), img, simplify = FALSE)
overlay_list <- as.list(frames)
final_frames <- Map(function(base, overlay) {
image_composite(base, overlay, operator = "over")
}, base = base_list, overlay = overlay_list)
# 6. 输出为 GIF
final_gif <- image_animate(image_join(final_frames), fps = 10)
image_write(final_gif, path = "爸爸超人_闪耀星星_带文字.gif")
#--------2. 父亲节文字动画-----------
# 定义彩虹颜色
colors <- rainbow(length(unique(dat$word)))
library(ggplot2)
library(stringr)
library(gganimate)
library(ggsci)
library(RColorBrewer)
rm(list = ls())
# 设置展示文字
text_str <- "HappyFathersDay"
text_vec <- str_split(text_str, "", simplify = TRUE)[1,]
# 构造固定文字的样式(居中显示)
dat_a <- data.frame(
word = text_vec,
x = seq(0, 5, length.out = length(text_vec)),
y = rep(5, length(text_vec)),
type = rep(2, length(text_vec))
)
# 构造第二组固定文字(低一点)
dat_b <- dat_a
dat_b$y <- dat_b$y - 3
dat_b$type <- 4
# 构造第三组固定文字(高一点)
dat_c <- dat_a
dat_c$y <- dat_c$y + 4
dat_c$type <- 6
# 随机状态函数
random <- function(x){
data.frame(
word = text_vec,
x = sample(seq(0, 5, by = 0.2), length(text_vec)),
y = sample(seq(0, 9, by = 0.5), length(text_vec)),
type = rep(x, length(text_vec))
)
}
# 合并数据
dat <- rbind(
random(1),
dat_a,
random(3),
dat_b,
random(5),
dat_c
)
# 使用彩虹色
colors <- rainbow(length(unique(dat$word)))
# 绘图
animation_to_save <- ggplot(dat) +
geom_text(aes(x, y, label = word, color = word), size = 10, fontface = "italic") +
xlim(0, 5) + ylim(0, 10) +
scale_color_manual(values = colors) +
theme_bw() +
theme(
legend.position = "none",
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank()
) +
transition_states(type, transition_length = rep(5, 6), state_length = c(0, 2, 0, 2, 0, 2))
# 保存动画
anim_save("happy-fathers-day.gif", animation = animation_to_save)
别躺了,起来赚奶粉钱了。
懒得下图的用下面的原图。