关键词搜索

源码搜索 ×
×

canvas笔记-画一片星空

发布2020-05-31浏览1785次

详情内容

程序运行截图如下:

源码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <canvas id="canvas" style="border:1px solid #aaa;display:block;margin:50px auto;">
  9. 当前浏览器不支持Canvas,请更换浏览器后再试
  10. </canvas>
  11. <script>
  12. window.onload = function() {
  13. let canvas = document.getElementById("canvas");
  14. canvas.width = 800;
  15. canvas.height = 800;
  16. let context = canvas.getContext("https://cdn.jxasp.com:9143/image/2d");
  17. context.fillStyle = "black";
  18. context.fillRect(0, 0, canvas.width, canvas.height);
  19. for(let i = 0; i < 200; i++){
  20. let r = Math.random() * 10 + 10;
  21. let x = Math.random() * canvas.width;
  22. let y = Math.random() * canvas.height;
  23. let a = Math.random() * 360;
  24. drawStar(context, x, y, r, r / 2.0, a);
  25. }
  26. }
  27. function drawStar(cxt, x, y, outerR, innerR, rot){
  28. cxt.beginPath();
  29. for(let i = 0; i < 5; i++){
  30. cxt.lineTo(Math.cos((18 + i * 72 - rot) / 180 * Math.PI) * outerR + x,
  31. -Math.sin((18 + i * 72- rot) / 180 * Math.PI) * outerR + y);
  32. cxt.lineTo(Math.cos((54 + i * 72 - rot) / 180 * Math.PI) * innerR + x,
  33. -Math.sin((54 + i * 72 - rot) / 180 * Math.PI) * innerR + y);
  34. }
  35. cxt.closePath();
  36. cxt.fillStyle = "#fb3";
  37. cxt.strokeStyle = "#fd5";
  38. cxt.lineWidth = 3;
  39. cxt.lineJoin = "round";
  40. cxt.fill();
  41. cxt.stroke();
  42. }
  43. </script>
  44. </body>
  45. </html>

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载