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

金刚石图纹

时间:2015-08-04 13:29:40      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

技术分享


<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="jquery.min.js"></script>
        <style type="text/css">
            
            .info {     
                position: relative;                           
                margin-bottom: 5px;
            }             
            
            .info input {
                width: 60px;
                height: 20px;   
            }  
                        
            #show {
                border: 3px solid #bababa;
            }
            
        </style>
    </head>
    <body id="body">
        <div id="get-info">
            <div class="info">
                <label>Node Num: </label><input id="node-num-input"/>
            </div>
            <div class="info">
                <label>Radius: </label><input id="radius-input"/>
            </div>
            <button id="submit-info">submit</button>
        </div>
        <canvas id="show" width="500px" height="500px"></canvas>
        <script type="text/javascript">        
            function draw( node_num, radius ) {
                var canvas = $( "#show" )[0];
                var context = canvas.getContext( "2d" );   
                var thta = 2.0 * Math.PI / node_num;
                var points = [];
                
                context.clearRect( 0, 0, 500, 500 );
                for ( var i = 0; i < node_num; ++i ) {
                    points.push( {
                        "X" : radius * Math.cos( i * thta ) + 250,
                        "Y" : radius * Math.sin( i * thta ) + 250
                    } );
                }
                context.beginPath();
                for ( var i = 0; i < node_num - 1; ++i ) {
                    for ( var j = i + 1; j <= node_num - 1; ++j ) {
                        context.moveTo( Math.round( points[i]["X"] ), Math.round( points[i]["Y"] ) );
                        context.lineTo( Math.round( points[j]["X"] ), Math.round( points[j]["Y"] ) );
                    }
                }
                context.closePath();
                context.stroke();
            }                    
            
            $( "#submit-info" ).click(
                function () {
                    var node_num = $( "#node-num-input" ).val();
                    var radius = $( "#radius-input" ).val();
                    draw( node_num, radius );
                }
            );
        </script>
    </body>
</html>


版权声明:本文为博主原创文章,未经博主允许不得转载。

金刚石图纹

标签:

原文地址:http://blog.csdn.net/pandora_madara/article/details/47275283

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