Tab-Alerts with mootools

Ever wanted to catch a users attention or provide them with important information but there away browsing Facebook or Twitter. Well Here is a simple solution with tab alerts.

Space carousel

View Demo

Eh! what does it do again?

Well quite simply it updates the web-pages title attribute when a user is inactive or browsing in another tab. When the user returns the title returns to the original state.

Basic Usage

This demo changes the title to show the user they have a new message in their inbox.

     var myVar = new tabAlert({
          text: 'You have one new message.'
     });

More Advanced Usage

This demo shows a goal alert.

     var myVar = new tabAlert({
          text: 'GOAL FLASH: Manchester United 0:1 LEEDS UNITED (Beckford 19',
          ticker: true,
          speed: 200,
          onLoop: function(){
             checkForMoreGoals();
          }
     });

My first Mootools plugin

This is my first MooTools plugin. I don’t usually get to time to write one (The process of submitting your first takes forever)! But it’s up and you can download it here.

CSS3 Pullout Menu

This demo is for a folding pull-out menu that uses CSS3 elements and JavaScript only. It uses no images or flash to produce the effect although they could be used to enhance the natural feel.

Space carousel

View Demo

The Idea

I’ve seen this effect used before but most recently in a tech demo for Microsoft’s “Courier”. Below is a snippet of how this menu system works:
Pullout Menu Design
My Idea was to build this using DHTML only. No Flash, Canvas or SVG but pure HTML, CSS and JavaScript.

The Basis

The Pullout menu comprises of five HTML elements a,b,c,d and a handle. Elements a,b,c,d are positioned and then skewed to give the effect of a folding object.
folding object
When the handle is pulled outwards the width of the elements is stretched and the skew becomes less. This and some colouring effects in JavaScript give the effect of a fold.

Final Effects

I added a simple “on complete” event that detects when the menu is fully opened. The event can be used to trigger any JavaScript you like. I also tinkered with a revert animation that was triggered if the menu was released without being fully open, although this was cool it made it impossible to view the code frame-by-frame in firebug.

Folding Improvements

The animation I used doesn’t seem natural to me, this was down to my lack of understanding in perspective drawings and transitions. Im sure someone with a better understanding could make a much better example from this concept. Maybe if images, gradients and shadows were introduced the effect would start to feel more natural.

Finally

If anyone does improve the menu could you let me know. I’d be really interested to see it.

Flipping Crazy – CSS3

This is a pseudo way to flip HTML elements on their backs. This is a way of doing it now without waiting for a possible introduction of 3D Transforms from webkit.

Space carousel

View Demo

About Flipping

I first used flipping a few years ago when building touch screen HTML applications for kiosks. The animation would basically make the elements width 0 whilst moving the elements position right. This would give the effect that the element had been “sort of” flipped. It was a very simple solution and would only work if the animation was fast. Tricking the human eye into thinking it was actually flipped.

So what’s changed

Well firstly CSS3 allows you to skew, not screw! but skew. Skew allows you to give a simple pseudo 3D effect. It’s not ideal for this example but until 3D Transforms come in, it greatly adds to the effect of tricking the eye into thinking the element has been flipped.

The Process

The animation works in three steps:

The first step goes from: [width: curWidth, left: 0, skew: 0deg 0deg] to: [width: 0, left: initialWidth/2, skew: 0deg 20deg]. This gives the effect that the element has been flipped like a playing card onto its thinnest edge, essentially you cannot see it at this stage.

The second step triggers any javaScript you want to happen. In my examples I add a class to the element called “toggleTrue” which can be used to change the CSS properties of the box. Potentially videos, images or text could be inserted creating a sort of flipping lightbox.

The third and final step animates the element from it’s side to it’s back-facing plane. The animation goes from: [width: 0, left: initialWidth/2, skew: 0deg -20deg] to: [width: initialWidth, left: 0, skew: 0deg 0deg]. Notice the skew changes from step one’s finishing position to a negative value, without doing this it looks as if the card has gone 90deg and back again.

Flipping an element with background-image set:

When I initially did this a few years ago I would use an image tag and just flip that. This was because changing the width of an image stretches the image and using background-image wouldn’t do that, until now that is. By using the CSS3 property “background-size” I can set the width to 100%, this makes the background-image stretch as the container does.

Why not use CSS3 animation in webkit?

I did experiment with CSS3 animations and failed quite quickly. The problem i found was the absence of a javaScript callback method. There might be one but I couldn’t work out how and without a callback, CSS animations seem quite useless to me.

I also tried using CSS3 3D transforms. This looks much better than skew because the element is actually flipped – No moving the elements position, no tricks of the eye, no fakery! The problem was that JavaScript manipulation of these CSS3 3D transforms seemed to create some crazy artefacts in the picture.

Finally

Take a look at the code in firebug and see how it works. If you know how to use firebug’s console try setting the effects speed using “effectSpeed = 1000;”. This slows the animation down. Please leave your comments and thoughts below.

Fun with CSS3 and mootools

These examples came about when experimenting with the extend property in MooTools. By extending the styles class I could add CSS3 properties into the Core MooTools framework and do CSS3 animations.

Space carousel

View Demo

The Extend Code

Bellow is the Mozilla code to extend MooTools with the Border-Radius and Box-Shadow:

var newStyles = new Hash({
     'MozBorderRadius': '@px @px @px @px',
     'MozBoxShadow': '@px @px @px rgb(@, @, @)'
});

$extend(Element.Styles, newStyles);

The WebKit code is slightly different. For some reason i couldn’t get the webkitBorderRadius to work without doing each corner individually. For more info on the differences read this

var newStyles = new Hash({
     'webkitBorderBottomLeftRadius': '@px @px',
     'webkitBorderBottomRightRadius': '@px @px',
     'webkitBorderTopLeftRadius': '@px @px',
     'webkitBorderTopRightRadius': '@px @px',
     'webkitBoxShadow': 'rgb(@, @, @) @px @px @px'
});

$extend(Element.Styles, newStyles);

The CSS3 Animation

For the animations we can now simply use the same MooTools FX and we would use with CSS2. Below is an example for FireFox:

     $("h31").set('morph', {
          duration: 300, 
          transition: 'Sine:out'
     });

     $("h31").addEvents({
          'mouseover': function(){
	       this.morph({
                    'MozBorderRadius': '8px 8px 30px 30px'
               }); 
	   },
          'mouseout': function(){
               this.morph({
                    'MozBorderRadius': '30px 30px 8px 8px'
               });
	   }
       });

And that’s it really. Very simple to set up and the animations are smooth and stylish. The example will work on any WebKit browser including Chrome and Safari and Mozilla browsers including FireFox.

rotate360 (Image rotation in javascript)

This is an example of image rotation and control using CSS sprites and JavaScript. The example i went for is based on a product demo by apple for the Ipod touch. There example uses Quicktime but i thought is was possible in JavaScript so i gave it a go.

Space carousel

View Demo

Step 1

First i download the QuickTime file opened it and exported it as JPEG images at 1 image per second. I think this produced about 72 images at 40k each.

Step 2 – FAIL!

In my first attempt i tried to show all 72 images positioned on-top of one another. When i needed a specific image I would just change the z-index. The method stank and the results were terribly slow.

Step 3 – RSI!

My next idea was to use it as a CSS sprite. I had no idea this would work but set out adding all 72 images into one very large file. I even thought of writing an app just to do this for me as it may have taken less time. Once done the image dimensions were 880px by 33840px – The problem with this is I couldn’t save it as JPEG/png/gif only TIFF which isnt natively supported in browsers. So… I did some research and found out the height must be less than 30000px to be a JPEG. Once resized and saved i was ready to code.

Step 4 – code!

The coding was simple really: i just change the background position on mouse-move and the animation is done. Take alook at the code on the demo, it’s really easy. I added a few options just in case your using a slow browser or your CPU is slow.

Step 5 – Future work

I really like the effect and might see if speed improvements can be made in canvas. I might also invest in a tri-pod and lightbox and take some stop-motion pictures of my own. I have done a few websites where this would have been great. Any further suggestions please leave them below.

Spacegallery – mooStyle

I was combing the JavaScript framework blogs and came across this image carousel in JQuery: SpaceGallery. I used this same concept in my old website but it was written in mootools. The jQuery plug-in feels unresponsive and slow and doesn’t have any features. So here is the mootools version I wrote, sorry it is not as a plugin but feel free to make it into one.

Space carousel

View Demo

Things you need

First get Mootools (This demo uses 1.2.4) and if you want the color plugin I use in the demo you can download this from mootools more.

The JavaScript

Sorry about the mess of this: I haven’t written it to be dynamic as it was rushed out late at night. The positioning of elements is hard-coded but would be simple to adapt if i could be bothered to comment anything. So here it is:

var spacers = "";

function loadDemo(){
     spacers = $("spaceMan").getElements(".spaceOB");
     intSpace();
}

function intSpace(){
     spacers.addEvents({
          'mouseover': function(){
               if(this.getStyle("z-index") != 20){
                    this.setStyle("top", parseInt(this.getStyle("top"))-2);
               }
          },
          'mouseout': function(){
                if(this.getStyle("z-index") != 20){
                     this.setStyle("top", parseInt(this.getStyle("top"))+2);
                }
          },
          'click': function(){
               doSpace(this);
          }
     });

     spacers.each(function(item, index){
          var color = [index*40, 82, 87].hsbToRgb();
          spacers[index].setStyles({
               "z-index": 20-index,
               "background-color": color
          });
          spacers[index].set(
               'morph',
               {
                    duration: 1000-(index*100),
                    transition: 'Sine:out'
               }
          );

          spacers[index].morph(
          {
                opacity: [0, (100-(index*10))/100],
                width: 323-(index*32.3),
                height: 203-(index*20.3),
                left: 60+((index*23.5)/1.5),
                top: 80-(index*(15-(index)))
          });
     });
     setTimeout(function(){$("spaceHelp").fade([0,1]);}, 1000);
}

function doSpace(el){
     $("spaceHelp").fade(0);
     var positionOfClicked = 20 - el.getStyle("z-index");
     var topPos = 0;
     spacers.each(function(item, index){
          var zIndexi = parseInt(spacers[index].getStyle("z-index"));
          if(zIndexi == 20){
               spacers[index].setStyle("z-index", 20-(spacers.length-1));
               spacers[index].set('morph', {
                    duration: '500',
                    transition: 'Sine:out',
                    onComplete: function(){
                         spacers[index].set(
                              'morph',
                              {
                                   duration: '500',
                                   transition: 'Sine:out'
                              }
                         );
                         spacers[index].morph({
                              opacity: [0, 0.5],
                              width: [94,161.5],
                              height: [50, 62.5],
                              left: [170, 138],
                              top: [40, 30]
                         });
               }});
               spacers[index].morph({
                    opacity: 0,
                    width: 400,
                    height: 220,
                    left: 25,
                    top: 110
               });
          }
          else{
               var thisPos = 20-(zIndexi+1);
               spacers[index].setStyle("z-index", zIndexi+1);
               spacers[index].set(
                    'morph',
                    {
                         duration: '500',
                         transition: 'Sine:out'
                    });
                spacers[index].morph({
                     opacity: (100-(thisPos*10))/100,
                     width: 323-(thisPos*32.3),
                     height: 203-(thisPos*20.3),
                     left: 60+((thisPos*23.5)/1.5),
                     top: 80-(thisPos*(15-(thisPos)))
                });
          }
     });
     if(positionOfClicked == 0){
          return false;
     }
     if(positionOfClicked != 1){
          setTimeout(function(){doSpace(el)}, 125);
     }
}

The CSS and HTML

The css:

 #spaceManHolder{
   	width: 450px;
 	height: 500px;
 	margin: 40px auto 0px auto;
 	position: relative;
 }
 #spaceMan{
  	width: 450px;
 	height: 300px;
  	position: relative;
 }

 #spaceHelp{
 	font-size: 10px;
 	color: #fff;
 	text-align: center;
 	opacity: 0;
 }

.spaceOB{
	position: absolute;
	width: 100px;
	height: 100px;
	z-index: 100;
}

and the html:

<div id="spaceManHolder">
     <div id="spaceMan">
          <div class="spaceOB"></div>
          <div class="spaceOB"></div>
          <div class="spaceOB"></div>
          <div class="spaceOB"></div>
          <div class="spaceOB"></div>
          <div class="spaceOB"></div>
     </div>
     <div id="spaceHelp">
          Click The front block or try selecting
          a block from the back
     </div>
</div>

If you would like to use or add to this example please do:)