JavaScript+html5 canvas繪製繽紛多彩的三角形效果完整執行個體,html5canvas

來源:互聯網
上載者:User

JavaScript+html5 canvas繪製繽紛多彩的三角形效果完整執行個體,html5canvas

本文執行個體講述了JavaScript+html5 canvas繪製繽紛多彩的三角形效果。分享給大家供大家參考,具體如下:

運行效果如下:

具體代碼如下:

<!DOCTYPE HTML><html> <head>  <title>demo</title>  <style type="text/css">   body {    margin:0; padding:0;    }   #canvas {    width:500px; height:500px; border:3px solid #F2F2F2; box-shadow:0px 0px 25px #494949; margin:0 auto;    margin-left:200px; margin-top:50px;   }  </style> </head> <body>  <canvas id="canvas" width="500px" height="500px"></canvas>  <script type="text/javascript">   var colorArray = "01234567890ABCDEFabcdef".split("");   var canvas = document.getElementById("canvas");   var ctx = canvas.getContext("2d");   function createTriangle(startPos, r, color) {    var startX = startPos.x,     startY = startPos.y;     ctx.save();     ctx.strokeStyle = color || "black";     ctx.beginPath();     ctx.lineWidth=2;     ctx.moveTo(startX, startY);     ctx.lineTo(startX+r*Math.sin(Math.PI/6), startY+r*Math.cos(Math.PI/6));     ctx.lineTo(startX-r*Math.sin(Math.PI/6), startY+r*Math.cos(Math.PI/6));     ctx.lineTo(startX, startY);     ctx.closePath();      ctx.stroke();     ctx.restore();   }   function createColor() {    var color = "#";    for(var i=0; i<6; i++) {     color += colorArray[Math.floor(Math.random()*colorArray.length)];    }    return color;   }   for(var i=0; i<100; i++) {    var x = Math.round(Math.random()*500),     y = Math.round(Math.random()*500),     color = createColor();    console.log(color);    createTriangle({x: x, y: y}, 50, color);    }  </script> </body></html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.