【成图】

【Canvas与密铺】90年代马赛克密铺效果 1920x1080_canvas 密铺

【Canvas与密铺】90年代马赛克密铺效果 1920x1080_canvas 密铺_02

【Canvas与密铺】90年代马赛克密铺效果 1920x1080_canvas 密铺_03

【Canvas与密铺】90年代马赛克密铺效果 1920x1080_canvas 密铺_04

【代码】

<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
     <title>20世纪90年代马赛克瓷砖效果1920x1080</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>
        </div>
     </body>
</html>
<script type="text/javascript">
<!--
/*****************************************************************
* 将全体代码(从<!DOCTYPE到script>)拷贝下来,粘贴到文本编辑器中,
* 另存为.html文件,再用chrome浏览器打开,就能看到实现效果。
******************************************************************/

// canvas的绘图环境
var ctx;

// 高宽
const WIDTH=1920;
const HEIGHT=1080;

// 舞台对象
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 = "rgb(232,251,169)";
        ctx.fillRect(0,0,WIDTH,HEIGHT);        
        
        const D=30;// 中心距
        const IMax=Math.ceil(WIDTH/D)+1;
        const JMax=Math.ceil(HEIGHT/D);
        // 绘制马赛克瓷砖
        for(var i=0;i<IMax;i++){
            for(var j=0;j<IMax;j++){
                var pt=createPt(i*D,j*D);

                if((i+j)%2==0){
                    //drawSolidCircle(ctx,pt.x,pt.y,3,"red");
                    drawSlashBrick(ctx,pt.x,pt.y,D);
                }else{
                    //drawSolidCircle(ctx,pt.x,pt.y,3,"blue");
                    drawBackSlashBrick(ctx,pt.x,pt.y,D);
                }
            }
        }

        // 横线
        for(var j=0;j<JMax;j++){
            var start=createPt(0,j*D+D/2);
            var end=createPt(WIDTH,j*D+D/2);

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

        // 竖线
        for(var i=0;i<IMax;i++){
            var start=createPt(i*D+D/2,0);
            var end=createPt(i*D+D/2,HEIGHT);

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

        // 蒙版
        ctx.fillStyle="rgba(9,9,9,0.85)";
        ctx.fillRect(0,0,WIDTH,HEIGHT);

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

/*----------------------------------------------------------
函数:用于绘制正斜瓷砖
ctx:绘图上下文
x:立方体中心横坐标
y:立方体中心纵坐标
sideLength:正方形边长
----------------------------------------------------------*/
function drawSlashBrick(ctx,x,y,sideLength){
    const L= sideLength;

    // 四角坐标
    var upLeft=createPt(x-L/2,y-L/2);
    var upRight=createPt(x+L/2,y-L/2);
    var downLeft=createPt(x-L/2,y+L/2);
    var downRight=createPt(x+L/2,y+L/2);

    // 正斜线
    ctx.lineWidth=1;
    ctx.strokeStyle="black";
    ctx.beginPath();
    ctx.moveTo(upRight.x,upRight.y);    
    ctx.lineTo(downLeft.x,downLeft.y);
    ctx.stroke();

    // 左下方块
    ctx.fillStyle = "white";
    ctx.fillRect(downLeft.x,downLeft.y-L/3,L/3,L/3);
    ctx.beginPath();
    ctx.moveTo(downLeft.x,downLeft.y-L/3);    
    ctx.lineTo(downLeft.x+L/3,downLeft.y-L/3);
    ctx.lineTo(downLeft.x+L/3,downLeft.y);
    ctx.stroke();    

    // 右上方块
    ctx.fillStyle = "white";
    ctx.fillRect(upRight.x-L/3,upRight.y,L/3,L/3);
    ctx.beginPath();
    ctx.moveTo(upRight.x-L/3,upRight.y);    
    ctx.lineTo(upRight.x-L/3,upRight.y+L/3);
    ctx.lineTo(upRight.x,upRight.y+L/3);
    ctx.stroke();
}

/*----------------------------------------------------------
函数:用于绘制反斜瓷砖
ctx:绘图上下文
x:立方体中心横坐标
y:立方体中心纵坐标
sideLength:正方形边长
----------------------------------------------------------*/
function drawBackSlashBrick(ctx,x,y,sideLength){
    const L= sideLength;

    // 四角坐标
    var upLeft=createPt(x-L/2,y-L/2);
    var upRight=createPt(x+L/2,y-L/2);
    var downLeft=createPt(x-L/2,y+L/2);
    var downRight=createPt(x+L/2,y+L/2);    

    // 反斜线
    ctx.lineWidth=1;
    ctx.strokeStyle="black";
    ctx.beginPath();
    ctx.moveTo(upLeft.x,upLeft.y);    
    ctx.lineTo(downRight.x,downRight.y);
    ctx.stroke();

    // 左上方块
    ctx.fillStyle = "rgb(66,127,128)";
    ctx.fillRect(upLeft.x,upLeft.y,L/3,L/3);
    ctx.beginPath();
    ctx.moveTo(upLeft.x+L/3,upLeft.y);    
    ctx.lineTo(upLeft.x+L/3,upLeft.y+L/3);
    ctx.lineTo(upLeft.x,upLeft.y+L/3);
    ctx.stroke();

    // 右下方块
    ctx.fillStyle = "rgb(66,127,128)";
    ctx.fillRect(downRight.x-L/3,downRight.y-L/3,L/3,L/3);
    ctx.beginPath();
    ctx.moveTo(downRight.x-L/3,downRight.y);    
    ctx.lineTo(downRight.x-L/3,downRight.y-L/3);
    ctx.lineTo(downRight.x,downRight.y-L/3);
    ctx.stroke();
}

/*----------------------------------------------------------
函数:用于绘制实心圆,用途是标记点以辅助作图
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="bottom";
    ctx.textAlign="center";
    ctx.font = font;
    ctx.fillStyle=color;
    ctx.fillText(text,x,y);
    ctx.restore();
}

/*-------------------------------------------------------------
一只鸡,没有进入麦当劳、没有进入肯德基、德克士、华莱士,甚至没有
进入正新鸡排,就这样庸庸碌碌地过完了一生,真是可悲啊?
对了,谁告诉你,来世上一定要事有所成?
真的,你细想,谁告诉你的。--佚名网友

《庄子.逍遥游》里的朝菌、蟪蛄、冥灵、大椿,不是让你自惭形秽,而是
告诉你:你再大,也有更大的;你再小,也有更小的,如果你不能控制住
你的欲望,你将永远觉得自己渺小,永远活在痛苦中。--小楼春雨
--------------------------------------------------------------*/
//-->
</script>

END