1. 新建js
  2. 引入js
  3. 别忘了canvas标签
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    // 落叶效果
    var stop, staticx;
    var img = new Image();
    img.src = "图片地址";

    var leafSize = 40; //树叶大小
    var leafNum = 8; //数量

    function Leaves(x, y, s, r, fn) {
    this.x = x;
    this.y = y;
    this.s = s;
    this.r = r;
    this.fn = fn
    }
    Leaves.prototype.draw = function(cxt) {
    cxt.save();
    var xc = 20 * this.s / 2;
    cxt.translate(this.x, this.y);
    cxt.rotate(this.r);
    cxt.drawImage(img, 0, 0, leafSize * this.s, leafSize * this.s);
    cxt.restore()
    };
    Leaves.prototype.update = function() {
    this.x = this.fn.x(this.x, this.y);
    this.y = this.fn.y(this.y, this.y);
    this.r = this.fn.r(this.r);
    if (this.x > window.innerWidth || this.x < 0 || this.y > window.innerHeight || this.y < 0) {
    this.r = getRandom("fnr");
    if (Math.random() > 0.4) {
    this.x = getRandom("x");
    this.y = 0;
    this.s = getRandom("s");
    this.r = getRandom("r")
    } else {
    this.x = window.innerWidth;
    this.y = getRandom("y");
    this.s = getRandom("s");
    this.r = getRandom("r")
    }
    }
    };
    LeavesList = function() {
    this.list = []
    };
    LeavesList.prototype.push = function(leaves) {
    this.list.push(leaves)
    };
    LeavesList.prototype.update = function() {
    for (var i = 0, len = this.list.length; i < len; i++) {
    this.list[i].update()
    }
    };
    LeavesList.prototype.draw = function(cxt) {
    for (var i = 0, len = this.list.length; i < len; i++) {
    this.list[i].draw(cxt)
    }
    };
    LeavesList.prototype.get = function(i) {
    return this.list[i]
    };
    LeavesList.prototype.size = function() {
    return this.list.length
    };

    // 计算长、宽、位置、旋转的随机
    function getRandom(option) {
    var ret, random;
    switch (option) {
    case "x":
    ret = Math.random() * window.innerWidth;
    break;
    case "y":
    ret = Math.random() * window.innerHeight;
    break;
    case "s":
    ret = 0.3 + Math.random();
    break;
    case "r":
    ret = Math.random() * 4;
    break;
    case "fnx":
    random = -0.5 + Math.random() * 1;
    ret = function(x, y) {
    return x + 0.5 * random - 1.7
    };
    break;
    case "fny":
    random = 1.5 + Math.random() * 0.7;
    ret = function(x, y) {
    return y + random
    };
    break;
    case "fnr":
    random = Math.random() * 0.03;
    ret = function(r) {
    return r + random
    };
    break
    }
    return ret
    }

    function startLeaves() {
    requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame || window.oRequestAnimationFrame;
    var canvas = document.createElement("canvas"),
    cxt;
    staticx = true;
    canvas.height = window.innerHeight;
    canvas.width = window.innerWidth;
    canvas.setAttribute("style", "position: fixed;left: 0;top: 0;pointer-events: none;");
    canvas.setAttribute("id", "canvas_leaves");
    document.getElementsByTagName("body")[0].appendChild(canvas);
    cxt = canvas.getContext("2d");
    var leavesList = new LeavesList();
    for (var i = 0; i < leafNum; i++) {
    var leaves, randomX, randomY, randomS, randomR, randomFnx, randomFny;
    randomX = getRandom("x");
    randomY = getRandom("y");
    randomR = getRandom("r");
    randomS = getRandom("s");
    randomFnx = getRandom("fnx");
    randomFny = getRandom("fny");
    randomFnR = getRandom("fnr");
    leaves = new Leaves(randomX, randomY, randomS, randomR, {
    x: randomFnx,
    y: randomFny,
    r: randomFnR
    });
    leaves.draw(cxt);
    leavesList.push(leaves)
    }
    stop = requestAnimationFrame(function() {
    cxt.clearRect(0, 0, canvas.width, canvas.height);
    leavesList.update();
    leavesList.draw(cxt);
    stop = requestAnimationFrame(arguments.callee)
    })
    }
    window.onresize = function() {
    var canvasSnow = document.getElementById("canvas_snow")
    };
    img.onload = function() {
    startLeaves()
    };

    function stopp() {
    if (staticx) {
    var child = document.getElementById("canvas_leaves");
    child.parentNode.removeChild(child);
    window.cancelAnimationFrame(stop);
    staticx = false
    } else {
    startLeaves()
    }
    };
  4. 图片

代码来源

https://hi-coder.gitee.io/