Анимированный фон из фигур на HTML5 и jQuery
1 2 3 4 5 6 |
<div class="wrapper"> <canvas id="geometric" height="300" width="450"> </canvas> <div class="accent"> </div> </div> |
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 |
#geometric { /* border: 1px dotted #ccc; width: 100%; height: 450px; */ position: absolute; top: 0; left: 0; } .accent { /* height: 300px; display: none; */ background-color: #56AA1C; z-index: 100; position: relative; opacity: 0.9; } .controls { position: absolute; top: 20px; right: 20px; z-index: 200; } |
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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
<script> function setOpts(standard, user) { if (typeof user === 'object') { for (var key in user) { standard[key] = user[key]; } } } function randomNumber(min, max) { return Math.floor(Math.random() * (max - min) + min); } function drawRotatedShape(shape, x, y, width, height, degrees, color, direction) { // first save the untranslated/unrotated context context.save(); context.beginPath(); // move the rotation point to the center of the rect context.translate(x + width / 2, y + height / 2); // rotate the rect if (direction == 'clockwise') { context.rotate(degrees * Math.PI / 180); } else { context.rotate(-(degrees * Math.PI / 180)); } context.fillStyle = color; context.lineWidth = 5; // draw the rect on the transformed context // Note: after transforming [0,0] is visually [x,y] // so the rect needs to be offset accordingly when drawn switch (shape) { case 'rect': context.rect(-width / 2, -height / 2, width, height); context.fill(); break; case 'circle': context.arc(-width / 2, -height / 2, width * 0.5, 0, Math.PI * 2, true); context.fill(); break; case 'triangle': context.moveTo(0, 0); context.lineTo(width, 0); context.lineTo(width, height); context.fill(); break; case 'minus': context.moveTo(0, 0); context.lineTo(30, 0); context.stroke(); break; case 'plus': context.moveTo(0, 15); context.lineTo(30, 15); context.moveTo(15, 0); context.lineTo(15, 30); context.stroke(); break; case 'equals': context.moveTo(0, 5); context.lineTo(30, 5); context.moveTo(0, 15); context.lineTo(30, 15); context.stroke(); break; } // restore the context to its untranslated/unrotated state context.restore(); } // Initialise an empty canvas and place it on the page var canvas = document.getElementById("geometric"); var context = canvas.getContext("2d"); $(canvas).attr('width', $(window).width()).attr('height', $(window).height()); $('.accent').height($(window).height()); $(window).resize(function() { $(canvas).attr('width', $(window).width()).attr('height', $(window).height()); $('.accent').height($(window).height()); }); //$(canvas).width($(window).width()); var particles = {}, particleIndex = 0, settings; // Set up a function to create multiple particles function Particle(x, y) { // Establish starting positions and velocities this.x = x; this.y = y; // Determine original X-axis speed based on setting limitation this.vx = Math.floor(randomNumber(0, 10)); this.vy = Math.floor(randomNumber(0, 10)); this.size = Math.floor(randomNumber(settings.minSize, settings.maxSize)); this.rotation = Math.floor(randomNumber(0, 180)); if (randomNumber(0, 100) > 50) { this.rotationDirection = 'clockwise'; } else { this.rotationDirection = 'counter-clockwise'; } this.shape = settings.shapes[randomNumber(0, settings.shapes.length)]; // Add new particle to the index // Object used as it's simpler to manage that an array particleIndex++; particles[particleIndex] = this; this.id = particleIndex; this.life = 0; this.maxLife = 10000; } // Some prototype methods for the particle's "draw" function Particle.prototype.draw = function() { // this.x += 1; this.y += settings.gravity * Math.floor(randomNumber(1, 2)); // Adjust for gravity this.vy += settings.gravity * Math.floor(randomNumber(0.5, 8)); // Age the particle this.life++; if (this.rotation == 'clockwise') { this.rotation += settings.rotationVelocity; } else { this.rotation -= settings.rotationVelocity; } // If Particle is old, it goes in the chamber for renewal if (this.life >= this.maxLife) { delete particles[this.id]; } // Create the shapes var size = this.size; context.clearRect(canvas.width, canvas.height, canvas.width, canvas.height); //context.beginPath(); context.fillStyle = settings.color; //context.rect(this.x,this.y, size, size); drawRotatedShape(this.shape, this.x, this.y, size, size, this.rotation, settings.color, this.rotationDirection); //context.closePath(); //context.fill(); /* if(settings.rotation == true) { context.rotate(this.rotation); }*/ }; function shapify(preset) { switch (preset) { case 'math': settings = { density: 60, velocity: 1, //startingX: canvas.width / 2, //startingY: canvas.height / 4, gravity: -0.6, color: 'rgba(0,0,0,0.3)', maxSize: 100, minSize: 15, rotation: true, rotationVelocity: 0.1, shapes: ['rect', 'circle', 'triangle', 'plus', 'minus', 'equals'] }; break; case 'science': settings = { density: 5, velocity: 1, //startingX: canvas.width / 2, //startingY: canvas.height / 4, gravity: -0.6, color: 'rgba(0,0,0,0.3)', maxSize: 100, minSize: 15, rotation: true, rotationVelocity: 0.05, shapes: ['cell', 'dna'] }; break; } for (var i = 0; i < settings.density; i++) { new Particle(Math.floor(randomNumber(0, canvas.width)), Math.floor(randomNumber(0, canvas.height))); } for (var p in particles) { particles[p].draw(); } setInterval(function() { context.fillStyle = "#ededed"; context.fillRect(0, 0, canvas.width, canvas.height); // Draw the particles for (var i = 0; i < settings.density; i++) { if (Math.random() > 0.999) { // Introducing a random chance of creating a particle // corresponding to an chance of 1 per second, // per "density" value new Particle(Math.floor(randomNumber(0, canvas.width)), (canvas.height + 70)); } } for (var p in particles) { particles[p].draw(); } }, 30); } shapify('math'); </script> |
Че никто не рискнул поставить? 😀
Думаю, если бы возникли вопросы, комментарии не заставили б себя долго ждать