Aug 30

Via Phil Franks comes an interesting HTML5/CSS3 site for There Studio, which is a kind of coworking space in London:

The site itself has a number of circles with information bouncing on the screen that respond to mouse clicks and moves.

Let’s crack the site open using View Source and see how they are doing things. First, they have a repeated background with a little plus symbol with the following style rule on the <body> tag:

CSS:

background: #ddd url(‘../images/bg.gif’) 50% 0 repeat fixed;
 

The textual content in each of the circles is clean semantic HTML that is search engine friendly:

HTML:

<div class=“section who first”>
  <h3>Who</h3>
  <p>Creatives, makers, thinkers <span class=“ampersand”>&amp;</span> doers</p>
</div>

To turn that into this:

The <h3> is first transformed into having an underline with the padding and margin being on the bottom:

CSS:

h3 {
        border-bottom: 1px solid #ccc;
        display: block;
        font-size: 25px;
        font-weight: normal;
        padding: 0 0 10px;
        margin: 0 0 8px;
}
 

JavaScript creates the circle. The script tags themselves are at the end of the HTML page at the bottom of the <body> tag, a good performance practice in general.

The heart of drawing each circle is in the createBall and createContentBall methods. If a ball will have HTML content, then the createContentBall method is used, otherwise the createBall method is used. Let’s look at the createContentBall method; we’ll break it down:

JAVASCRIPT:

function createContentBall(className,size,color,html) {
  var element = document.createElement( ‘div’ );
  element.className = className;
  element.width = element.height = size;
  element.style.position = ‘absolute’;
  element.style.left = -size + ‘px’;
  element.style.top = -size + ‘px’;
  element.style.cursor = “default”;
  canvas.appendChild(element);
  elements.push( element );
  var circle = document.createElement( ‘canvas’ );
  circle.width = size;
  circle.height = size;
  if (className !==‘image’ && className !==‘image first’) {
    var graphics = circle.getContext( ’2d’ );
    graphics.fillStyle = color;
    graphics.beginPath();
    graphics.arc( size * .5, size * .5, size * .5, 0, PI2, true );
    graphics.closePath();
    graphics.fill();
  }
  element.appendChild( circle );
  content = document.createElement( ‘div’ );
  content.className = “content”;
  content.onSelectStart = null;
  content.innerHTML = html;
  element.appendChild(content);
  if (className !==‘image’ && className !==‘image first’ ) {
    content.style.width = (size – contentPadding*2) + ‘px’;
    content.style.left = (((size – content.clientWidth) / 2)) +‘px’;
    content.style.top = ((size – content.clientHeight) / 2) +‘px’;
  }
  var b2body = new b2BodyDef();
  var circle = new b2CircleDef();
  circle.radius = size / 2;
  circle.density = ballDensity;
  circle.friction = ballFriction;
  circle.restitution = ballRestitution;
  b2body.AddShape(circle);
  b2body.userData = {element: element};
  b2body.position.Set( Math.random() * stage[2], Math.random() * (stage[3]-size) + size/2);
  b2body.linearVelocity.Set( Math.random() * 200, Math.random() * 200 );
  bodies.push( world.CreateBody(b2body) );
}
 

First, we create an absolutely positioned DIV that will house our Canvas tag:

JAVASCRIPT:

var element = document.createElement( ‘div’ );
element.className = className;
element.width = element.height = size;
element.style.position = ‘absolute’;
element.style.left = -size + ‘px’;
element.style.top = -size + ‘px’;
element.style.cursor = “default”;
canvas.appendChild(element);
elements.push( element );
 

Then we draw the actual circle itself using the Canvas tag and append it to our container DIV (Note that an SVG circle created programmatically could have also been used here):

JAVASCRIPT:

var circle = document.createElement( ‘canvas’ );
circle.width = size;
circle.height = size;
if (className !==‘image’ && className !==‘image first’) {
   var graphics = circle.getContext( ’2d’ );
   graphics.fillStyle = color;
   graphics.beginPath();
   graphics.arc( size * .5, size * .5, size * .5, 0, PI2, true );
   graphics.closePath();
   graphics.fill();
}
element.appendChild( circle );
 

Then we create another DIV to house the HTML content itself:

JAVASCRIPT:

content = document.createElement( ‘div’ );
content.className = “content”;
content.onSelectStart = null;
content.innerHTML = html;
element.appendChild(content);
if (className !==‘image’ && className !==‘image first’ ) {
   content.style.width = (size – contentPadding*2) + ‘px’;
   content.style.left = (((size – content.clientWidth) / 2)) +‘px’;
   content.style.top = ((size – content.clientHeight) / 2) +‘px’;
}
 

Notice that we are setting content.onSelectStart to null above; this is so that when you run the mouse button over the text it doesn’t select (An alternative way to do this is to use the HTML pointer-events CSS property).

Next comes code to deal with the physics of the circles, which uses Box2D.js, a JavaScript physics engine ported from Flash:

JAVASCRIPT:

var b2body = new b2BodyDef();
var circle = new b2CircleDef();
circle.radius = size / 2;
circle.density = ballDensity;
circle.friction = ballFriction;
circle.restitution = ballRestitution;
b2body.AddShape(circle);
b2body.userData = {element: element};
b2body.position.Set( Math.random() * stage[2], Math.random() * (stage[3]-size) + size/2);
b2body.linearVelocity.Set( Math.random() * 200, Math.random() * 200 );
bodies.push( world.CreateBody(b2body) );
 

Basically, we define a circle, give it a radius, density, friction, and restitution, and then add it to our collection of shapes with a position and linear velocity. Box2D will then handle the physics and we can just take the values back out to draw things on the screen with a setInterval, which happens in the loop method:

JAVASCRIPT:

function loop() {
  delta[0] += (0 – delta[0]) * .5;
  delta[1] += (0 – delta[1]) * .5;
  world.m_gravity.x = 0 // -(0 + delta[0]);
  world.m_gravity.y = -(100 + delta[1]);
  mouseDrag();
  world.Step(timeStep, iterations);
  for (i = 0; i <bodies.length; i++) {
    var body = bodies[i];
    var element = elements[i];
    element.style.left = (body.m_position0.x(element.width>> 1)) + ‘px’;
    element.style.top = (body.m_position0.y(element.height>> 1)) + ‘px’;
    if (ballRotation && element.tagName == ‘DIV’) {
      var rotationStyle = ‘rotate(‘ + (body.m_rotation0 * 57.2957795) + ‘deg)’;
      element.style.WebkitTransform = rotationStyle;
      element.style.MozTransform = rotationStyle;
      element.style.OTransform = rotationStyle;
    }
  }
}
 

This method gets called with a setInterval periodically. Basically, we apply a delta and a gravity at each time step; see if the mouse is being pressed down (with hooks for touch devices like the iPhone to see if a finger is being pressed down); tell the Box2D World about our gravity and delta and to make an iteration step; and finally use the computed physics values from Box2D to apply CSS3 rotation transforms on our parent DIV and move the element’s CSS top and left values around the screen.

Continue reading »

Tagged with:
Aug 23

Don’t be bummed it’s Monday, ‘cuse the CSS3 Song is here to cheer you up:

How can you go wrong with lyrics like this:

CSS3
Web animation done properly
CSS3
Degrading gracefully

I had a dream, an awesome dream
People surfing in the park
On Windows, Linux and Mac
And their page load speeds were oh-so-high
No big JavaScript library
Just to show some eye-candy

CSS3
Web animation done properly
CSS3
Degrading gracefully

As we surf down the superhighway
Did you ever even think
We could replace <marquee> and <blink>?
With just one, just one line of code
But it’s open to abuse
We must be careful not to overuse it

CSS3
Web animation done properly
CSS3
Degrading gracefully

CSS3
Web animation done properly
CSS3
Degrading gracefully
Degrading gracefully
With HTML5, naturally

[via Bruce Lawson]

Continue reading »

Tagged with:
Aug 20

We’ve seen a number of nice CSS3 generators. I stumbled across another one recently that has a nice set of features for autogenerating the following from a single CSS3 generator web page:

  • Border Radius
  • Gradients
  • CSS Transforms
  • CSS Animations
  • CSS Transitions
  • Text Shadow
  • Box Shadow
  • Text Rotation
  • @Font Face

Continue reading »

Tagged with:
Jul 27

Today we presents collection of best CSS3 and HTML5 tutorials and articles.

read more

Continue reading »

Tagged with:
Jul 19

Everyone’s chomping at the bit to leverage new HTML5 and CSS3 features but with some older browsers not supporting them, hacks are still needed to make things work in a cross-browser fashion. We’ve seen libs that make things easier such as Remy Sharp’s html5shiv and Modernizr and now we can add another one.

Jason Johnston’s new PIE library makes it easy to rendering several of the most useful CSS3 decoration features within Internet Explorer versions 6 through 8. He took an interesting approach by using IE DHTML Behaviors to style the elements and provide the necessary functionality to emulate the CSS3 functionality. So to add rounded corners to an element, your CSS code might look like this in plain ‘ole CSS:

CSS:

#myElement {
    background: #EEE;
    padding: 2em;
    -moz-border-radius: 1em;
    -webkit-border-radius: 1em;
    border-radius: 1em;
}
 

To add support in IE 6-8 using PIE, you’d add this:

CSS:

#myElement {
    …
    behavior: url(PIE.htc);
}
 

PIE currently has full or partial support for:

  • border-radius
  • box-shadow
  • border-image
  • multiple background images
  • linear-gradient background images

Unfortunately, there seems to only be one demo at the moment, which is border-radius rendering via the home page, but it’s still seems like a good start with a lot of future potential.

I’ve never personally used IE DHTML Behaviors or HTML Components so I looked them up and found these intro links for those who might be interested in better understanding them:

Using HTML Components to Implement DHTML Behaviors in Script
Introduction to DHTML Behaviors

Continue reading »

Tagged with:
Jun 23

In this article we collected the best CSS3 and HTML5 tutorials for last month.

read more

Continue reading »

Tagged with:
Jun 02

Greg Murray has an early beta of a fun sample app: HTML5 Fish Tank. The app lets you build out your fishes, and then place them in the tank.

It puts the low level Canvas and CSS3 transitions/transforms to work.

E.g.

HTML:

<div id=“1275463173677″ class=“fish” style=“-webkit-transition-duration: 3369ms; -webkit-transform-origin-x: 508px; -webkit-transform-origin-y: 485px; -webkit-transform: rotateY(0deg) translate(508px, 485px); “><div style=“position: absolute; margin-top: 27px; margin-left: 70px; “><canvas width=“30″ height=“44″ style=“width: 30px; height: 66px; “></canvas></div><div style=“position: absolute; margin-top: 25px; margin-left: 2px; “><canvas width=“71″ height=“71″ style=“width: 71.5px; height: 71.5px; “></canvas></div><div style=“position: absolute; margin-top: 40px; margin-left: 19px; “><canvas width=“20″ height=“20″ style=“width: 20px; height: 20px; “></canvas></div><div style=“position: absolute; margin-top: 59px; margin-left: 4px; “><canvas width=“44″ height=“44″ style=“width: 44px; height: 44px; “></canvas></div><div style=“position: absolute; margin-top: 45px; margin-left: 40px; “><canvas width=“20″ height=“20″ style=“width: 20px; height: 20px; “></canvas></div><div style=“position: absolute; margin-top: 19px; margin-left: 26px; “><canvas width=“33″ height=“33″ style=“width: 33px; height: 33px; “></canvas></div><div style=“position: absolute; margin-top: 81px; margin-left: 40px; “><canvas width=“44″ height=“44″ style=“width: 44px; height: 44px; “></canvas></div></div>
 

The fish animation is driven by code like this:

JAVASCRIPT:

function goFish( target) {
        if ( !target.lastPoint) {
            target.lastPoint = target.currentPoint;
        }
        if ( target.timer ) {
            clearTimeout(  target.timer );
        }
        target.currentPoint = getRandomPoint();
        target.style.webkitTransitionDuration = target.currentPoint.d;
        target.style.webkitTransformOrigin = target.currentPoint .x + ” “ + target.currentPoint.y;
        var _scale = “”;
        if ( target.lastPoint.x> target.currentPoint.x ) {
            _scale = “rotateY(-180deg)”;
        } else {
             _scale = “rotateY(0deg)”;
        }
         target.style.webkitTransform =  _scale + ” translate(“ + target.currentPoint.x + “px, “ + target.currentPoint.y + “px)”;
        target.lastPoint = target.currentPoint;
        target.timer = setTimeout( function () {
            console.log( “saved the day” );
            goFish( target);
        }( target.currentPoint.d + 2000 ) );
}
 

A fun little way to start your day.

Continue reading »

Tagged with:
May 31

Gordon Brander has a fun demo implementing
the “Stacks” dock concept from Leopard using CSS3 only, including the following:

  • :target pseudo-class
  • transform
  • transition
  • border-radius
  • border-image
  • CSS gradients
  • rgba colors
  • text-overflow: ellipsis

Check out the source to see it at work, with code such as:

CSS:

.stack:target> ul {
        opacity: 1;
        -moz-transform: scale(1); /* FF3.5+ */
        -webkit-transform: scale(1); /* Saf3.1+, Chrome */
        -o-transform: scale(1); /* Opera 10.5 */
        transform: scale(1) ;
}

.stack> ul> li> a:focus {
        background-image: gradient(linear,left top,left bottom, color-stop(0, rgba(221, 221, 221, .8)), color-stop(1, rgba(61, 61, 61, .8)));
        background-image: -moz-linear-gradient(top, rgba(221, 221, 221, .8), rgba(61, 61, 61, .8)); /* FF3.6 */
        background-image: -webkit-gradient(linear,left top,left bottom, color-stop(0, rgba(221, 221, 221, .8)), color-stop(1, rgba(61, 61, 61, .8))); /* Saf4+, Chrome */
        outline: 0;
}
 

Continue reading »

Tagged with:
May 18

Alex Girón, of CSS Beauty, has been playing with CSS3 goodness. His current experiment recreates our solar system spinning around using border-radius, transforms, and animations.

The experiment runs beeeautifully on WebKit Nightly, but for some reason it lags on Chrome.

Alex gives us the deatils:

border-radius

CSS:

ul.solarsystem li.sun {
    width: 40px;
    height: 40px;
    -webkit-border-radius: 20px;
    -moz-border-radius: 20px;
    border-radius: 20px;
}
 

Animations and Transforms

CSS:

ul.solarsystem li {
    -webkit-animation-iteration-count:infinite;
    -webkit-animation-timing-function:linear;
    -webkit-animation-name:orbit;
}
ul.solarsystem li.earth span {
    -webkit-animation-iteration-count:infinite;
    -webkit-animation-timing-function:linear;
    -webkit-animation-name:moon;
}
ul.solarsystem li.mercury {-webkit-animation-duration:5s;}
ul.solarsystem li.venus {-webkit-animation-duration:8s;}
ul.solarsystem li.earth {-webkit-animation-duration:12s;}
ul.solarsystem li.earth span {-webkit-animation-duration:2s;}
ul.solarsystem li.mars {-webkit-animation-duration:20s;}
ul.solarsystem li.asteroids_meteorids {-webkit-animation-duration:50s;}
ul.solarsystem li.jupiter {-webkit-animation-duration:30s;}
ul.solarsystem li.saturn {-webkit-animation-duration:60s;}
ul.solarsystem li.uranus {-webkit-animation-duration:70s;}
ul.solarsystem li.neptune {-webkit-animation-duration:100s;}
ul.solarsystem li.pluto {-webkit-animation-duration:120s;}

@-webkit-keyframes orbit {
from { -webkit-transform:rotate(0deg) }
to { -webkit-transform:rotate(360deg) } }
 

Continue reading »

Tagged with:
May 14

CSS3 has a lot of great stuff in it. Transforms, new layout, media query, CSS object model, you name it. How do you keep up with the specs?

That is what Šime Vidas thought, so he whipped together a little iframe love:

Continue reading »

Tagged with:
May 12

Today we presents a collection of HTML5 and CSS3 tutorials.

read more

Continue reading »

Tagged with:
Apr 19

html5cssreadiness

Paul Irish and Divya Manian have created a fun visualization on readiness of HTML5 and CSS3 standards in various browsers.

It uses a bunch of the usual CSS cool-suspects: -webkit-gradient, -webkit-transition, -webkit-border-radius, and the like (and -moz/-o too).

The added feature is…. do a mouse scroll on the page:

JAVASCRIPT:

  1.  
  2. jQuery(document).bind(‘DOMMouseScroll mousewheel’, function(e, delta) {
  3.     var newval,
  4.         num = $(‘div.css-chart p’).css(‘padding-left’);
  5.  
  6.     delta = delta || e.detail || e.wheelDelta;
  7.  
  8.     if (delta> 0 || e.which == 38) {
  9.         newval = parseFloat(num) + 10 * (e.shiftKey ? .1 : 1);
  10.     } else if (delta <0 || e.which == 40) {
  11.         newval = parseFloat(num)10 * (e.shiftKey ? .1 : 1);
  12.     } else {
  13.         return true;
  14.     }
  15.    
  16.     $(‘style.padleft’).remove();
  17.     $(‘<style class="padleft"> div.css-chart p { padding-left : ‘+newval+‘px; }’)
  18.         .appendTo(document.body);
  19.  
  20.     e.preventDefault();
  21.  
  22. })
  23.  

Continue reading »

Tagged with:
preload preload preload