HTML5 用 canvas 繪製心形線

來源:互聯網
上載者:User

標籤:height   net   bsp   win   刪除   -o   image   pow   tin   

  1. 笛卡爾的心形線

  2. 桃心形

  3. 這個桃心比較符合口味。。遂決定使用此方程。。

    當然還有一些其它方程,如下:

  4. 下面就開始寫程式了.

    <!DOCTYPE html>  

    <html>   

    <head>  

        <title>Draw Heart</title>  

        <style type="text/css">  

            * {  

                margin: 0;  

                padding: 0;  

            }  

      

            html {  

                height: 100%;  

                margin: 0;  

            }  

      

            body {  

                height: 100%;  

                  

            }  

     

            #canvasZone {  

                width: 100%;  

                height: 100%;  

                text-align: center;  

                background-color: black;  

            }  

     

            #myCanvas {  

                height:100%;  

                display: block;  

                /*background-color:aqua;*/  

            }  

        </style>  

        <script type="text/javascript">  

            var arr = [];//儲存所有的XY座標,只為驗證。實際程式中可刪除。  

            var r = 4;  

            var radian;//弧度  

            var i;  

            var radianDecrement;//弧度增量  

            var time = 10;//每個點之間的時間間隔  

            var intervalId;  

            var num = 360;//分割為 360 個點  

            var startRadian = Math.PI;  

            var ctx;  

            window.onload = function () {  

                startAnimation();  

            }  

            function startAnimation() {  

                ctx = document.getElementById("myCanvas").getContext("2d");  

                //讓畫布撐滿整個螢幕,-20是捲軸的位置,需留出。如捲軸出現則會擠壓畫布。  

                WINDOW_HEIGHT=document.documentElement.clientHeight-20;  

                WINDOW_WIDTH=document.documentElement.clientWidth-20;  

                ctx.width = WINDOW_WIDTH;  

                ctx.heigh = WINDOW_HEIGHT;  

                drawHeart();  

            }  

      

            function drawHeart() {  

      

                ctx.strokeStyle = "red";  

                ctx.lineWidth = 1;//設定線的寬度  

                radian = startRadian;//弧度設為初始弧度  

                radianDecrement = Math.PI / num * 2;  

                ctx.moveTo(getX(radian), getY(radian));//移動到初始點  

                i = 0;  

                intervalId = setInterval("printHeart()", time);  

            }  

            //x = 16 sin^3 t, y = (13 cos t - 5 cos 2t - 2 cos 3t - cos 4t)  

            function printHeart() {  

                radian += radianDecrement;  

                ctx.lineTo(getX(radian), getY(radian));//在舊點和新點之間連線  

                //arr.push("X:" + getX(radian) + "<br/>Y:" + getY(radian) + "<br/>");  

                i++;  

                ctx.stroke();//畫線  

                if (i >= num) {  

                    clearInterval(intervalId);  

                    //document.getElementById("bs").innerHTML = arr.join("");//列印所有的XY座標點。  

                }  

            }  

            function getX(t) {//由弧度得到 X 座標  

                return 100 + r * (16 * Math.pow(Math.sin(t), 3));  

            }  

      

            function getY(t) {//由弧度得到 Y 座標  

                return 50 - r * (13 * Math.cos(t) - 5 * Math.cos(2 * t) - 2 * Math.cos(3 * t) - Math.cos(4 * t));  

            }  

        </script>   

    </head>    

    <body>    

        <div id="canvasZone">  

            <canvas id="myCanvas"></canvas>  

        </div>  

        <div id="bs">  

        </div>    

    </body>    

    </html> 

HTML5 用 canvas 繪製心形線

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.