首页 > 其他分享 >canvas

canvas

时间:2024-02-21 16:46:19浏览次数:14  
标签:canvas const random ctx stars Math

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        html,
        body {
            width: 100%;
            height: 100%;
        }
        div {
            width: 100%;
            height: 100%;
            background: #000;
        }
        .home_bg {
            position: fixed;
            left: 0;
            right: 0;
            top: 0;
            bottom: 0;
            z-index: 0;
        }
    </style>
    <body>
        <div>
            <canvas id="canvas-bg"></canvas>
        </div>
        <script>
            const drawBg = () => {
                const canvas = document.getElementById('canvas-bg')
                if (canvas === null) return
                const ctx = canvas?.getContext('2d')
                ctx.canvas.width = window.innerWidth
                ctx.canvas.height = window.innerHeight
                const cW = canvas.width
                const cH = canvas.height
                const stars = []
                // add star with random position
                function addStar() {
                    const x = Math.floor(Math.random() * cW) + -200
                    const y = Math.floor(Math.random() * cH) + 1
                    const s = Math.floor(Math.random() * 12) + 1
                    stars.push({ x: x, y: y, s: s })
                }

                function starField() {
                    addStar()
                    for (let i = 0; i < stars.length; i++) {
                        ctx.fillStyle = 'rgba(255, 255, 255, 1)'
                        ctx.fillRect(stars[i].x++, stars[i].y, stars[i].s * 0.15, stars[i].s * 0.15)
                        ctx.fill()
                        if (stars[i].x > cW) {
                            stars.splice(i, 1)
                        }
                    }
                }
                // animate
                function animate() {
                    ctx.save()
                    ctx.clearRect(0, 0, cW, cH)
                    starField()
                    ctx.restore()
                }
                const animateInterval = setInterval(animate, 50)
            }
            window.onload = () => {
                drawBg()
            }
        </script>
    </body>
</html>

 

标签:canvas,const,random,ctx,stars,Math
From: https://www.cnblogs.com/conlin/p/18025599

相关文章

  • npm i canvas 报错 [email protected] run install node-pre-gyp install --fallback-to-b
    今天在写项目的时候安装npmicanvas的时候一直报错。具体错误如下npmERR!commandfailednpmERR!commandC:\Windows\system32\cmd.exe/d/s/cnode-pre-gypinstall--fallback-to-build--update-binarynpmERR!node-pre-gypinfoitworkedifitendswithoknpmE......
  • 使用html2Canvas截图网页时,网页存在图片报错:Tainted canvases may not be exported
     这是因为图片跨域产生的报错,看有的说法可以把所有图片进行允许跨域配置,但是比较麻烦。html2canvas有参数可以配置是否允许跨域 参数名称类型默认值描述allowTaintbooleanfalseWhethertoallowcross-originimagestotaintthecanvas---允许跨域background......
  • uniapp编译成微信小程序报错-Component is not found in path "components/canvaspage
     问题:我需要将components/canvaspagebg/index引入进pages/index/index   报错了pages/index/index页面引入: uni-app程序编译成微信小程序后,组件无法显示,控制台报错,错误信息为: 我查看了路径,是对的看网上的解决办法就是 我取消勾选后刷新页面就可以了,此时我在选中......
  • P9697 Canvas 题解
    首先观察到有一个关键性质是\(1\leqx_i,y_i\leq2\)。那么我们贪心的考虑我们肯定会将\((x,y)=(1,1)\)的在一开始操作,\((x,y)=(2,2)\)的最后操作。也就是说现在没有固定的是\((x,y)=(1,2)\)和\((x,y)=(2,1)\)的。我们不妨令\((x,y)=(1,2)\),然后连边\((l,r)\)。然......
  • Canvas实现音频可视化波形图
    @目录1.效果演示2.基本概念说明2.1音频源节点2.2时域图2.3频谱图3.案例代码1.效果演示上图演示了音频的波形随着音乐的节奏而跌宕起伏2.基本概念说明2.1音频源节点音频数据的来源,是音频处理的其中一个环节2.2时域图声音信号是一维的时域信号,无法观察出频......
  • 布局算力新基建,九章云极DataCanvas公司赋能AI产业高质量发展
    近日,WIM2023(WorldInnovatorsMeet,世界创新者年会)在北京盛大举行,九章云极DataCanvas联合创始人尚明栋受邀出席论坛,并作“AI基础软件加速产业智能化升级”主题演讲。WIM2023由亿欧、中关村国家自主创新示范区展示中心、中关村会展与服务产业联盟联合主办,以“穿越波动韧性前行”为......
  • h5 canvas 视频透明度抠图,视频需要一个灰度通道副本表示透明度
    视频透明度抠图,视频需要一个灰度通道副本表示透明度目前抖音等直播平台的礼物特效就是这个方法处理的 <!DOCTYPEhtml><html><head><metacharset="UTF-8"><title>视频灰度通道抠图,视频包含灰度值表示透明的副本</title><style>html,body{ ::-......
  • vue3+js使用canvas实现签字、重签
    <el-dialogtitle="签字板"v-model="visible"width="1000px"append-to-body><canvas@mousemove="canvasMove"@mouseup="canvasUp"ref="canvas"width="1000"height="5......
  • P9697 [GDCPC2023] Canvas(强联通分量)
    题意简述有一个长度为\(n\)的数组\(a\)和\(m\)次操作,\(a_i\)初始为\(0\)。每次操作形如\(l_i,x_i,r_i,y_i\)表示执行\(a_{l_i}\leftarrowx_i,a_{r_i}\leftarrowy_i\),你可以改变\(m\)次操作的执行顺序,求最终\(\sum_{i=1}^na_i\)的最大值,并给出执行操作的顺序。......
  • canvas绘制图形总结
    ctx.beginPath()//新建一条路径,生成之后,图形绘制命令被指向到路径上生成路径。ctx.closePath()//闭合路径之后图形绘制命令又重新指向到上下文中。ctx.stroke()//通过线条来绘制图形轮廓。ctx.fill()//通过填充路径的内容区域生成实心的图形。 绘制矩形 ctx.save() ctx.......