继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

动态设置七巧板数组,设置好宽高,加上canvas,就能在网页上绘制出七巧板

inRush
关注TA
已关注
手记 5
粉丝 1
获赞 43
var width = 600,height = 600;
var transform = [
        {p: [{x: 0, y: 0}, {x: 0, y: height}, {x: width/2, y: height/2}], color: "#FF3030"},
        {p: [{x: 0, y: 0}, {x: width, y: 0}, {x: width/2, y: height/2}], color: "#FFD700"},
        {p: [{x: 0, y: height}, {x: width/2, y: height}, {x: width/4, y: height/4*3}], color: "#EE00EE"},
        {p: [{x: width/2, y: height}, {x: width, y: height}, {x: width, y: height/2}], color: "#8B4500"},
        {p: [{x: width/2, y: height}, {x: width/4*3, y: height/4*3}, {x: width/2, y: height/2}, {x: width/4, y: height/4*3}], color: "#87CEEB"},
        {p: [{x: width/2, y: height/2}, {x: width/4*3, y: height/4}, {x: width/4*3, y: height/4*3}], color: "#458B00"},
        {p: [{x: width, y: 0}, {x: width/4*3, y: height/4}, {x: width/4*3, y: height/4*3}, {x: width, y: height/2}], color: "#1C86EE"}
    ];

window.onload = function () {
        var canvas = document.getElementById("canvas");
        canvas.width = width;
        canvas.height = height;
        var context = canvas.getContext('2d');

        for (var i = 0; i < transform.length; i++) {
            var route = transform[i];
            draw(route, context);
        }
    };

    function draw(route, ctx) {
        ctx.beginPath();
        ctx.moveTo(route.p[0].x, route.p[0].y);
        for (var i = 1; i < route.p.length; i++) {
            var path = route.p[i];
            ctx.lineTo(path.x, path.y);
        }
        ctx.closePath();
        ctx.fillStyle = route.color;
        ctx.fill();
        ctx.strokeStyle = "#000";
        ctx.stroke();
    }
打开App,阅读手记
4人推荐
发表评论
随时随地看视频慕课网APP