Difference between revisions of "Widget:Logo"
(created initial logo widget) |
m (fixed JS copy-paste error, removed category) |
||
Line 27: | Line 27: | ||
== Copy to your site == | == Copy to your site == | ||
To use this widget on your site, just install [http://www.mediawiki.org/wiki/Extension:Widgets MediaWiki Widgets extension] and copy [{{fullurl:{{FULLPAGENAME}}|action=edit}} full source code] of this page to your wiki as '''{{FULLPAGENAME}}''' article. | To use this widget on your site, just install [http://www.mediawiki.org/wiki/Extension:Widgets MediaWiki Widgets extension] and copy [{{fullurl:{{FULLPAGENAME}}|action=edit}} full source code] of this page to your wiki as '''{{FULLPAGENAME}}''' article. | ||
− | + | ||
</noinclude><includeonly><script type="text/javascript"> | </noinclude><includeonly><script type="text/javascript"> | ||
(function( ) | (function( ) | ||
Line 33: | Line 33: | ||
"use strict"; | "use strict"; | ||
− | + | var start = null; | |
+ | var oldTimestamp = null; | ||
+ | var maxFlakes = 200; | ||
+ | var snowFlakes = []; | ||
+ | var dynamicImage = null; | ||
+ | var width = null; | ||
+ | var height = null; | ||
+ | var ctx = null; | ||
+ | var offsetY = 50; | ||
+ | var alphaDir = 0.01; | ||
+ | var alpha = 0; | ||
− | + | function blink() | |
− | + | { | |
− | + | treeLights[ nToggle ].on = !treeLights[ nToggle ].on; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | nToggle++; | |
− | + | if ( nToggle >= treeLights.length ) | |
− | + | nToggle = 0; | |
− | + | } | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | var nToggle = 0; | |
− | + | var treeLights = [ | |
− | + | { "x" : 84, "y": 16, "color" : "red", "on" : false }, | |
+ | { "x" : 101, "y": 40, "color" : "orange", "on" : false }, | ||
+ | { "x" : 87, "y": 60, "color" : "pink", "on" : false }, | ||
+ | { "x" : 64, "y": 79, "color" : "#36f", "on" : false }, | ||
+ | { "x" : 119, "y": 91, "color" : "yellow", "on" : false } | ||
+ | ]; | ||
− | + | var oldTimestamp = null; | |
+ | function snowFall( _timestamp ) | ||
{ | { | ||
− | // | + | // Smooth progress |
− | + | if ( !start ) | |
− | var | + | start = _timestamp; |
+ | var progress = _timestamp - start; | ||
− | + | if ( ( progress / 500 ) > snowFlakes.length && ( _timestamp - oldTimestamp < 50 ) ) | |
+ | snowFlakes.push( { "x" : ( Math.random( ) * width ) | 0, "y" : 0 } ); | ||
oldTimestamp = _timestamp; | oldTimestamp = _timestamp; | ||
− | + | if ( snowFlakes.length ) | |
{ | { | ||
− | // | + | // Fetch a random flake |
− | + | var pixelPos; | |
− | + | var nFlakeNot = ( Math.random( ) * snowFlakes.length ) | 0; | |
+ | |||
+ | var delta = _timestamp - oldTimestamp; | ||
+ | oldTimestamp = _timestamp; | ||
− | var | + | for ( var nFlake = 0; nFlake < snowFlakes.length; nFlake++ ) |
− | |||
− | |||
− | |||
{ | { | ||
− | // Fixate flake | + | // Skip some flakes |
− | + | if ( Math.random() > 0.8 ) | |
− | + | continue; | |
− | + | ||
− | + | var flake = snowFlakes[ nFlake ]; | |
− | + | // Calculate the new position of the flake. | |
+ | // If it can't move anywhere down (3 positions): fixate it in the dynamic image and reset the flake | ||
+ | if ( !updateFlake( flake, dynamicImage ) ) | ||
+ | { | ||
+ | // Fixate flake | ||
+ | pixelPos = ( flake.x + width * flake.y ) * 4; | ||
+ | dynamicImage.data[ pixelPos + 0 ] = 255; | ||
+ | dynamicImage.data[ pixelPos + 1 ] = 255; | ||
+ | dynamicImage.data[ pixelPos + 2 ] = 255; | ||
+ | dynamicImage.data[ pixelPos + 3 ] = 255; | ||
+ | |||
+ | // Generate new flake | ||
+ | flake = { "x" : ( Math.random( ) * width ) | 0, "y" : 0 }; | ||
+ | } | ||
+ | |||
+ | // Store the updated or newly created flake. | ||
+ | snowFlakes[ nFlake ] = flake; | ||
− | // | + | // Draw the dynamic image |
− | + | ctx.putImageData( dynamicImage, 0, 0 ); | |
} | } | ||
− | // | + | // Draw all flakes |
− | snowFlakes[ nFlake ] | + | ctx.fillStyle = 'white'; |
+ | ctx.shadowBlur = 0; | ||
+ | |||
+ | for ( nFlake = 0; nFlake < snowFlakes.length; nFlake++ ) | ||
+ | { | ||
+ | flake = snowFlakes[ nFlake ]; | ||
+ | ctx.fillRect( flake.x, flake.y, 1, 1 ); | ||
+ | } | ||
− | // Draw | + | // Draw all tree lights |
− | + | for ( var t = 0; t < treeLights.length; t++ ) | |
− | + | drawTreeLight( treeLights[t], false ); | |
− | + | // Draw the glowing ACK bit | |
− | + | ctx.strokeStyle = ctx.shadowColor = 'red'; | |
− | + | ctx.globalAlpha = 1; | |
+ | ctx.lineWidth = 10; | ||
+ | ctx.shadowBlur = 70; | ||
+ | for ( var n = 0; n < 10; n++ ) | ||
+ | { | ||
+ | ctx.beginPath(); | ||
+ | ctx.rect( 292, 92 + offsetY, 1, 1 ); | ||
+ | ctx.stroke(); | ||
+ | } | ||
− | + | // Draw all tree lights overlay: either on or off | |
− | + | ctx.shadowBlur = 0; | |
− | + | for ( var t = 0; t < treeLights.length; t++ ) | |
− | + | drawTreeLight( treeLights[t], true ); | |
} | } | ||
− | + | window.requestAnimationFrame( snowFall ); | |
− | + | } | |
− | + | ||
+ | function drawTreeLight( _treeLight, _glow ) | ||
+ | { | ||
+ | ctx.strokeStyle = ctx.shadowColor = _treeLight.color; | ||
− | + | if ( !_glow ) | |
− | |||
− | |||
− | |||
− | |||
− | |||
{ | { | ||
+ | // Draw the light | ||
+ | ctx.lineWidth = 3; | ||
+ | ctx.globalAlpha = 1; | ||
ctx.beginPath(); | ctx.beginPath(); | ||
− | ctx. | + | ctx.arc( _treeLight.x, _treeLight.y + offsetY, 2, 0, 2 * Math.PI ); |
ctx.stroke(); | ctx.stroke(); | ||
} | } | ||
− | + | else if ( _treeLight.on ) | |
− | // | + | { |
− | + | // Let it glow! | |
− | + | ctx.lineWidth = 1; | |
− | + | for ( var n = 0; n < 20; n++ ) | |
− | + | { | |
− | + | ctx.globalAlpha = n / 30; | |
− | + | ctx.beginPath(); | |
− | + | ctx.arc( _treeLight.x, _treeLight.y + offsetY, 22 - n, 0, 2 * Math.PI ); | |
− | + | ctx.stroke(); | |
− | + | } | |
− | { | + | } |
− | + | else | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
{ | { | ||
− | ctx.globalAlpha = | + | // Add dark 'off' mask |
+ | ctx.strokeStyle = ctx.shadowColor = "rgba( 0,0,0,0.3)"; | ||
+ | ctx.lineWidth = 3; | ||
+ | ctx.globalAlpha = 1; | ||
ctx.beginPath(); | ctx.beginPath(); | ||
− | ctx.arc( _treeLight.x, _treeLight.y + offsetY, | + | ctx.arc( _treeLight.x, _treeLight.y + offsetY, 2, 0, 2 * Math.PI ); |
ctx.stroke(); | ctx.stroke(); | ||
} | } | ||
} | } | ||
− | + | ||
+ | function updateFlake( _flake, _dynamicImage ) | ||
{ | { | ||
− | + | var newFlake; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | // Check each next possible 'y' pixel, and add it to the list of possibilities | |
− | + | var possibilities = []; | |
− | |||
− | + | // Try three flake possibilities | |
− | + | for ( var n = -1; n <= 1; n++ ) | |
+ | { | ||
+ | newFlake = tryCreateFlake( _flake, n, 1, _dynamicImage ); | ||
+ | if ( newFlake ) | ||
+ | possibilities.push( newFlake ); | ||
+ | } | ||
− | + | // No possibilities means, no update: will fixate this flake | |
− | + | if ( !possibilities.length ) | |
− | + | return false; | |
− | |||
− | if ( | ||
− | |||
− | |||
− | + | // Pick a flake | |
− | + | var newFlake = possibilities[ ( Math.random( ) * possibilities.length ) | 0 ]; | |
− | + | _flake.x = newFlake.x; | |
+ | _flake.y = newFlake.y; | ||
− | + | // Flake updated | |
− | + | return true; | |
− | + | } | |
− | |||
− | // | + | function tryCreateFlake( _flake, _x, _y, _dynamicImage ) |
− | + | { | |
− | + | // Don't create flake if we're off the bottom of the screen | |
+ | if ( ( _flake.y + _y ) >= height ) | ||
+ | return null; | ||
− | + | // Don't allow to go off the sides of the screen | |
− | + | if ( ( _flake.x + _x ) < 0 ) | |
− | + | return null; | |
− | |||
− | |||
− | + | if ( ( _flake.x + _x ) >= width ) | |
− | + | return null; | |
− | |||
− | + | var newFlake = { "x" : _flake.x + _x, "y" : _flake.y + _y }; | |
− | |||
− | + | // Check the alpha channel (apparently, our logo has opacity < 250) | |
+ | if ( _dynamicImage.data[ ( newFlake.x + width * newFlake.y ) * 4 + 3 ] > 220 ) | ||
+ | return null; | ||
− | + | // All test cases valid, return the resulting flake | |
− | + | return newFlake; | |
− | + | } | |
− | |||
− | |||
− | |||
− | } | ||
var logo = document.getElementById( "logo" ); | var logo = document.getElementById( "logo" ); |
Revision as of 11:12, 17 December 2015
This widget creates an animated themed ACKspace logo.
Created by xopr
Using this widget
To insert this widget, use the following code:
{{#widget:Logo |image=https://ackspace.nl/w/images/thumb/8/83/ACKlogo.png/600px-ACKlogo.png |width=260px |height=20px |padding=8px |float=right }}
This will give the following result:
Note that image is mandatory, the rest is optional. Also, you must provide a unit for the sizes (i.e. px, %, etc.)
Copy to your site
To use this widget on your site, just install MediaWiki Widgets extension and copy full source code of this page to your wiki as Widget:Logo article.