代码雨-学习

 HTML

 





  
  
  
  Document
  



  

  


TS

// 获取实例
let canvas: HTMLCanvasElement = document.querySelector('canvas')
console.log(canvas, '8989');
// 创建画布--2D的
let ctx = canvas.getContext('2d')
// 设置宽度为可是区域宽度 ==??直接设置100vw可以么-待测试
canvas.width = screen.availWidth
// 设置高度为可是区域高度
canvas.height = screen.availHeight

// 要使用的(雨点)
let str1: string[] = '唐宋元明清'.split('')

// 要维护的数组-----fill(0)填充数组内容
let Arr = Array(Math.ceil(canvas.width / 16)).fill(0)
console.log(Arr);

const rain = () => {
  // 设置画布背景色
  ctx.fillStyle = 'rgba(0,0,0,0.05)'
  // 对应x y 轴,铺满
  ctx.fillRect(0, 0, canvas.width, canvas.height)
  // 文字颜色
  ctx.fillStyle = '#0f0'

  Arr.forEach((item, index) => {
    ctx.fillText(str1[Math.floor(Math.random() * str1.length)], index * 16, item + 16)
    // item都是0 所以让后续的item每次加上16,但是如果超过高度-直接为0----但是下的太整齐-所以加上随机归零
    // 随机超过视口就会被结束
    Arr[index] = item > canvas.height || item > 10000 * Math.random() ? 0 : item + 16
  })
}
// 定时填充
setInterval(rain, 100)

你可能感兴趣的:(html,前端,javascript)