Перейти к содержимому страницы
Переключить меню
Переключить меню настроек
Переключить персональное меню
Вы не представились системе
Ваш IP-адрес будет виден всем, если вы внесёте какие-либо изменения.

MediaWiki:Common.js

Страница интерфейса MediaWiki

Замечание: Возможно, после публикации вам придётся очистить кэш своего браузера, чтобы увидеть изменения.

  • Firefox / Safari: Удерживая клавишу Shift, нажмите на панели инструментов Обновить либо нажмите Ctrl+F5 или Ctrl+R (⌘+R на Mac)
  • Google Chrome: Нажмите Ctrl+Shift+R (⌘+Shift+R на Mac)
  • Edge: Удерживая Ctrl, нажмите Обновить либо нажмите Ctrl+F5
  • Opera: Нажмите Ctrl+F5.
/* Senrai Wiki — animated space background (WebGL).
 * Nebula from domain-warped fbm + three parallax star layers with twinkle.
 * Renders at native resolution (adaptive), pauses in hidden tabs; the
 * page falls back to the CSS background without WebGL.
 */
( function () {
	'use strict';
	if ( window.senraiSpace ) { return; }
	window.senraiSpace = true;

	var VERT = 'attribute vec2 p;void main(){gl_Position=vec4(p,0.,1.);}';
	var FRAG = [
		'#ifdef GL_FRAGMENT_PRECISION_HIGH',
		'precision highp float;',
		'#else',
		'precision mediump float;',
		'#endif',
		'uniform vec2 r;uniform float t;uniform vec2 m;',
		'float h(vec2 p){p=fract(p*vec2(123.34,456.21));p+=dot(p,p+45.32);return fract(p.x*p.y);}',
		'float n(vec2 p){vec2 i=floor(p),f=fract(p);f=f*f*(3.-2.*f);',
		' return mix(mix(h(i),h(i+vec2(1,0)),f.x),mix(h(i+vec2(0,1)),h(i+vec2(1,1)),f.x),f.y);}',
		'float fbm(vec2 p){float v=0.,a=.5;mat2 R=mat2(.8,.6,-.6,.8);',
		' for(int i=0;i<5;i++){v+=a*n(p);p=R*p*2.02+3.1;a*=.5;}return v;}',
		'float stars(vec2 uv,float sc,float th,float sp){',
		' vec2 g=uv*sc;vec2 id=floor(g);vec2 f=fract(g)-.5;float s=h(id);',
		' if(s<th)return 0.;vec2 o=vec2(h(id+7.1),h(id+3.7))-.5;',
		' float d=length(f-o*.7);float tw=.55+.45*sin(t*sp*(1.+s*3.)+s*40.);',
		/* crisp point: size in pixels so stars stay sharp at any resolution */
		' float px=sc/r.y*1.4;',
		' return smoothstep(px*2.2,px*.4,d)*tw*(s-th)/(1.-th)*2.4;}',
		'void main(){',
		' vec2 uv=(gl_FragCoord.xy-.5*r)/r.y;',
		' vec2 par=m*.02;',
		/* slow rotation of the whole sky + visible drift and swirl */
		' float a=t*.004;mat2 rot=mat2(cos(a),-sin(a),sin(a),cos(a));vec2 ru=rot*uv;',
		' vec2 q=ru*1.5+vec2(t*.018,t*.011)+par*.4;',
		' vec2 w=vec2(fbm(q+vec2(0.,t*.035)),fbm(q+vec2(5.2,1.3)-t*.03));',
		' float neb=fbm(q+2.4*w);',
		' float neb2=fbm(q*2.2-1.8*w+vec2(t*.025,-t*.01));',
		/* far layer: a fainter, slower nebula behind the main one gives depth */
		' vec2 qf=ru*.8-vec2(t*.006,t*.004)+par*.15;',
		' float far=fbm(qf+1.5*vec2(fbm(qf+3.3),fbm(qf-7.1+t*.01)));',
		' vec3 col=vec3(.002,.003,.010);',
		' col+=vec3(.035,.025,.11)*smoothstep(.30,.95,far);',
		' float band=exp(-pow((ru.y+ru.x*.42+.05)*2.3+(neb-.5)*1.4,2.));',
		' col+=vec3(.03,.045,.15)*band;',
		' float nb=smoothstep(.40,.92,neb);nb*=nb;',
		' float vi=smoothstep(.40,.90,neb2)*smoothstep(.25,.80,neb);vi*=sqrt(vi);',
		' col+=vec3(.05,.12,.48)*nb*(.45+.55*band);',
		' col+=vec3(.30,.08,.52)*vi*(.6+.4*band);',
		' col+=vec3(.06,.32,.60)*pow(smoothstep(.62,1.,neb),3.)*.7;',
		' col+=vec3(.45,.12,.40)*pow(smoothstep(.62,1.,neb2*neb*1.6),4.)*.35;',
		/* dark dust lanes */
		' col*=1.-.7*smoothstep(.5,.85,fbm(q*3.1+w*1.3+vec2(9.,2.)))*band;',
		' float vg=1.-.65*dot(uv*.72,uv*.72);col*=max(vg,0.);',
		' vec3 sc=vec3(0.);',
		' sc+=stars(ru+par*.3+vec2(t*.003,0.),130.,.968,1.3)*vec3(.70,.76,1.)*(.25+1.1*band);',
		' sc+=stars(ru+par*.7+vec2(t*.006,0.),60.,.98,1.8)*vec3(1.,.88,1.)*.9;',
		' sc+=stars(ru+par*1.4+vec2(t*.012,0.),24.,.985,2.4)*vec3(.8,.9,1.)*1.4;',
		/* a few bright stars with cross flares */
		' vec2 g=(ru+par*2.+vec2(t*.015,0.))*6.;vec2 id=floor(g);float s=h(id+11.3);',
		/* the glow must fade to zero inside its grid cell, otherwise the cell edge shows as a faint square */
		' if(s>.88){vec2 c=fract(g)-.5;vec2 f=c-(vec2(h(id+1.7),h(id+5.9))-.5)*.3;',
		'  float tw=.55+.45*sin(t*(1.5+s*2.)+s*50.);',
		'  float win=smoothstep(.5,.32,max(abs(c.x),abs(c.y)))*smoothstep(.3,.0,length(f));',
		'  float core=max(.0012/dot(f,f)-.0012/.09,0.);float fl=.0005/(abs(f.x*f.y)+.0003);',
		'  sc+=(core+fl*.4)*win*tw*vec3(.75,.82,1.)*(s-.88)*7.;}',
		' col+=sc;',
		' col=1.-exp(-col*1.6);',
		' col=pow(col,vec3(1.08));',
		/* dithering removes banding in the dark gradients */
		' col+=(h(gl_FragCoord.xy+fract(t))-.5)/255.;',
		' gl_FragColor=vec4(col,1.);',
		'}'
	].join( '\n' );

	function start() {
		var cv = document.createElement( 'canvas' );
		cv.id = 'senrai-space';
		cv.setAttribute( 'aria-hidden', 'true' );
		document.body.insertBefore( cv, document.body.firstChild );
		var gl = cv.getContext( 'webgl', { alpha: false, antialias: false, powerPreference: 'low-power' } );
		if ( !gl ) { cv.remove(); return; }

		function sh( type, src ) {
			var s = gl.createShader( type );
			gl.shaderSource( s, src );
			gl.compileShader( s );
			return gl.getShaderParameter( s, gl.COMPILE_STATUS ) ? s : null;
		}
		var vs = sh( gl.VERTEX_SHADER, VERT ), fs = sh( gl.FRAGMENT_SHADER, FRAG );
		if ( !vs || !fs ) { cv.remove(); return; }
		var pr = gl.createProgram();
		gl.attachShader( pr, vs );
		gl.attachShader( pr, fs );
		gl.linkProgram( pr );
		if ( !gl.getProgramParameter( pr, gl.LINK_STATUS ) ) { cv.remove(); return; }
		gl.useProgram( pr );
		gl.bindBuffer( gl.ARRAY_BUFFER, gl.createBuffer() );
		gl.bufferData( gl.ARRAY_BUFFER, new Float32Array( [ -1, -1, 3, -1, -1, 3 ] ), gl.STATIC_DRAW );
		var loc = gl.getAttribLocation( pr, 'p' );
		gl.enableVertexAttribArray( loc );
		gl.vertexAttribPointer( loc, 2, gl.FLOAT, false, 0, 0 );
		var uR = gl.getUniformLocation( pr, 'r' ), uT = gl.getUniformLocation( pr, 't' ), uM = gl.getUniformLocation( pr, 'm' );

		// native resolution (capped for 4K/retina); drops automatically if the GPU can't keep up
		var SCALE = Math.min( window.devicePixelRatio || 1, 1.5 ), slow = 0, lastT = 0;
		function resize() {
			var w = Math.max( 1, Math.floor( window.innerWidth * SCALE ) ),
				h = Math.max( 1, Math.floor( window.innerHeight * SCALE ) );
			if ( cv.width !== w || cv.height !== h ) {
				cv.width = w;
				cv.height = h;
				gl.viewport( 0, 0, w, h );
			}
		}
		var mx = 0, my = 0, tmx = 0, tmy = 0;
		window.addEventListener( 'mousemove', function ( e ) {
			tmx = ( e.clientX / window.innerWidth - 0.5 ) * 2;
			tmy = ( 0.5 - e.clientY / window.innerHeight ) * 2;
		}, { passive: true } );

		var t0 = performance.now(), running = false;
		function frame( now ) {
			if ( lastT ) {
				slow = ( now - lastT > 28 ) ? slow + 1 : Math.max( 0, slow - 1 );
				if ( slow > 90 && SCALE > 0.6 ) { SCALE *= 0.8; slow = 0; }
			}
			lastT = now;
			resize();
			mx += ( tmx - mx ) * 0.03;
			my += ( tmy - my ) * 0.03;
			gl.uniform2f( uR, cv.width, cv.height );
			gl.uniform1f( uT, ( now - t0 ) / 1000 + 40 );
			gl.uniform2f( uM, mx, my );
			gl.drawArrays( gl.TRIANGLES, 0, 3 );
			if ( running ) { requestAnimationFrame( frame ); }
		}
		function play() {
			if ( running ) { return; }
			running = true;
			lastT = 0;
			requestAnimationFrame( frame );
		}
		document.addEventListener( 'visibilitychange', function () {
			if ( document.hidden ) { running = false; } else { play(); }
		} );
		window.addEventListener( 'resize', function () { if ( !running ) { frame( performance.now() ); } } );
		document.documentElement.classList.add( 'senrai-space-on' );
		frame( performance.now() );
		play();
	}

	/* Boosty button in the Citizen header rail, right after search and the menu */
	function boosty() {
		var drawer = document.querySelector( '.citizen-header .citizen-drawer' );
		if ( !drawer || document.querySelector( '.senrai-boosty' ) ) { return; }
		var item = document.createElement( 'div' );
		item.className = 'citizen-header__item senrai-boosty';
		item.innerHTML =
			'<a class="citizen-cdx-button--size-large cdx-button cdx-button--fake-button cdx-button--fake-button--enabled' +
			' cdx-button--icon-only cdx-button--weight-quiet" href="https://boosty.to/senrai" target="_blank"' +
			' rel="noopener" title="Поддержать сервер на Boosty" aria-label="Boosty">' +
			'<img src="' + mw.config.get( 'wgScriptPath' ) + '/resources/assets/senrai/boosty.png" width="17" height="20" alt=""></a>';
		drawer.parentNode.insertBefore( item, drawer.nextSibling );
	}

	if ( document.readyState === 'loading' ) {
		document.addEventListener( 'DOMContentLoaded', start );
		document.addEventListener( 'DOMContentLoaded', boosty );
	} else {
		start();
		boosty();
	}
}() );