• canvas-8searchLight4.html


    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
        <canvas id="canvas" style="margin:0 auto;border:1px #ddd solid">
            The current browser does not support Canvas, can replace the browser a try!
        </canvas>
    
        <script>
    
            var searchLight = {
                x : 400,
                y : 400,
                radius : 150,
                vx : Math.random() * 5 + 10,
                vy : Math.random() * 5 + 10
            }
    
            var isIncrease = true;
    
            window.onload = function(){
                var canvas = document.getElementById('canvas');
    
                canvas.width = 1024;
                canvas.height = 768;
    
                if(canvas.getContext('2d')){
                    var context = canvas.getContext('2d');
    
                    setInterval(function(){    
                            draw(context);
                            update(canvas.width,canvas.height);
                    },40);
    
                }else{
                    alert('当前游览器不支持Canvas,请更换游览器后再试!');
                }
            }
    
            function draw(cxt){
                var canvas = cxt.canvas;
                cxt.clearRect(0,0,canvas.width,canvas.height);
    
                cxt.save();
    
                    cxt.beginPath();
                        cxt.fillStyle = "#000";
                        cxt.fillRect(0,0,canvas.width,canvas.height)
                    cxt.closePath();
    
                    cxt.save();
                        cxt.translate(searchLight.x,searchLight.y);
                        cxt.scale(searchLight.radius,searchLight.radius);
                        starPath(cxt);
                        cxt.fillStyle = "#fff";
                        cxt.fill();
                    cxt.restore();
                        cxt.clip();
    
                    cxt.font = "bold 150px Arial";
                    cxt.textAlign = "center";
                    cxt.textBaseline = "middle";
                    cxt.fillStyle = "#058";
                    cxt.fillText("Canvas",canvas.width/2,canvas.height/4);
                    cxt.fillText("Canvas",canvas.width/2,canvas.height/2);
                    cxt.fillText("Canvas",canvas.width/2,canvas.height * 3/4);
                cxt.restore();
            }
    
    
            function starPath(cxt){
            
                cxt.beginPath();
                for(var i=0;i<5;i++){
                    cxt.lineTo(Math.cos( (18+i*72) / 180 * Math.PI ),
                                    -Math.sin( (18+i*72) / 180 * Math.PI ));
                    cxt.lineTo(Math.cos( (54+i*72) / 180 * Math.PI )*0.5,
                                    -Math.sin( (54+i*72) / 180 * Math.PI )*0.5);
                }
                cxt.closePath();
            }
    
    
            function update(canvasWidth,canvasHeight){
                // rot += 1;
    
                // searchLight.x += searchLight.vx;
                // searchLight.y += searchLight.vy;
    
                // if(searchLight.x - searchLight.radius <= 0){
                //     searchLight.vx = -searchLight.vx;
                //     searchLight.x = searchLight.radius;
                // }
    
                // if(searchLight.x + searchLight.radius >= canvasWidth){
                //     searchLight.vx = -searchLight.vx;
                //     searchLight.x = canvasWidth - searchLight.radius;
                // }
    
                // if(searchLight.y - searchLight.radius <= 0){
                //     searchLight.vy = -searchLight.vy;
                //     searchLight.y = searchLight.radius;
                // }
    
                // if(searchLight.y + searchLight.radius >= canvasHeight){
                //     searchLight.vy = -searchLight.vy;
                //     searchLight.y = canvasHeight - searchLight.radius;
                // }
    
                if(searchLight.radius > 700)
                    isIncrease = false;
                else if(searchLight.radius < 150)
                    isIncrease = true;
    
                if(isIncrease)
                    searchLight.radius += 5;
                else 
                    searchLight.radius -= 5;
            }
    
        </script>
    </body>
    </html>
  • 相关阅读:
    五个字符就能让你电脑死机
    易语言e.exe在一些系统运行出错解决方法
    检测是否联网
    JS判断设备的类型
    JavaScript判断移动端及pc端访问不同的网站
    代码片段
    WEB前端知识在乱花渐欲迷人眼的当下,如何分清主次和学习优先级呢?
    说说JSON和JSONP,也许你会豁然开朗
    HTML5 LocalStorage 本地存储
    namenode 和datanode无法启动,错误:FSNamesystem initialization failed. datanode.DataNode: Incompatible namespaceIDs
  • 原文地址:https://www.cnblogs.com/cynthia-wuqian/p/4988537.html
Copyright © 2020-2023  润新知