canvas随机绘制各种不同的树木效果代码

代码语言:html

所属分类:布局界面

代码描述:canvas随机绘制各种不同的树木效果代码,点击左上角new tree即可生成新的树木。

代码标签: canvas 随机 绘制 各种 不同 树木

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <style>
        html,body,canvas{
               margin:0;
               padding:0
               
           }
               html,body{
                   width:100%;
                   height:100%;
                   overflow-x:hidden;
                   background-color:limeGreen;
                   
               }
    </style>
</head>

<body>
<canvas id="cvs"></canvas>

    <script>
        var degToRad = Math.PI / 180;
    var body = document.getElementsByTagName("html")[0];
    var wid = body.clientWidth + 17;
    var hei = Math.max(530, body.clientHeight - 10);
    var cvs = document.getElementById("cvs");
    cvs.height = hei;
    cvs.width = wid;
    var seasons = ["winter", "spring", "summer", "fall"];
    var season = seasons[Math.floor(Math.random() * seasons.length)];
    var ctx = cvs.getContext("2d");
    
    function randomizeOpacity(f, e, d) {
        f += Math.max(0, Math.min(255, 50 - Math.floor(Math.random() * 100)));
        e += Math.max(0, Math.min(255, 50 - Math.floor(Math.random() * 100)));
        d += Math.max(0, Math.min(255, 50 - Math.floor(Math.random() * 100)));
        var c = Math.random() * 0.6 + 0.4;
        return "rgba(" + f + "," + e + "," + d + "," + c + ")"
    }
    
    function makeLeaf(c, d, a, b) {
        branch(c, d, 0, 0, a, 0, 1, b, true)
    }
    
    function branch(r, s, q, l, c, j, g, b, k) {
        ctx.lineWidth = q;
        ctx.lineCap = "round";
        ctx.strokeStyle = "saddlebrown";
        ctx.beginPath();
        ctx.moveTo(r, hei - s);
        g = g || 0;
        b = b || 0;
        k = k || false;
        if (k) {
            l = 5;
            ctx.lineWidth = 3;
            if (season === "spring") {
                ctx.strokeStyle = Math.random() * 5 > 3 ? randomizeOpacity(255, 192, 203) : randomizeOpacity(0, 128, 0)
            } else {
                if (season === "summer") {
                    ctx.strokeStyle = randomizeOpacity(0, 128, 0)
                } else {
                    if (season === "fall") {
                        var p = "transparent";
                        var f = [randomizeOpacity(255, 165, 0), randomizeOpacity(255, 0, 0), randomizeOpacity(255, 165, 0)];
                        ctx.strokeStyle = Math.random() * 15 < 13 ? "transparent" : f[Math.floor(Math.random() * f.length)]
                    } else {
                        ctx.strokeStyle = "transparent"
                    }
                }
            }
            ctx.lineCap = "butt"
        }
        var n = (g === 0 || k) ? l : l * (0.5 + Math.random() * 0.6);
        var m = g === 0 ? b : b + (4 * c - (Math.random() * 8 * c));
        var d = r + (n * Math.sin(b * degToRad));
        var e = s + (n * Math.cos(b * degToRad));
        ctx.lineTo(d, hei - e);
        var h = 3 + Math.floor(Math.random() * j);
        if (!k && g >= h) {
            ctx.lineWidth = Math.min(1, ctx.lineWidth)
        }
        ctx.stroke();
        b += 10 - (Math.random() * 20);
        if (!k && g < h) {
            var o = Math.random() * 2 > 1 ? -1 : 1;
            branch(d, e, Math.max(0.2, q * 0.7), l * 0.85, c, j, g + 1, b + o * c);
            branch(d, e, Math.max(0.2, q * 0.7), l * 0.85, c, j, g + 1, b - o * c);
            if (Math.random() * 2 > 1) {
                branch(r + (d - r) * 0..........完整代码请登录后点击上方下载按钮下载查看

网友评论0