【成图】

【Canvas与艺术】红色纵横田字结_canvas 绳结

【Canvas与艺术】红色纵横田字结_canvas 绳结_02

【Canvas与艺术】红色纵横田字结_canvas 绳结_03

【代码】
<!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>
        </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 = "white";
        ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);

        const N=4;
        const D=40;// 圆心到最内正方形尖角距离,调节此参数即调整图大小
        const W=12;// lineWidth,线宽
        const G=4; // Gap Wdith,隔空宽度
        ctx.lineWidth=W;
        ctx.strokeStyle="red";
        for(var i=0;i<N;i++){
            var theta=Math.PI*2/N*i-Math.PI/4*3;
            var a=createPt(D*Math.cos(theta),D*Math.sin(theta));

            // a段
            var angle=theta+Math.PI/4*3;
            var r=W/2+G;
            var a0=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle));
                        
            angle=theta+Math.PI/4*3;
            r=(D*Math.sqrt(2)-W/2-G)*2;
            var a1=createPt(a0.x+r*Math.cos(angle),a0.y+r*Math.sin(angle));

            ctx.beginPath();
            ctx.moveTo(a0.x,a0.y);
            ctx.lineTo(a1.x,a1.y);
            ctx.stroke();

            // b段
            angle=theta+Math.PI/4*3;
            r=W+2*G;
            var b1=createPt(a1.x+r*Math.cos(angle),a1.y+r*Math.sin(angle));

            angle=theta+Math.PI/4*3;
            r=2*D*Math.sqrt(2);
            var b2=createPt(b1.x+r*Math.cos(angle),b1.y+r*Math.sin(angle));

            angle=theta+Math.PI/4*3+Math.PI/2;
            r=D*Math.sqrt(2);
            var b3=createPt(b2.x+r*Math.cos(angle),b2.y+r*Math.sin(angle));

            angle=theta+Math.PI/4*3+Math.PI;
            r=3*D*Math.sqrt(2);
            var b4=createPt(b3.x+r*Math.cos(angle),b3.y+r*Math.sin(angle));

            ctx.beginPath();
            ctx.moveTo(b1.x,b1.y);
            ctx.lineTo(b2.x,b2.y);
            ctx.lineTo(b3.x,b3.y);
            ctx.lineTo(b4.x,b4.y);
            ctx.stroke();

            // C段
            angle=theta+Math.PI/4*3;
            r=W/2+G;
            var c=createPt(a1.x+r*Math.cos(angle),a1.y+r*Math.sin(angle));

            angle=theta+Math.PI/4*3+Math.PI/2;
            r=D*Math.sqrt(2)-W/2-G;
            var c1=createPt(c.x+r*Math.cos(angle),c.y+r*Math.sin(angle));

            angle=theta+Math.PI/4*3-Math.PI/2;
            r=D*Math.sqrt(2)-W/2-G;
            var c2=createPt(c.x+r*Math.cos(angle),c.y+r*Math.sin(angle));

            ctx.beginPath();
            ctx.moveTo(c1.x,c1.y);
            ctx.lineTo(c2.x,c2.y);
            ctx.stroke();

            // D段
            angle=theta+Math.PI/4*3-Math.PI/2;
            r=W+2*G;
            var d1=createPt(c2.x+r*Math.cos(angle),c2.y+r*Math.sin(angle));

            angle=theta+Math.PI/4*3-Math.PI/2;
            r=2*D*Math.sqrt(2);
            var d2=createPt(d1.x+r*Math.cos(angle),d1.y+r*Math.sin(angle));

            angle=theta+Math.PI/4*3;
            r=2*D*Math.sqrt(2)+2*G;
            var d3=createPt(d2.x+r*Math.cos(angle),d2.y+r*Math.sin(angle));

            angle=theta+Math.PI/4*3+Math.PI/2;
            r=2*D*Math.sqrt(2)+W/2+G;
            var d4=createPt(d3.x+r*Math.cos(angle),d3.y+r*Math.sin(angle));

            angle=theta+Math.PI/4*3+Math.PI;
            r=3*D*Math.sqrt(2)-G/2;
            var d5=createPt(d4.x+r*Math.cos(angle),d4.y+r*Math.sin(angle));

            ctx.beginPath();
            ctx.moveTo(d1.x,d1.y);
            ctx.lineTo(d2.x,d2.y);
            ctx.lineTo(d3.x,d3.y);
            ctx.lineTo(d4.x,d4.y);
            ctx.lineTo(d5.x,d5.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();
}

/*-------------------------------------------------------------
1.发现一个奇怪的现象:越是喜欢单打独斗靠自己的人,往往原生家庭,条件和成员之间彼此关系都不好。而越是善于拉帮结伙、团队行动的,往往原生家庭中经济条件不错和成员关系都非常融洽。

2.决定一个人立场和观点的,只有他切身利益,和事实关系不大。

3.资源的分配,往往是自上而下。而事情工作量的分配,往往是自下而上。

4.你值多少钱,你自己说了不算,要那个愿意付钱的人才说了算。价值不等于价格,价值更不等于需求。你有屠龙术,无龙可屠,那你就是普通人。

5.为什么会有人背叛你、伤害你、欺骗你?不是你不够好,而是你没有让别人看到你被冒犯之后的狠。相比起得到好处,人更害怕被伤害。当你的狠别人都知道时,别人就会掂量一下和你对着干的下场是什么,哪怕有好处可能都不算了。

6.最喜欢成功学的莫过于老板,但老板并不会去读、看、学,而是会让下面员工去学。因为只有这样才能把一些员工忽悠起来卖力干活,这样老板就能继续成功了。

7.做生意赚钱,本质就是利用信息差的问题。信息差的问题不是你知不知道的问题,而是你找不找得到,能不能分辨真假,更不能分析出更深的东西。这就好比,同样看报纸,为啥别人能看到上级,你就看到八卦娱乐?说是信息差的问题,其实是信息处理能力的问题。

8.如果别人只会在道德层面谴责你,只能说明一个问题,你做的这事合法合规,别人也拿你没办法,他只能用道德来对你无能狂怒。

9.人对别人和自己往往会用两套行为准则,对别人的严苛一点,对自己的宽松一些。但如果别人也如此对你,你会跳起来说自己受到了不公正待遇。

10.情商高有一个非常明显的特点,我说话,你说不对,要反驳我,我也不解释,反正就是你说的对,不花一点时间和精力来和你辩论。
--------------------------------------------------------------*/
//-->
</script>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.
  • 228.
  • 229.
  • 230.
  • 231.
  • 232.
  • 233.
  • 234.
  • 235.
  • 236.
  • 237.
  • 238.
  • 239.
  • 240.
  • 241.
  • 242.
  • 243.
  • 244.
  • 245.
  • 246.
  • 247.
  • 248.
  • 249.
  • 250.
  • 251.
  • 252.
  • 253.
  • 254.
  • 255.
  • 256.
  • 257.
  • 258.
  • 259.
  • 260.
  • 261.
  • 262.
  • 263.
END