码迷,mamicode.com
首页 > 其他好文 > 详细

canvas——七巧板

时间:2017-05-22 15:13:07      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:close   pie   path   img   http   cti   技术   image   alt   

技术分享

<canvas id="canvas" style="border:1px solid #aaa;display: block;margin: auto;"></canvas>

    <script type="text/javascript">

    var tangram = [
        {p:[{x:0,y:0},{x:300,y:300},{x:0,y:600}],color:"#caff67"},
        {p:[{x:0,y:0},{x:300,y:300},{x:600,y:0}],color:"#67becf"},
        {p:[{x:300,y:600},{x:150,y:450},{x:0,y:600}],color:"#ef3d61"},
        {p:[{x:300,y:600},{x:600,y:300},{x:600,y:600}],color:"#f9f51a"},
        {p:[{x:300,y:300},{x:450,y:450},{x:300,y:600},{x:150,y:450}],color:"#a594c0"},
        {p:[{x:300,y:300},{x:450,y:150},{x:450,y:450}],color:"#fa8ece"},
        {p:[{x:450,y:150},{x:600,y:0},{x:600,y:300},{x:450,y:450}],color:"#f6ca29"},

    ]
        window.onload = function(){
            var canvas = document.getElementById("canvas");

            canvas.width = 600;
            canvas.height = 600;
    
            var context = canvas.getContext("2d");

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

            function draw(piece, cxt){
                cxt.beginPath();
                cxt.moveTo(piece.p[0].x,piece.p[0].y);
                for (var i = 1; i < piece.p.length; i++) {
                    cxt.lineTo(piece.p[i].x,piece.p[i].y);
                }
                cxt.fillStyle = piece.color;
                cxt.closePath();
                
                cxt.fill();
            }
        }
    </script>

 

canvas——七巧板

标签:close   pie   path   img   http   cti   技术   image   alt   

原文地址:http://www.cnblogs.com/WChen1227/p/6888918.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!