为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
Html5
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();
    }
点击查看更多内容
4人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消