当前位置: 首页 > article >正文

【Canvas与艺术】六鱼六燕铁艺壁画

【成图】

【代码】

<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
     <title>六鱼六燕铁艺壁画Draft3</title>
     <style type="text/css">
     .centerlize{
        margin:0 auto;
        width:1200px;
     }
     </style>
     </head>

     <body οnlοad="init();">
        <div class="centerlize">
            <canvas id="myCanvas" width="12px" height="12px" style="border:1px dotted black;">
                如果看到这段文字说您的浏览器尚不支持HTML5 Canvas,请更换浏览器再试.
            </canvas>
        </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.save();
        ctx.fillStyle = "white";
        ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);
        ctx.restore();

        const R=210;// 基准尺寸

        // 第一圈
        ctx.save();
        var r=R*1.00;
        ctx.shadowOffsetX=r/30;
        ctx.shadowOffsetY=r/30;
        ctx.shadowColor="grey";
        ctx.shadowBlur=6;    

        var gdnt=ctx.createLinearGradient(-r,-r,r,r);
        gdnt.addColorStop(0,"rgb(255,216,208)");
        gdnt.addColorStop(1,"grey");
        ctx.fillStyle=gdnt;
        drawRoundRect(ctx,0,0,2*r,2*r,R/21);
        ctx.fill();
        ctx.restore();

        // 第二圈木框
        ctx.save();
        var r=R*0.99;
        var gdnt=ctx.createLinearGradient(-r,-r,r,r);// 整体渐变色
        gdnt.addColorStop(0,"rgb(254,208,170)");
        gdnt.addColorStop(0.5,"rgb(255,216,208)");
        gdnt.addColorStop(1,"rgb(229,169,81)");
        ctx.fillStyle=gdnt;
        drawRoundRect(ctx,0,0,2*r,2*r,R/21);
        ctx.fill();
        ctx.restore();

        // 第三
        ctx.save();
        var r=R*0.97;
        var gdnt=ctx.createLinearGradient(-r,-r,r,r);
        gdnt.addColorStop(0,"rgb(229,169,81)");
        gdnt.addColorStop(1,"rgb(254,208,170)");
        ctx.fillStyle=gdnt;
        drawRoundRect(ctx,0,0,2*r,2*r,R/21);
        ctx.fill();
        ctx.restore();

        // 白圈
        ctx.save();
        var r=R*0.96;
        var gdnt=ctx.createLinearGradient(-r,-r,r,r);// 整体渐变色
        ctx.fillStyle="lightgrey";
        drawRoundRect(ctx,0,0,2*r,2*r,R/21);
        ctx.fill();
        ctx.restore();

        // 深绿底
        ctx.save();
        var r=R*0.90;
        var gdnt=ctx.createLinearGradient(-r,-r,r,r);// 整体渐变色
        ctx.fillStyle="rgb(16,36,41)";
        drawRoundRect(ctx,0,0,2*r,2*r,R/21);
        ctx.fill();
        ctx.restore();

        // 画铁艺图案
        ctx.save();
        var r=R*0.36;
        drawSixFish(ctx,r);
        ctx.restore();

        // 玻璃光
        var r=R*0.80;
        draw9Glasses(ctx,r);

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

/*----------------------------------------------------------
函数:用于绘制铁艺图案
ctx:绘图上下文
radius:图案基准半径
----------------------------------------------------------*/
function drawSixFish(ctx,radius){
    ctx.save();

    const R=radius;// 六边形短边长
    const D=R*1.2;// 六边形长边长
    var   N=12;// 边数

    ctx.lineWidth=1;
    var gnt=ctx.createLinearGradient(-R,-R,R,R);// 左上到右下径向渐变色
    gnt.addColorStop(0,  "rgb(254,253,189)");
    gnt.addColorStop(1,  "rgb(204,154,69)");    

    ctx.strokeStyle=gnt;
    ctx.fillStyle=gnt;
    
    var r=R;
    for(var i=0;i<N;i++){
        var theta=Math.PI*2/N*i;
        var a=createPt(0,0);

        r=R;
        var angle=theta-Math.PI/3;
        var b=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle));

        r=D;
        angle=theta;
        var c=createPt(b.x+r*Math.cos(angle),b.y+r*Math.sin(angle));

        r=R;
        angle=theta+Math.PI/3;
        var d=createPt(c.x+r*Math.cos(angle),c.y+r*Math.sin(angle));

        r=R;
        angle=theta+Math.PI/3*2;
        var e=createPt(d.x+r*Math.cos(angle),d.y+r*Math.sin(angle));

        r=D;
        angle=theta+Math.PI;
        var f=createPt(e.x+r*Math.cos(angle),e.y+r*Math.sin(angle));
        
        ctx.beginPath();
        ctx.moveTo(a.x,a.y);
        ctx.lineTo(b.x,b.y);
        ctx.lineTo(c.x,c.y);
        ctx.lineTo(d.x,d.y);
        ctx.lineTo(e.x,e.y);
        ctx.lineTo(f.x,f.y);
        ctx.closePath();
        ctx.stroke();
    }

    // 六叶
    N=6;
    r=R;
    for(var i=0;i<N;i++){
        var theta=Math.PI*2/N*i;

        var a=createPt(0,0);
        
        r=R;
        angle=theta;
        var b=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle));

        r=R/2*Math.sqrt(6);
        angle=theta+Math.PI/12;
        var c=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle));

        r=R;
        angle=theta+Math.PI/6;
        var d=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle));
        
        ctx.save();
        ctx.fillStyle=gnt;
        ctx.beginPath();
        ctx.moveTo(a.x,a.y);
        ctx.lineTo(b.x,b.y);
        ctx.lineTo(c.x,c.y);
        ctx.lineTo(d.x,d.y);
        ctx.closePath();
        ctx.fill();
        ctx.restore();
    }

    // 六燕
    N=6;
    r=R;
    for(var i=0;i<N;i++){
        var theta=Math.PI*2/N*i+Math.PI/6;

        r=R*Math.sqrt(3);
        var angle=theta;
        var a=createPt(r*Math.cos(angle),r*Math.sin(angle));

        r=D-R;
        angle=theta-Math.PI/6;
        var b=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle));

        r=(D+R)/2*Math.sqrt(3)*Math.tan(Math.PI/12)-(D-R)/2;
        angle=theta-Math.PI/2;
        var c=createPt(b.x+r*Math.cos(angle),b.y+r*Math.sin(angle));

        r=D+R;
        angle=theta;
        var d=createPt(r*Math.cos(angle),r*Math.sin(angle));

        r=D-R;
        angle=theta+Math.PI/6;
        var f=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle));

        r=(D+R)/2*Math.sqrt(3)*Math.tan(Math.PI/12)-(D-R)/2;
        angle=theta+Math.PI/2;
        var e=createPt(f.x+r*Math.cos(angle),f.y+r*Math.sin(angle));

        ctx.save();
        ctx.fillStyle=gnt;
        ctx.beginPath();
        ctx.moveTo(a.x,a.y);
        ctx.lineTo(b.x,b.y);
        ctx.lineTo(c.x,c.y);
        ctx.lineTo(d.x,d.y);
        ctx.lineTo(e.x,e.y);
        ctx.lineTo(f.x,f.y);
        ctx.closePath();
        ctx.fill();
        ctx.restore();
    }

    // 六点
    N=6;
    for(var i=0;i<N;i++){
        var theta=Math.PI*2/N*i+Math.PI/6;

        r=R*Math.sqrt(3)*0.8;
        var angle=theta;
        var pt=createPt(r*Math.cos(angle),r*Math.sin(angle));

        drawSolidCircle(ctx,pt.x,pt.y,5,gnt);
    }

    ctx.restore();
}

/*----------------------------------------------------------
函数:用于绘制圆角矩形
ctx:绘图上下文
x:矩形中心横坐标
y:矩形中心纵坐标
width:矩形宽
height:矩形高
radius:圆角半径
----------------------------------------------------------*/
function drawRoundRect(ctx,x,y,width,height,radius){
    ctx.beginPath();
    ctx.moveTo(x-width/2+radius,y-height/2);
    ctx.lineTo(x+width/2-radius,y-height/2);
    ctx.arcTo(x+width/2,y-height/2,x+width/2,y-height/2+radius,radius);
    ctx.lineTo(x+width/2,y-height/2+radius);
    ctx.lineTo(x+width/2,y+height/2-radius);
    ctx.arcTo(x+width/2,y+height/2,x+width/2-radius,y+height/2,radius);
    ctx.lineTo(x+width/2-radius,y+height/2);
    ctx.lineTo(x-width/2+radius,y+height/2);
    ctx.arcTo(x-width/2,y+height/2,x-width/2,y+height/2-radius,radius);
    ctx.lineTo(x-width/2,y+height/2-radius);
    ctx.lineTo(x-width/2,y-height/2+radius);
    ctx.arcTo(x-width/2,y-height/2,x-width/2+radius,y-height/2,radius);
    ctx.closePath();
}

/*----------------------------------------------------------
函数:用于绘制九块玻璃光
ctx:绘图上下文
radius:基准半径
----------------------------------------------------------*/
function draw9Glasses(ctx,radius){
    ctx.save();
    const D=-radius/2;
    const W=-radius/8;
    const G=-radius/16;
    const color="rgba(190,190,190,0.6)";

    // 上玻璃光
    var alpha=Math.PI/12;
    var beta=Math.PI/6;

    for(var i=0;i<3;i++){
        var s1=D+i*(W+G)
        var a=createPt(s1,s1*Math.tan(alpha));
        var b=createPt(s1,s1*Math.tan(alpha+beta));
        
        var s2=D+W+i*(W+G);
        var d=createPt(s2,s2*Math.tan(alpha));
        var c=createPt(s2,s2*Math.tan(alpha+beta));

        ctx.fillStyle=color;
        ctx.beginPath();
        ctx.moveTo(a.x,a.y);
        ctx.lineTo(b.x,b.y);        
        ctx.lineTo(c.x,c.y);
        ctx.lineTo(d.x,d.y);
        ctx.closePath();
        ctx.fill();
    }

    // 上玻璃光
    var alpha=-Math.PI/10;
    var beta=Math.PI/6;

    for(var i=0;i<3;i++){
        var s1=D+i*(W+G)
        var a=createPt(s1,s1*Math.tan(alpha));
        var b=createPt(s1,s1*Math.tan(alpha+beta));
        
        var s2=D+W+i*(W+G);
        var d=createPt(s2,s2*Math.tan(alpha));
        var c=createPt(s2,s2*Math.tan(alpha+beta));

        ctx.fillStyle=color;
        ctx.beginPath();
        ctx.moveTo(a.x,a.y);
        ctx.lineTo(b.x,b.y);        
        ctx.lineTo(c.x,c.y);
        ctx.lineTo(d.x,d.y);
        ctx.closePath();
        ctx.fill();
    }

    // 下玻璃光
    var alpha=-Math.PI/4.1;
    var beta=Math.PI/8;

    for(var i=0;i<3;i++){
        var s1=D+i*(W+G)
        var a=createPt(s1,s1*Math.tan(alpha));
        var b=createPt(s1,s1*Math.tan(alpha+beta));
        
        var s2=D+W+i*(W+G);
        var d=createPt(s2,s2*Math.tan(alpha));
        var c=createPt(s2,s2*Math.tan(alpha+beta));

        ctx.fillStyle=color;
        ctx.beginPath();
        ctx.moveTo(a.x,a.y);
        ctx.lineTo(b.x,b.y);        
        ctx.lineTo(c.x,c.y);
        ctx.lineTo(d.x,d.y);
        ctx.closePath();
        ctx.fill();
    }
    ctx.restore();
}

/*----------------------------------------------------------
函数:用于绘制实心圆,用途是标记点以辅助作图
ctx:绘图上下文
x:矩形中心横坐标
y:矩形中心纵坐标
r:圆半径
color:填充圆的颜色
----------------------------------------------------------*/
function drawSolidCircle(ctx,x,y,r,color){
    ctx.fillStyle=color;
    ctx.beginPath();
    ctx.arc(x,y,r,0,Math.PI*2,false);
    ctx.closePath();
    ctx.fill();
}

/*----------------------------------------------------------
函数:创建一个二维坐标点
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="middle";
    ctx.textAlign="center";
    ctx.font = font;
    ctx.fillStyle=color;
    ctx.fillText(text,x,y);
    ctx.restore();
}

/*-------------------------------------------------------------
所谓的中年危机,
真正让人焦虑的不是孤单,不是贫穷,更不是衰老,
而是人到中年,你才发现你从来都没有按照自己喜欢的方式活过,
这烟火人间事事值得,事事也遗憾呀!
该用多懂事的理智去压抑心中难过和不甘。
我们一直在应对生活中各种苦难和挑战,却忘记了按照自己喜欢的方式活着。
人只有到了中年阶段,才意识到为自己而活,过自己喜欢的生活,
才不会辜负此生。
                                                   --余华
--------------------------------------------------------------*/
//-->
</script>

END


http://www.kler.cn/news/329589.html

相关文章:

  • 微服务 OpenFeign 解析部署使用全流程
  • PHP include和require的区别
  • TiDB 6.0 Book Rush | TiDB 和 Python 的 CRUD 应用开发实践
  • 使用ESP8266扫描WiFi列表
  • H.264编解码工具 - Intel Quick Sync Video
  • 类C语言补充
  • 如何在 macOS(MacBook Pro、Air 和 iMac)上恢复未保存的 Word 文档
  • 灵当CRM index.php接口SQL注入漏洞复现 [附POC]
  • 一文上手SpringSecurity【五】
  • 前端动态创建svg不起效果?
  • 鸿蒙开发:自制原生下拉框
  • 【网络安全 | JAVA代码审计】基础安全问题和解决方法初探
  • 零工市场小程序的未来发展趋势
  • 1-仙灵之谜(区块链游戏详情介绍)
  • Ubuntu 22.04无法连接网络(网络图标丢失)解决方案
  • image离散小波变换及pytorch_wavelets实现
  • Qemu开发ARM篇-7、uboot以及系统网络连接及配置
  • Android Debug Bridge(ADB)完全指南
  • 【重学 MySQL】四十九、阿里 MySQL 命名规范及 MySQL8 DDL 的原子化
  • elasticsearch 写入新数据测试(二)
  • 练习题 - DRF 3.x Validators 验证使用示例和配置方法
  • Java中的switch分支结构
  • 解析Vue2源码diff算法更新子节点逻辑以及优化
  • 专访 Bitlayer 联合创始人 Charlie:探索比特币 Layer2 技术的未来
  • 【高阶数据结构】平衡二叉树(AVL)的删除和调整
  • Hadoop三大组件之MapReduce(一)
  • 计算机毕业设计 C语言学习辅导网站的设计与实现 Java实战项目 附源码+文档+视频讲解
  • C#秒如何转为时分秒格式
  • 智能BI项目第六期
  • 亚信安全天穹5分钟勒索体检 免费试用今起上线