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
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style>
#canvas1 { position:absolute; top: 0; left: 0; background-color: rgb(11, 4, 4); z-index: 11; display: none; } #canvas2 { position:absolute; top: 0; left: 0; background: rgb(11, 4, 4); z-index: -11; } </style> </head> <body> <canvas id="canvas1"></canvas> <canvas id="canvas2"></canvas>
<script> /** @type {HTMLCanvasElement} */ const canvas1 = document.getElementById('canvas1') const ctx1 = canvas1.getContext('2d') canvas1.width = 200 canvas1.height= 200 //画布设置 ctx1.lineCap = 'round'; //阴影设置 ctx1.shadowColor = 'rgba(0,0,0,0.7)'; ctx1.shadowOffsetX = 10; ctx1.shadowOffsetY = 5; ctx1.shadowBlur = 10;
//第二个画布 const canvas2 = document.getElementById('canvas2'); const ctx2 = canvas2.getContext('2d'); canvas2.width = window.innerWidth; canvas2.height = window.innerHeight; let color = 'hsl('+Math.random()*100+',50%,50%)' function drawLine() { ctx1.beginPath() ctx1.moveTo(0,0) ctx1.lineTo(100, 0) ctx1.lineWidth=4 ctx1.strokeStyle = color ctx1.stroke() }
function draw() { ctx1.save() ctx1.translate(canvas1.width/2, canvas1.height/2); for (let i = 0; i < 12; i++){ ctx1.rotate((Math.PI * 2)/12); drawLine(); } ctx1.restore() }
draw()
const image = new Image() image.src = canvas1.toDataURL();
class Snow { constructor(canvasWidth, canvasHeight, image){ this.canvasWidth = canvasWidth this.canvasHeight = canvasHeight this.image = image
this.size = Math.random() * 0.4 + 0.1 this.width = this.image.width * this.size this.height = this.image.height * this.size this.x = Math.random() * this.canvasWidth this.y = Math.random() * this.canvasHeight this.speed = Math.random() * 1 + 1; this.angle = 0; this.va = Math.random() * 0.05 - 0.025;
} update(){ this.angle += this.va; if (this.y < -this.height) { this.y = this.canvasHeight + this.height; this.x = Math.random() * (this.canvasWidth - this.width); this.angle = 1; } else this.y -= this.speed; } draw(ctx){ ctx.save() ctx.translate(this.x, this.y) ctx.rotate(this.angle) ctx.drawImage(this.image, -this.width/2, -this.height/2, this.width, this.height) ctx.restore(); } }
let snows = [] image.onload = function(){ for (let i = 0; i < 50; i++){ snows.push(new Snow(canvas2.width, canvas2.height, image)) } function animate() { ctx2.clearRect(0,0,canvas2.width, canvas2.height) for(let i=0;i<snows.length;i++) { snows[i].draw(ctx2) snows[i].update() } window.requestAnimationFrame(animate) } animate() } </script> </body> </html>
|