zoukankan      html  css  js  c++  java
  • canvas-star3

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>添加天空渐变</title>
    </head>
    <body>
        <canvas id="canvas" style="margin:0 auto;">
            The current browser does not support Canvas, can replace the browser a try!
        </canvas>
    
        <script>
    
            window.onload = function(){
                var canvas = document.getElementById('canvas');
    
                canvas.width = 1200;
                canvas.height = 800;
    
                if(canvas.getContext('2d')){
                    var context = canvas.getContext('2d');
    
                    // context.fillStyle = "#000";
                    var skyStyle = context.createLinearGradient(0,0,0,canvas.height);
                    skyStyle.addColorStop(0.0,'black');
                    skyStyle.addColorStop(1.0,'#035');
                    context.fillStyle = skyStyle;
                    
                    context.fillRect(0,0,canvas.width,canvas.height)
    
                    for(var i=0;i<200;i++){
                        var r = Math.random() * 5 + 5;
                        var x = Math.random() * canvas.width;
                        var y = Math.random() * canvas.height * 0.65;
                        var a = Math.random() * 360;
    
                        drawStar(context , x , y , r , a )
                    }
    
                }else{
                    alert('当前游览器不支持Canvas,请更换游览器后再试!');
                }
            }
    
            function drawStar(cxt,x,y,R,rot){
                cxt.save();
    
                    cxt.translate(x,y);
                    cxt.rotate(rot/180*Math.PI);
                    cxt.scale(0.05*R,0.05*R)
    
                    starPath(cxt);
    
                    cxt.fillStyle = "#fb3";
                    cxt.strokeStyle = "#fd5";
                    cxt.lineWidth = 3;
                    cxt.lineJoin = "round";
    
                    cxt.fill();
                    cxt.stroke();
    
                cxt.restore()
            }
    
        
            function starPath(cxt){
            
                cxt.beginPath();
                for(var i=0;i<5;i++){
                    cxt.lineTo(Math.cos( (18+i*72) / 180 * Math.PI )*20,
                                    -Math.sin( (18+i*72) / 180 * Math.PI )*20);
                    cxt.lineTo(Math.cos( (54+i*72) / 180 * Math.PI )*0.5*20,
                                    -Math.sin( (54+i*72) / 180 * Math.PI )*0.5*20);
                }
                cxt.closePath();
            }
        </script>
    </body>
    <script>
        /*图形变换
    
            位移 translate(x,y)
    
            旋转 rotate(deg)
    
            缩放 scale(sx,sy)
    
        */
    </script>
    </html>
  • 相关阅读:
    前端接口设计
    前端协作流程
    编写jQuery插件
    jQuery插件之validation插件
    深入理解ajax系列第九篇——jQuery中的ajax
    前端学PHP之Smarty模板引擎
    第3选择-解决所有难题的关键思维,种下好的种子避免落入钻石交易
    阿里BCG重磅报告《人工智能,未来致胜之道》
    关于web开发前端h5框架的选择
    html5+php实现文件的断点续传ajax异步上传
  • 原文地址:https://www.cnblogs.com/cynthia-wuqian/p/4981608.html
Copyright © 2011-2022 走看看