【Canvas与色彩】太空中看太阳

【成图】

【代码】

复制代码
<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
     <title>太空中看太阳</title>
     <style type="text/css">
     .centerlize{
        margin:0 auto;
        width:1200px;
     }
     </style>
     </head>

     <body onload="init();">
        <div class="centerlize">
            <canvas id="myCanvas" width="12px" height="12px" style="border:1px dotted black;">
                如果看到这段文字说您的浏览器尚不支持HTML5 Canvas,请更换浏览器再试.
            </canvas>
            <img id="myImg" src="342.jpg" style="display:none;"/>
        </div>
     </body>
</html>
<script type="text/javascript">
<!--
/*****************************************************************
* 将全体代码(从<!DOCTYPE到script>)拷贝下来,粘贴到文本编辑器中,
* 另存为.html文件,再用chrome浏览器打开,就能看到实现效果。
******************************************************************/

// canvas的绘图环境
var ctx;

// 高宽
const WIDTH=512;
const HEIGHT=512;

// 舞台对象
var stage;

//-------------------------------
// 初始化
//-------------------------------
function init(){
    // 获得canvas对象
    var canvas=document.getElementById('myCanvas');  
    canvas.width=WIDTH;
    canvas.height=HEIGHT;

    // 初始化canvas的绘图环境
    ctx=canvas.getContext('2d');  
    ctx.translate(WIDTH/2,HEIGHT/2);// 原点平移

    // 准备
    stage=new Stage();    
    stage.init();

    // 开幕
    animate();
}

// 播放动画
function animate(){    
    stage.update();    
    stage.paintBg(ctx);
    stage.paintFg(ctx);     

    // 循环
    if(true){
        //sleep(100);
        window.requestAnimationFrame(animate);   
    }
}

// 舞台类
function Stage(){
    // 初始化
    this.init=function(){
        
    }

    // 更新
    this.update=function(){    
        
    }

    // 画背景
    this.paintBg=function(ctx){
        ctx.clearRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);// 清屏    
    }

    // 画前景
    this.paintFg=function(ctx){
        // 底色
        ctx.fillStyle = "black";
        ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);        
        
        // 径向渐变色
        const R=120;
        var gnt1=ctx.createRadialGradient(0,0,0,0,0,R);
        gnt1.addColorStop(0.5,"rgb(251,183,0)");
        gnt1.addColorStop(0.6,"rgb(255,222,29)");
        gnt1.addColorStop(0.7,"rgb(255,222,29)");
        gnt1.addColorStop(0.8,"rgb(255,222,29)");
        gnt1.addColorStop(0.9,"rgb(232,115,2)");


        ctx.fillStyle=gnt1;
        ctx.beginPath();
        ctx.arc(0,0,R,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fill();
                
        
        var r=R*0.6;
        var gnt2=ctx.createRadialGradient(0,0,R/2,0,0,R*1.2);
        gnt2.addColorStop(0,"rgb(255,222,29)");
        gnt2.addColorStop(0.7,"rgb(255,222,219)");
        gnt2.addColorStop(1,"rgb(255,255,170)");
        // 刺透太阳光芒
        const N=24;
        for(var i=0;i<N;i++){
            var theta=Math.PI*2/N*i;            
            var pt=createPt(r*Math.cos(theta),r*Math.sin(theta));

            var angle=theta-Math.PI/2;
            var rad=1.5;
            var up=createPt(pt.x+rad*Math.cos(angle),pt.y+rad*Math.sin(angle));

            angle=theta+Math.PI/2;
            var down=createPt(pt.x+rad*Math.cos(angle),pt.y+rad*Math.sin(angle));

            var top=createPt(R*1.2*Math.cos(theta),R*1.2*Math.sin(theta));

            ctx.fillStyle=gnt2;
            ctx.beginPath();
            ctx.lineTo(up.x,up.y);
            ctx.lineTo(down.x,down.y);
            ctx.lineTo(top.x,top.y);
            ctx.closePath();
            ctx.fill();
        }

        r=R*0.8;
        var gnt3=ctx.createRadialGradient(0,0,R/2,0,0,R*1.2);
        gnt3.addColorStop(0,"rgb(255,222,29)");
        gnt3.addColorStop(0.7,"rgb(255,222,29)");
        gnt3.addColorStop(1,"rgb(255,255,170)");
        // 未刺透太阳光芒
        for(var i=0;i<N;i++){
            var theta=Math.PI*2/N*i+Math.PI*1/N;            
            var pt=createPt(r*Math.cos(theta),r*Math.sin(theta));

            var angle=theta-Math.PI/2;
            var rad=2;
            var up=createPt(pt.x+rad*Math.cos(angle),pt.y+rad*Math.sin(angle));

            angle=theta+Math.PI/2;
            var down=createPt(pt.x+rad*Math.cos(angle),pt.y+rad*Math.sin(angle));

            var top=createPt(1.1*R*Math.cos(theta),1.1*R*Math.sin(theta));

            ctx.fillStyle=gnt3;
            ctx.beginPath();
            ctx.lineTo(up.x,up.y);
            ctx.lineTo(down.x,down.y);
            ctx.lineTo(top.x,top.y);
            ctx.closePath();
            ctx.fill();
        }

        r=R*1.3;
        var gnt4=ctx.createRadialGradient(0,0,R*1.2,0,0,R*1.8);
        gnt4.addColorStop(0.1,"black");
        gnt4.addColorStop(0.30,"red");
        gnt4.addColorStop(0.35,"orange");
        gnt4.addColorStop(0.40,"yellow");
        gnt4.addColorStop(0.45,"green");
        gnt4.addColorStop(0.50,"blue");
        gnt4.addColorStop(0.55,"navy");
        gnt4.addColorStop(0.60,"indigo");
        gnt4.addColorStop(0.65,"purple");
        gnt4.addColorStop(0.7,"white");
        gnt4.addColorStop(0.9,"black");

        ctx.lineWidth=2;
        ctx.strokeStyle=gnt4;

        // 未刺透太阳光芒
        for(var i=0;i<N;i++){
            var theta=Math.PI*2/N*i+Math.PI*1/N;            
            var start=createPt(1.2*R*Math.cos(theta),1.2*R*Math.sin(theta));
            var end=createPt(1.8*R*Math.cos(theta),1.8*R*Math.sin(theta));

            
            ctx.beginPath();
            ctx.lineTo(start.x,start.y);
            ctx.lineTo(end.x,end.y);
            ctx.stroke();
        }


        writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火原创","8px consolas","lightgrey");// 版权
    }
}

/*----------------------------------------------------------
函数:创建一个二维坐标点
x:横坐标
y:纵坐标
Pt即Point
----------------------------------------------------------*/
function createPt(x,y){
    var retval={};
    retval.x=x;
    retval.y=y;
    return retval;
}

/*----------------------------------------------------------
函数:延时若干毫秒
milliseconds:毫秒数
----------------------------------------------------------*/
function sleep(milliSeconds) {
    const date = Date.now();
    let currDate = null;
    while (currDate - date < milliSeconds) {
        currDate = Date.now();
    } 
}

/*----------------------------------------------------------
函数:书写文字
ctx:绘图上下文
x:横坐标
y:纵坐标
text:文字
font:字体
color:颜色
----------------------------------------------------------*/
function writeText(ctx,x,y,text,font,color){
    ctx.save();
    ctx.textBaseline="bottom";
    ctx.textAlign="center";
    ctx.font = font;
    ctx.fillStyle=color;
    ctx.fillText(text,x,y);
    ctx.restore();
}

/*-------------------------------------------------------------
我原志在吞天灭地,奈何忍辱藏于污泥。
--------------------------------------------------------------*/
//-->
</script>
复制代码

END

posted @   逆火狂飙  阅读(691)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2014-04-27 实现淡入淡出效果的组件,继承自JComponent
2014-04-27 以JPanel为基础实现一个图像框
2014-04-27 扩展JButton实现自己的图片按钮
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东
点击右上角即可分享
微信分享提示