zoukankan      html  css  js  c++  java
  • 利用canvas实现雪花的飘动

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Canvas</title>
    <style type="text/css">
    *{
    margin: 0;
    padding: 0;
    }
    html, body{
    height: 100%;
    }
    #myCanvas{
    background-color: #87CEEB;
    }
    </style>
    </head>
    <body>
    <canvas id="myCanvas" width="1000" height="600">您的浏览器不支持Canvas元素</canvas>
    <script type="text/javascript">
    var canvas = document.getElementById("myCanvas");
    var context = canvas.getContext("2d");
    var deg = Math.PI/180;
    var practicles = [];
    for (var i = 0; i < 500; i++) {//循环生成500粒
    practicles.push({
    x: Math.random()*(window.innerWidth),
    y: Math.random()*(window.innerHeight),
    vx: Math.random()-0.5,
    vy: Math.random()+0.5,
    size: Math.random()*25+1,
    color: "#FFF"
    })
    }
    function timeUpdate(){
    context.clearRect(0,0,window.innerWidth,window.innerHeight);//清除画布区域
    var practicle;
    for (var i = 0; i < 500; i++) {
    var practicle = practicles[i];
    practicle.x += practicles[i].vx;
    practicle.y += practicles[i].vy;
    if (practicle.x<0) {practicle.x=window.innerWidth}
    if (practicle.x>window.innerWidth) {practicle.x=0}
    if (practicle.y>window.innerHeight) {practicle.y=0}
    context.beginPath();
    //context.arc(practicle.x,practicle.y,practicle.size,0,Math.PI*2);
    snowflake(context,3,practicle.x,practicle.y,practicle.size);
    }
    }

    //实现科赫雪花
    function snowflake(c,n,x,y,len) {
    c.save();
    c.translate(x,y);
    c.moveTo(0,0);
    leg(n);
    c.rotate(-120*deg);
    leg(n);
    c.rotate(-120*deg);
    leg(n);
    c.closePath();
    function leg(n) {
    c.save();
    if(n==0){
    c.lineTo(len,0);
    }else{
    c.scale(1/3,1/3);
    leg(n-1);
    c.rotate(60*deg);
    leg(n-1);
    c.rotate(-120*deg);
    leg(n-1);
    c.rotate(60*deg);
    leg(n-1);
    }
    c.restore();
    c.translate(len,0)
    }
    c.stroke();
    c.fillStyle = "#fff";
    c.fill();
    c.restore();
    }
    setInterval(timeUpdate,40);
    </script>
    </body>
    </html>
    最后效果:




  • 相关阅读:
    Hystrix源码分析(一)
    docker虚拟ip和项目地ip冲突问题解决
    Hystrix入门demo
    为什么spirng扫描到@compent会把对象注册到容器上
    hive安装与基本操作
    hadoop基本安装
    spring session 存到数据库的设置
    React加Ant Design实现的一个登陆界面及小案例
    一个获取天气预报数据的小案例
    React学习笔记案例2
  • 原文地址:https://www.cnblogs.com/huqinqin/p/7511121.html
Copyright © 2011-2022 走看看