流星雨动画 svg

春天的晚上感觉还挺凉爽,不过已经不那么冷了,晚上在窗前又能看看星星了。
请添加图片描述
因此搞了这,看起来还是挺放松的。放到网站上看起来还挺好的。
逻辑还是比较简单的,就是控制svg的线段运动,在视窗内滑动显示。
代码也比较简单,如下:
采用vue+tailwindcss

<script setup lang="ts">
import { Ref, onMounted, ref } from "vue";

interface Lines {
  x1: number
  y1: number
  x2: number
  y2: number
  tx1: number
  ty1: number
  tx2: number
  ty2: number
  dur: number
}

const position: Ref<Lines[]> = ref([])

onMounted(() => {
  let ps:Lines[] = []
  let w = 300
  let h = 150
  for(let i = 0; i < 20; i++) {
    let length = Math.random() * 50 + 4
    let mlen = Math.max(w,h)
    let x = Math.random() * mlen - mlen
    let y = Math.random() * mlen - mlen
    let distance = Math.max(w-x, h-y)
    let ex = x + distance
    let ey = y + distance
    ps.push({
      x1: x,
      y1: y,
      x2: x + length,
      y2: y + length,
      tx1: ex,
      ty1: ey,
      tx2: ex + length,
      ty2: ey + length,
      dur: distance / mlen * (4 + Math.random())
    })
  }
  position.value = ps
})
</script>

<template>
  <svg class="w-[300px] h-[150px] bg-slate-800" version="1.1"
    xmlns="http://www.w3.org/2000/svg">

    <line v-for="(p, i) in position" 
      :x1="p.x1" :y1="p.y1" :x2="p.x2" :y2="p.y2"
      style="stroke:rgb(99,99,99); stroke-width:1">
      <animate attributeName="x1" attributeType="XML" 
              :from="p.x1"  :to="p.tx1" 
              begin="0s" :dur="p.dur" 
              repeatCount="indefinite"/>
      <animate attributeName="y1" attributeType="XML" 
              :from="p.y1"  :to="p.ty1" 
              begin="0s" :dur="p.dur" 
              repeatCount="indefinite"/>
      <animate attributeName="x2" attributeType="XML" 
              :from="p.x2"  :to="p.tx2"  
              begin="0s" :dur="p.dur" 
              repeatCount="indefinite"/>
      <animate attributeName="y2" attributeType="XML" 
              :from="p.y2"  :to="p.ty2"
              begin="0s" :dur="p.dur" 
              repeatCount="indefinite"/>
    </line>
  </svg>
</template>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

xyccstudio

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

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

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

打赏作者

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

抵扣说明:

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

余额充值