用R语言放个烟花|理工男的浪漫

2024的最后一天了~

上半年还好,下半年太忙了,近年末忙飞了。

立个flag!

2025力求抽空勤快更新!

文献解读和代码分享!

还有一些好玩的东西~

最后,

祝大家除夕快乐🎉

提前祝大家新年快乐🎉

2025,更上一层楼!

这里用R写了一个烟花html

## Author: ChakMan Tsui (Modified)
## URL1: http://cos.name/cn/topic/103272/
## URL2:https://github.com/yihui/animation/blob/main/demo/fireworks.R

library(animation)
library(showtext)   
library(sysfonts)   

## 添加中文字体
font_add("SimHei", "simhei.ttf")  # 确保系统安装了该字体,我这里是macOS
showtext_auto()

saveHTML({
  fire <- function(
    centre = c(0, 0), r = 1:2, theta = seq(0, 2 * pi, length = 80),
    l.col = rgb(1, 1, 0), lwd = 2, ...
  ) {
    x <- centre[1] + outer(r, theta, function(r, theta) r * sin(theta))
    y <- centre[2] + outer(r, theta, function(r, theta) r * cos(theta))
    matplot(x, y, type = 'l', lty = 1, col = l.col, add = T, lwd = lwd, ...)
  }
  
  f <- function(
    centre, n, N = 15, l.col, p.col = '#C31D1D', lwd = 3, ...
  ) {
    ani.options(interval = 0.1)  # 调整动画间隔
    matplot(centre[, 1], centre[, 2], col = p.col, ...)
    
    l = lapply(n, function(n_i) seq(0.05, 0.3, length = n_i))
    
    if (length(l.col) == 1) l.col = rep(l.col, length(n))
    
    for (r in 1:N) {
      for (j in 1:length(n)) {
        if (r%%(n[j] + 1) == 0) {
          for (r1 in 1:n[j]) {
            fire(
              centre = centre[j, ], 
              r = seq(r1 - l[[j]][r1], r1 + l[[j]][r1], length = 8),
              theta = seq(0, 2 * pi, length = 8 * r1) + 1, 
              l.col = par('bg'),
              lwd = lwd + 1
            )
          }
        } else {
          fire(
            centre = centre[j, ], 
            r = seq(r%%(n[j] + 1) - l[[j]][r%%(n[j] + 1)], 
                    r%%(n[j] + 1) + l[[j]][r%%(n[j] + 1)], length = 8),
            theta = seq(0, 2 * pi, length = 8 * r%%(n[j] + 1)) + 1, 
            l.col = rainbow(n[j])[r%%(n[j] + 1)],
            lwd = lwd, ...
          )
        }
        ani.pause()
      }
    }
  }
  
  card <- function(N = 60, p.col = '#5AB883', bgcolour = 'black', lwd = 3, ...) {
    ani.options(interval = 0.1)  # 每帧的间隔时间
    for (i in 1:N) {
      par(ann = F, bg = bgcolour, mar = rep(0, 4), pty = 's')
      f(N = i, lwd = lwd, ...)
      ## 祝福语,设置颜色闪烁
      text(0, 0, '祥蛇贺新 吉光焕彩', col = rainbow(N)[i %% length(rainbow(N)) + 1], cex = 4, family = "bold")
      ani.pause()
    }
  }
  
  ani.options(interval = 0.1)  # 更细致的帧间隔
  
  ## 设置 23 朵烟花的中心坐标
  centre_positions <- matrix(runif(46, -10, 10), ncol = 2)
  
  ## 设置每朵烟花的叶片数
  n_leaves <- sample(4:6, 23, replace = TRUE)
  
  ## 烟花动画
  card(N = 60, centre = centre_positions, n = n_leaves, pch = 8,
       p.col = '#5AB883', l.col = rep('rainbow', 23),
       xlim = c(-10, 10), ylim = c(-10, 10))
  
}, img.name = 'fireworks_chinese_23', htmlfile = 'fireworks_chinese_23.html', ani.height = 800, ani.width = 1000,
title = 'fireworks_demo', description = '烟花动画')


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bioinfo Guy

你的鼓励将是我创作的最大动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值