欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > 画一颗随机数

画一颗随机数

2025/2/23 21:12:17 来源:https://blog.csdn.net/print_2022/article/details/144571671  浏览:    关键词:画一颗随机数

代码:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>codePen - Random Tree</title>
</head>
<body><canvas></canvas><script>const canvas = document.querySelector('canvas');const ctx = canvas.getContext('2d');// canvas.width = window.innerWidth * devicePixelRatio;// canvas.height = window.innerHeight * devicePixelRatio;canvas.width = window.innerWidth;canvas.height = window.innerHeight;ctx.translate(canvas.width / 2, canvas.height);ctx.scale(1, -1);drawBranch([0, 0], 200, 30, 90);function drawBranch (v0, len, thick, angle) {if(thick < 10 && Math.random() < 0.3) {return;}if (thick < 2) {ctx.beginPath()ctx.arc(...v0, 4, 0, 2 * Math.PI);ctx.fillStyle = 'red';ctx.fill();return;}ctx.beginPath();ctx.moveTo(...v0);const v1 = [v0[0] + len * Math.cos(((angle) * Math.PI) / 180),v0[1] + len * Math.sin(((angle) * Math.PI) / 180)]ctx.lineTo(...v1);ctx.strokeStyle = '#333';ctx.lineWidth = thick;ctx.lineCap = 'round';ctx.stroke();drawBranch(v1, len * 0.8, thick * 0.7, angle + Math.random() * 30);drawBranch(v1, len * 0.8, thick * 0.7, angle - Math.random() * 30);}
</script>
</body>
</html>

效果:

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词