Страницы

Поиск по вопросам

воскресенье, 15 декабря 2019 г.

Северное сияние на фон. CSS, JS?

#javascript #html5 #css3 #background #эффекты


Задумка сделать фоном переливающееся северное сияние или что-то на подобие (именно
фоном, не эффект текста). Мои познания в JS слишком скудны, чтоб написать такой скрипт
самому.
Подскажите, может кто встречал что-то подобное?
    


Ответы

Ответ 1



Это легко гуглится. Но раз тут не любят вгуглпосылателей, то вот: Не особо впечатляет, но... .bubble { width: 500px; height: 500px; position: absolute; border-radius: 500px; -webkit-filter: blur(200px); -webkit-animation: pulse 2s alternate infinite; } .bubble-blue { background: blue; } .bubble-yellow { background: yellow; } .bubble-red { background: red; } .bubble-green { background: green; } .bubble-purple { background: purple; } @-webkit-keyframes pulse { from {-webkit-filter: blur(200px) opacity(0%);} to {-webkit-filter: blur(250px) opacity(100%);} }
http://codepen.io/JoJoZ/pen/YymvQr Halm + SCSS: https://codepen.io/insprd/pen/Hjsye/ // Ported from Stefan Gustavson's java implementation // http://staffwww.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf // Read Stefan's excellent paper for details on how this code works. // // Sean McCullough banksean@gmail.com // // Added 4D noise // Joshua Koo zz85nus@gmail.com /** * You can pass in a random number generator object if you like. * It is assumed to have a random() method. */ var SimplexNoise = function(r) { if (r == undefined) r = Math; this.grad3 = [[1,1,0],[-1,1,0],[1,-1,0],[-1,-1,0], [1,0,1],[-1,0,1],[1,0,-1],[-1,0,-1], [0,1,1],[0,-1,1],[0,1,-1],[0,-1,-1]]; this.grad4 = [[0,1,1,1], [0,1,1,-1], [0,1,-1,1], [0,1,-1,-1], [0,-1,1,1], [0,-1,1,-1], [0,-1,-1,1], [0,-1,-1,-1], [1,0,1,1], [1,0,1,-1], [1,0,-1,1], [1,0,-1,-1], [-1,0,1,1], [-1,0,1,-1], [-1,0,-1,1], [-1,0,-1,-1], [1,1,0,1], [1,1,0,-1], [1,-1,0,1], [1,-1,0,-1], [-1,1,0,1], [-1,1,0,-1], [-1,-1,0,1], [-1,-1,0,-1], [1,1,1,0], [1,1,-1,0], [1,-1,1,0], [1,-1,-1,0], [-1,1,1,0], [-1,1,-1,0], [-1,-1,1,0], [-1,-1,-1,0]]; this.p = []; for (var i=0; i<256; i++) { this.p[i] = Math.floor(r.random()*256); } // To remove the need for index wrapping, double the permutation table length this.perm = []; for(var i=0; i<512; i++) { this.perm[i]=this.p[i & 255]; } // A lookup table to traverse the simplex around a given point in 4D. // Details can be found where this table is used, in the 4D noise method. this.simplex = [ [0,1,2,3],[0,1,3,2],[0,0,0,0],[0,2,3,1],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,2,3,0], [0,2,1,3],[0,0,0,0],[0,3,1,2],[0,3,2,1],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,3,2,0], [0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0], [1,2,0,3],[0,0,0,0],[1,3,0,2],[0,0,0,0],[0,0,0,0],[0,0,0,0],[2,3,0,1],[2,3,1,0], [1,0,2,3],[1,0,3,2],[0,0,0,0],[0,0,0,0],[0,0,0,0],[2,0,3,1],[0,0,0,0],[2,1,3,0], [0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0], [2,0,1,3],[0,0,0,0],[0,0,0,0],[0,0,0,0],[3,0,1,2],[3,0,2,1],[0,0,0,0],[3,1,2,0], [2,1,0,3],[0,0,0,0],[0,0,0,0],[0,0,0,0],[3,1,0,2],[0,0,0,0],[3,2,0,1],[3,2,1,0]]; }; SimplexNoise.prototype.dot = function(g, x, y) { return g[0]*x + g[1]*y; }; SimplexNoise.prototype.noise = function(xin, yin) { var n0, n1, n2; // Noise contributions from the three corners // Skew the input space to determine which simplex cell we're in var F2 = 0.5*(Math.sqrt(3.0)-1.0); var s = (xin+yin)*F2; // Hairy factor for 2D var i = Math.floor(xin+s); var j = Math.floor(yin+s); var G2 = (3.0-Math.sqrt(3.0))/6.0; var t = (i+j)*G2; var X0 = i-t; // Unskew the cell origin back to (x,y) space var Y0 = j-t; var x0 = xin-X0; // The x,y distances from the cell origin var y0 = yin-Y0; // For the 2D case, the simplex shape is an equilateral triangle. // Determine which simplex we are in. var i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords if(x0>y0) {i1=1; j1=0;} // lower triangle, XY order: (0,0)->(1,0)->(1,1) else {i1=0; j1=1;} // upper triangle, YX order: (0,0)->(0,1)->(1,1) // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where // c = (3-sqrt(3))/6 var x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords var y1 = y0 - j1 + G2; var x2 = x0 - 1.0 + 2.0 * G2; // Offsets for last corner in (x,y) unskewed coords var y2 = y0 - 1.0 + 2.0 * G2; // Work out the hashed gradient indices of the three simplex corners var ii = i & 255; var jj = j & 255; var gi0 = this.perm[ii+this.perm[jj]] % 12; var gi1 = this.perm[ii+i1+this.perm[jj+j1]] % 12; var gi2 = this.perm[ii+1+this.perm[jj+1]] % 12; // Calculate the contribution from the three corners var t0 = 0.5 - x0*x0-y0*y0; if(t0<0) n0 = 0.0; else { t0 *= t0; n0 = t0 * t0 * this.dot(this.grad3[gi0], x0, y0); // (x,y) of grad3 used for 2D gradient } var t1 = 0.5 - x1*x1-y1*y1; if(t1<0) n1 = 0.0; else { t1 *= t1; n1 = t1 * t1 * this.dot(this.grad3[gi1], x1, y1); } var t2 = 0.5 - x2*x2-y2*y2; if(t2<0) n2 = 0.0; else { t2 *= t2; n2 = t2 * t2 * this.dot(this.grad3[gi2], x2, y2); } // Add contributions from each corner to get the final noise value. // The result is scaled to return values in the interval [-1,1]. return 70.0 * (n0 + n1 + n2); }; // 3D simplex noise SimplexNoise.prototype.noise3d = function(xin, yin, zin) { var n0, n1, n2, n3; // Noise contributions from the four corners // Skew the input space to determine which simplex cell we're in var F3 = 1.0/3.0; var s = (xin+yin+zin)*F3; // Very nice and simple skew factor for 3D var i = Math.floor(xin+s); var j = Math.floor(yin+s); var k = Math.floor(zin+s); var G3 = 1.0/6.0; // Very nice and simple unskew factor, too var t = (i+j+k)*G3; var X0 = i-t; // Unskew the cell origin back to (x,y,z) space var Y0 = j-t; var Z0 = k-t; var x0 = xin-X0; // The x,y,z distances from the cell origin var y0 = yin-Y0; var z0 = zin-Z0; // For the 3D case, the simplex shape is a slightly irregular tetrahedron. // Determine which simplex we are in. var i1, j1, k1; // Offsets for second corner of simplex in (i,j,k) coords var i2, j2, k2; // Offsets for third corner of simplex in (i,j,k) coords if(x0>=y0) { if(y0>=z0) { i1=1; j1=0; k1=0; i2=1; j2=1; k2=0; } // X Y Z order else if(x0>=z0) { i1=1; j1=0; k1=0; i2=1; j2=0; k2=1; } // X Z Y order else { i1=0; j1=0; k1=1; i2=1; j2=0; k2=1; } // Z X Y order } else { // x0 y0) ? 32 : 0; var c2 = (x0 > z0) ? 16 : 0; var c3 = (y0 > z0) ? 8 : 0; var c4 = (x0 > w0) ? 4 : 0; var c5 = (y0 > w0) ? 2 : 0; var c6 = (z0 > w0) ? 1 : 0; var c = c1 + c2 + c3 + c4 + c5 + c6; var i1, j1, k1, l1; // The integer offsets for the second simplex corner var i2, j2, k2, l2; // The integer offsets for the third simplex corner var i3, j3, k3, l3; // The integer offsets for the fourth simplex corner // simplex[c] is a 4-vector with the numbers 0, 1, 2 and 3 in some order. // Many values of c will never occur, since e.g. x>y>z>w makes x=3 ? 1 : 0; j1 = simplex[c][1]>=3 ? 1 : 0; k1 = simplex[c][2]>=3 ? 1 : 0; l1 = simplex[c][3]>=3 ? 1 : 0; // The number 2 in the "simplex" array is at the second largest coordinate. i2 = simplex[c][0]>=2 ? 1 : 0; j2 = simplex[c][1]>=2 ? 1 : 0; k2 = simplex[c][2]>=2 ? 1 : 0; l2 = simplex[c][3]>=2 ? 1 : 0; // The number 1 in the "simplex" array is at the second smallest coordinate. i3 = simplex[c][0]>=1 ? 1 : 0; j3 = simplex[c][1]>=1 ? 1 : 0; k3 = simplex[c][2]>=1 ? 1 : 0; l3 = simplex[c][3]>=1 ? 1 : 0; // The fifth corner has all coordinate offsets = 1, so no need to look that up. var x1 = x0 - i1 + G4; // Offsets for second corner in (x,y,z,w) coords var y1 = y0 - j1 + G4; var z1 = z0 - k1 + G4; var w1 = w0 - l1 + G4; var x2 = x0 - i2 + 2.0*G4; // Offsets for third corner in (x,y,z,w) coords var y2 = y0 - j2 + 2.0*G4; var z2 = z0 - k2 + 2.0*G4; var w2 = w0 - l2 + 2.0*G4; var x3 = x0 - i3 + 3.0*G4; // Offsets for fourth corner in (x,y,z,w) coords var y3 = y0 - j3 + 3.0*G4; var z3 = z0 - k3 + 3.0*G4; var w3 = w0 - l3 + 3.0*G4; var x4 = x0 - 1.0 + 4.0*G4; // Offsets for last corner in (x,y,z,w) coords var y4 = y0 - 1.0 + 4.0*G4; var z4 = z0 - 1.0 + 4.0*G4; var w4 = w0 - 1.0 + 4.0*G4; // Work out the hashed gradient indices of the five simplex corners var ii = i & 255; var jj = j & 255; var kk = k & 255; var ll = l & 255; var gi0 = perm[ii+perm[jj+perm[kk+perm[ll]]]] % 32; var gi1 = perm[ii+i1+perm[jj+j1+perm[kk+k1+perm[ll+l1]]]] % 32; var gi2 = perm[ii+i2+perm[jj+j2+perm[kk+k2+perm[ll+l2]]]] % 32; var gi3 = perm[ii+i3+perm[jj+j3+perm[kk+k3+perm[ll+l3]]]] % 32; var gi4 = perm[ii+1+perm[jj+1+perm[kk+1+perm[ll+1]]]] % 32; // Calculate the contribution from the five corners var t0 = 0.6 - x0*x0 - y0*y0 - z0*z0 - w0*w0; if(t0<0) n0 = 0.0; else { t0 *= t0; n0 = t0 * t0 * this.dot(grad4[gi0], x0, y0, z0, w0); } var t1 = 0.6 - x1*x1 - y1*y1 - z1*z1 - w1*w1; if(t1<0) n1 = 0.0; else { t1 *= t1; n1 = t1 * t1 * this.dot(grad4[gi1], x1, y1, z1, w1); } var t2 = 0.6 - x2*x2 - y2*y2 - z2*z2 - w2*w2; if(t2<0) n2 = 0.0; else { t2 *= t2; n2 = t2 * t2 * this.dot(grad4[gi2], x2, y2, z2, w2); } var t3 = 0.6 - x3*x3 - y3*y3 - z3*z3 - w3*w3; if(t3<0) n3 = 0.0; else { t3 *= t3; n3 = t3 * t3 * this.dot(grad4[gi3], x3, y3, z3, w3); } var t4 = 0.6 - x4*x4 - y4*y4 - z4*z4 - w4*w4; if(t4<0) n4 = 0.0; else { t4 *= t4; n4 = t4 * t4 * this.dot(grad4[gi4], x4, y4, z4, w4); } // Sum up and scale the result to cover the range [-1,1] return 27.0 * (n0 + n1 + n2 + n3 + n4); }; /* PolyFill http://paulirish.com/2011/requestanimationframe-for-smart-animating/ */ (function() { var lastTime = 0; var vendors = ['ms', 'moz', 'webkit', 'o']; for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) { window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame']; window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame']; } if (!window.requestAnimationFrame) window.requestAnimationFrame = function(callback, element) { var currTime = new Date().getTime(); var timeToCall = Math.max(0, 16 - (currTime - lastTime)); var id = window.setTimeout(function() { callback(currTime + timeToCall); }, timeToCall); lastTime = currTime + timeToCall; return id; }; if (!window.cancelAnimationFrame) window.cancelAnimationFrame = function(id) { clearTimeout(id); }; }()); var context; var simplex = new SimplexNoise(); /* zz85 aka @blurspline */ /* CPU Based method here*/ /* For GLSL version see http://glsl.heroku.com/e#812.1 */ function createCloudTexture(width, height) { var canvas = document.createElement("canvas"); canvas.width = width; canvas.height = height; context = canvas.getContext('2d'); document.body.appendChild(canvas); this.redraw = function() { var now = Date.now(); var time = now / 8000; context.clearRect(0, 0, width, height); var gradient = context.createLinearGradient( 0, (Math.sin(time / 2)+1) * 0.5 * height, width, height - (Math.sin(time / 2)+1) * 0.5* height ); gradient.addColorStop( 0, 'rgba(200,200,0,0.9)' ); gradient.addColorStop( (Math.sin(time)+1) * 0.5 * 0.2, 'rgba(100,0,0,1)' ); gradient.addColorStop( (Math.cos(time)+1) * 0.5 * 0.2 + 0.4 , 'rgba(0,200,0,1)' ); // 0.6 gradient.addColorStop( 0.8, 'rgba(0,0,200,1)' ); gradient.addColorStop( 1, 'rgba(200,200,200,1)' ); context.fillStyle = gradient; context.fillRect(0,0, width, height); context.save(); context.globalCompositeOperation = 'lighter'; var gradient = context.createLinearGradient( 0, 0, 0, height ); gradient.addColorStop( 0, 'rgba(0,0,0,0.2)' ); gradient.addColorStop( 1, 'rgba(200,200,200,0.5)' ); context.fillStyle = gradient; context.fillRect(0,0, width, height); context.restore(); var image = context.createImageData( width, height ); var image2 = context.getImageData( 0, 0, width, height ); var imageData = image.data; var imageData2 = image2.data; var w,h, n; // settings var octaves = 2; var scaleX = 4 /octaves, scaleY = 0.25 /octaves; for ( var i = 0, j = 0, l = imageData.length; i < l; i += 4, j ++ ) { h = Math.floor( j/width ); w = j % width; n = 0; var frequency = 1; var persistance = 0.5; var amptitude ; for (var oi=0; oi < octaves; oi++) { frequency *= 2; amptitude = Math.pow(persistance, oi); n += simplex.noise3d(w/width * frequency * scaleX, h/height* frequency * scaleY, time) * amptitude ; } var m = n; var factor = n* 0.5 + 0.5; // + 1 ) * 0.5 n = Math.floor( factor * 255); //Math.floor // Multiply ** (best!!!) imageData[ i ] = Math.floor( factor * imageData2[ i ]); imageData[ i + 1 ] = Math.floor( factor * imageData2[ i + 1]); imageData[ i + 2 ] = Math.floor( factor * imageData2[ i + 2 ]); imageData[ i + 3 ] = 255; } context.putImageData( image, 0, 0 ); //console.log('done', Date.now() - now); } this.redraw(); return this; } // var canvas = createCloudTexture(800, 600) var canvas = createCloudTexture(465, 465) animate(); function animate() { requestAnimationFrame( animate ); render(); } function render() { canvas.redraw(); } body { background-color: #DDDDDD; font: 30px sans-serif; } canvas { margin: auto; } http://jsdo.it/zz85/r8vv /* Meyer Reset */ html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,font,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td{font-family: "Lucida Grande", Lucida, Verdana, sans-serif;margin:0;padding:0;border:0;outline:0;font-size:100%;vertical-align:baseline;background:transparent}body{line-height:1}ol,ul{list-style:none}blockquote,q{quotes:none}blockquote:before,blockquote:after,q:before,q:after{content:'';content:none}:focus{outline:0}ins{text-decoration:none}del{text-decoration:line-through}table{border-collapse:collapse;border-spacing:0} html { background: #000 url(https://jedidiah.eu/code/css/masks/aurora_borealis/sky.jpg) repeat-x top center; } .wavey { /* Make the hidden image fit to the full window to match the background on the html element */ position:fixed; top:0; right:0; bottom:0; left:0; opacity:0.8; /* Set the background */ background: transparent url(https://jedidiah.eu/code/css/masks/aurora_borealis/lines.jpg) repeat top left; /* Set the mask (uses the same properties as background) */ -webkit-mask-image: url(https://jedidiah.eu/code/css/masks/aurora_borealis/wavey4.png); -webkit-mask-repeat:repeat-y; -webkit-animation-name: pulsed; -webkit-animation-duration: 1000s; -webkit-animation-timing-function: linear; -webkit-animation-iteration-count: infinite; } .waveyecho { /* Make the hidden image fit to the full window to match the background on the html element */ position:fixed; top:0; right:0; bottom:0; left:0; /* Set the background */ background: transparent url(https://jedidiah.eu/code/css/masks/aurora_borealis/lines.jpg) repeat top left; opacity:0.5; /* Set the mask (uses the same properties as background) */ -webkit-mask-image: url(https://jedidiah.eu/code/css/masks/aurora_borealis/wavey3.png); -webkit-mask-repeat:repeat-y; -webkit-animation-name: pulsedecho; -webkit-animation-duration: 1000s; -webkit-animation-timing-function: linear; -webkit-animation-iteration-count: infinite; } #pulsed { /* Make the hidden image fit to the full window to match the background on the html element */ position:fixed; top:0; right:0; bottom:0; left:0; /* Set the background */ /* Set the mask (uses the same properties as background) */ -webkit-mask-image: url(https://jedidiah.eu/code/css/masks/aurora_borealis/fade.png); -webkit-mask-repeat:repeat-x; } @-webkit-keyframes pulsed { 0% { -webkit-mask-position: 0px -5000px; background-position: 1% 1%; } 50% { -webkit-mask-position: -500px 5000px; background-position: -1000% 1%; } 100% { -webkit-mask-position: 0px 15000px; background-position: -3000% 1%; } } @-webkit-keyframes pulsedecho { 0% { -webkit-mask-position: 0px 20000px; background-position: -1000% 1%; } 50% { -webkit-mask-position: -500px 15000px; background-position: 0% 1%; } 100% { -webkit-mask-position: 0px 0px; background-position: -1000% 1%; } }
https://jedidiah.eu/code/css/masks/aurora_borealis/ При желании, можно найти ещё.

Комментариев нет:

Отправить комментарий