Showing posts with label graphics. Show all posts
Showing posts with label graphics. Show all posts

How to make a CSS3 and JavaScript analog clock

1 2 3 4 5 6 7 8 9 10 11 12

This is a "How To" guiding the ones that are interesting in the creation of an analog clock, similar to the one on the left, with CSS3 and jQuery. First of all, I would like to say that this is in no way a useful method of placing an analog clock on your website. It's only meant as an experiment with CSS3. If you do want an analog clock, you could probably use Flash or an image based JavaScript clock.

I used as a reference the Windows 7 sidebar clock, but it's not a perfect copy. I could have added a lot more details to make it look closer to the real one, but it would have made it a lot more difficult to implement.

Internet Explorer (any version) has not been targeted for this experiment due to it's lack of CSS3 support, so don't be surprised if it doesn't work. Making it functional in Internet Explorer would have over-complicated the JavaScript, attempting to make it work with IE's filters. With that out of the way, let's get started.

The HTML

All the styles will be set in the stylesheet later on, so don't bother with it for now. You can see there are a lot of tags that make the clock very heavy. That's why this is not a good way to create an analog clock for use on your website.

The div#clock will hold everything together. The div.hour-marks holds all the numbers and hour marks. We could have done without it, but it's good to have some sort of container just for those. The next 3 divs are the clock handles (hours, minutes, seconds). The last span is the round circle in the center of the clock.

<div id="clock">
 <div class="back"></div>

 <div class="hour-marks">
  <span class="mark m1"></span>
  <span class="mark m2"></span>
  <span class="mark m3"></span>
  <span class="mark m4"></span>
  <span class="mark m5"></span>
  <span class="mark m6"></span>
  <span class="mark m7"></span>
  <span class="mark m8"></span>
  <span class="mark m9"></span>
  <span class="mark m10"></span>
  <span class="mark m11"></span>
  <span class="mark m12"></span>
  <span class="hour h1">1</span>
  <span class="hour h2">2</span>
  <span class="hour h3">3</span>
  <span class="hour h4">4</span>
  <span class="hour h5">5</span>
  <span class="hour h6">6</span>
  <span class="hour h7">7</span>
  <span class="hour h8">8</span>
  <span class="hour h9">9</span>
  <span class="hour h10">10</span>
  <span class="hour h11">11</span>
  <span class="hour h12">12</span>
 </div>

 <div class="hour-handle"><span class="handle"></span></div>
 <div class="minute-handle"><span class="handle"></span></div>
 <div class="seconds-handle"><span class="handle"></span></div>

 <span class="center"><span></span></span>
</div>

The CSS

Oh man, this is a lot of CSS for something so trivial. Let's try to lay it out.

We're going to start with our clock div, or better yet, div#clock and standard CSS. I gave it a width and height of 200px, but you can change that to whatever you like. Take note however that you will need to modify the position of the dandles and marks also. Actually you will need to modify pretty much everything. Position:relative will allow us to position everything else inside it with position:absolute.

div#clock{
 width: 200px;
 height: 200px;
 position: relative;
 }

Now for the fun part. We don't need a rectangular clock. That would look unattractive. Instead we will make it round with the border-radius feature in CSS3. This allows you to make the corners of a box element round. But if you take it all the way up, that box will turn into a circle. For that you need to specify at least half of the width or height, in this case 100px. This works best for squares as it creates a perfect circle. The first rule is the standard CSS3 declaration. This is used by Opera also. The next one is the proprietary declaration for Gecko based browsers (Firefox, Flock) and the last, but not least, is the proprietary declaration for Webkit based browsers (Safari, Chrome).

div#clock{
 border-radius: 100px;
 -moz-border-radius: 100px;
 -webkit-border-radius: 100px;
 }

To make it look a little nicer, we'll add a gradient for the background. First the standard CSS background. Opera will use this as it does not have a linear gradient method implemented yet. The second one is for Mozilla and the third for Webkit.

div#clock{
 background: #3a3937;
 background: -moz-linear-gradient(right 240deg, #3a3937, #1b1d1c);
 background: -webkit-gradient(linear, right top, left bottom, from(#3a3937), to(#1b1d1c));
 }

Now we're adding a drop-shadow around our clock, just to make it stand out a bit. This can be changed to look like a halo on a dark background. The color can be changed and also the size of the shadow.

div#clock{
 box-shadow: 0 0 10px #666;
 -moz-box-shadow: 0 0 10px #666;
 -webkit-box-shadow: 0 0 10px #666;
 }

These methods will be used throughout the stylesheet, so there's no point in describing each of them. Here's some more of the CSS:

div#clock div.back{
 width: 184px;
 height: 184px;
 position: absolute;
 top: 8px;
 left: 8px;

 border-radius: 92px;
 -moz-border-radius: 92px;
 -webkit-border-radius: 92px;

 background: #fffdf4;
 background: -moz-linear-gradient(top 260deg, #fffdf4, #e2dfce);
 background: -webkit-gradient(linear, center top, center bottom, from(#fffdf4), to(#e2dfce));

 box-shadow: inset 0 0 5px #000;
 -moz-box-shadow: inset 0 0 9px #000;
 -webkit-box-shadow: inset 0 0 9px #000;
}
div#clock div.hour-marks span{
 display: block;
 position: absolute;
}
div#clock div.hour-marks span.mark{ background: #333; width: 8px; height: 3px;}
div#clock div.hour-marks span.hour{
 font-size: 14px;
 text-align: center;
 font-family: Helvetica, Arial, sans-serif;
 color: #444;
}
div#clock span.center,div#clock span.center span{
 position: absolute;
 top: 95px;
 left: 95px;
 background: #333;
 width: 10px;
 height: 10px;

 border-radius: 5px;
 -moz-border-radius: 5px;
 -webkit-border-radius: 5px;
}
div#clock span.center span{ width: 6px; height: 6px; top: 2px; left: 2px; background: #666;}

In order to rotate our hour marks so they face towards the center of the clock, we will be using -*-transform: rotate(). This rotates the element by a specific number of degrees. Since we have 12 hours and a circle has 360 degrees, each mark will be rotated at 30 degree intervals. The positioning of these marks are more of a guess. I didn't feel like finding a method of calculating where each of them should go on a circle. So I positioned them by eye.

div#clock div.hour-marks span.m1 {-moz-transform: rotate(-60deg); -webkit-transform: rotate(-60deg); -o-transform: rotate(-60deg); top:  22px; right:  52px;}
div#clock div.hour-marks span.m2 {-moz-transform: rotate(-30deg); -webkit-transform: rotate(-30deg); -o-transform: rotate(-30deg); top:  52px; right:  22px;}
div#clock div.hour-marks span.m3 {-moz-transform: rotate(0deg);   -webkit-transform: rotate(0deg);   -o-transform: rotate(0deg);   top: 100px; right:   8px;}
div#clock div.hour-marks span.m4 {-moz-transform: rotate(30deg);  -webkit-transform: rotate(30deg);  -o-transform: rotate(30deg);  top: 146px; right:  22px;}
div#clock div.hour-marks span.m5 {-moz-transform: rotate(60deg);  -webkit-transform: rotate(60deg);  -o-transform: rotate(60deg);  top: 176px; right:  54px;}
div#clock div.hour-marks span.m6 {-moz-transform: rotate(90deg);  -webkit-transform: rotate(90deg);  -o-transform: rotate(90deg);  top: 188px; right:  95px;}
div#clock div.hour-marks span.m7 {-moz-transform: rotate(120deg); -webkit-transform: rotate(120deg); -o-transform: rotate(120deg); top: 176px; right: 140px;}
div#clock div.hour-marks span.m8 {-moz-transform: rotate(150deg); -webkit-transform: rotate(150deg); -o-transform: rotate(150deg); top: 146px; right: 170px;}
div#clock div.hour-marks span.m9 {-moz-transform: rotate(180deg); -webkit-transform: rotate(180deg); -o-transform: rotate(180deg); top: 100px; right: 184px;}
div#clock div.hour-marks span.m10{-moz-transform: rotate(210deg); -webkit-transform: rotate(210deg); -o-transform: rotate(210deg); top:  52px; right: 170px;}
div#clock div.hour-marks span.m11{-moz-transform: rotate(240deg); -webkit-transform: rotate(240deg); -o-transform: rotate(240deg); top:  22px; right: 140px;}
div#clock div.hour-marks span.m12{-moz-transform: rotate(270deg); -webkit-transform: rotate(270deg); -o-transform: rotate(270deg); top:  10px; right:  95px;}

div#clock div.hour-marks span.h1 { top:  27px; right:  62px;}
div#clock div.hour-marks span.h2 { top:  50px; right:  35px;}
div#clock div.hour-marks span.h3 { top:  90px; right:  20px; font-weight: bold; font-size: 16px}
div#clock div.hour-marks span.h4 { top: 130px; right:  35px;}
div#clock div.hour-marks span.h5 { top: 153px; right:  62px;}
div#clock div.hour-marks span.h6 { top: 163px; right:  95px; font-weight: bold;font-size: 16px}
div#clock div.hour-marks span.h7 { top: 153px; right: 132px;}
div#clock div.hour-marks span.h8 { top: 130px; right: 160px;}
div#clock div.hour-marks span.h9 { top:  90px; right: 170px; font-weight: bold;font-size: 16px}
div#clock div.hour-marks span.h10{ top:  50px; right: 160px;}
div#clock div.hour-marks span.h11{ top:  27px; right: 132px;}
div#clock div.hour-marks span.h12{ top:  20px; right: 100px; font-weight: bold;font-size: 16px}

The handles are also rotated using CSS3 and JavaScript. To make them rotate around a certain axis, we need to set the transformation origin. If we don't do that, they would just spin in one place, like the needle on a compass.

div#clock div.hour-handle, div#clock div.minute-handle, div#clock div.seconds-handle{
 height: 55px;
 width: 6px;
 position: absolute;
 top: 45px;
 left: 97px;
 overflow: hidden;

 -moz-transform-origin: 50% 100%;
 -webkit-transform-origin: 50% 100%;
 -o-transform-origin: 50% 100%;
}
div#clock div.minute-handle{ height: 80px; top: 20px; width: 4px}
div#clock div.seconds-handle{ height: 80px; top: 20px; left: 100px; width: 1px;}

The color for the handles is given by the inside span.handle. This will also suffer some transformations, in order to become pointed towards one end. In this case we will skew them along the Y axis by a huge amount. The number of degrees indicates the angle at which the skewing is make. This increases the height of the handle, but it's parent container has overflow:hidden and the only part that we can see is the one that we nee: a nice sharp clock handle.

div#clock div.hour-handle .handle, div#clock div.minute-handle .handle, div#clock div.seconds-handle .handle{
 display: block;
 background: #333;
 width: 180%;
 height: 100%;

 -moz-transform: skewY(-84deg);
 -webkit-transform: skewY(-84deg);
 -o-transform: skewY(-84deg);
}
div#clock div.minute-handle .handle{
 -moz-transform: skewY(-86deg); 
 -webkit-transform: skewY(-86deg); 
 -o-transform: skewY(-86deg);
}
div#clock div.seconds-handle .handle{
 -moz-transform: skewY(-88deg); 
 -webkit-transform: skewY(-88deg); 
 -o-transform: skewY(-88deg); 
 background: #f00;
}

That's about all the CSS required for our analog clock.

The JavaScript - jQuery, what else?

After loading the jQuery framework (1.4.2 in this case, but older versions might work) we can add the JavaScript required for the clock to function. It does this by repeating a function every second with the help of setInterval(). Read the comments below for more information. The position / angle of the handles is modified each time the function is run using the same -*-transform: rotate() method described in the CSS.

/* make sure everything is loaded */
$(document).ready(function(){
 /*
 *  this function is run every second to update the clock handles
 */
 function setClock(){
  /* get the handles of the clock */
  var hourHandle = $('div#clock div.hour-handle');
  var minuteHandle = $('div#clock div.minute-handle');
  var secondsHandle = $('div#clock div.seconds-handle');
  /* get the current date from te users computer */
  var now = new Date();

  /* 
  * determine the degrees we need to rotate each handle 
  * we need to create this variable as a string here, before applying it
  */
  var hourDeg = 'rotate(' + ((now.getHours() * 30) + Math.floor(now.getMinutes() / 2)) + 'deg)';
  var minDeg = 'rotate(' + (now.getMinutes() * 6) + 'deg)';
  var secDeg = 'rotate(' + (now.getSeconds() * 6) + 'deg)';

  /* change the CSS for each of the handles */
  hourHandle.css({
   '-moz-transform': hourDeg,
   '-webkit-transform': hourDeg,
   '-o-transform': hourDeg
  });
  minuteHandle.css({
   '-moz-transform': minDeg,
   '-webkit-transform': minDeg,
   '-o-transform': minDeg
  });
  secondsHandle.css({
   '-moz-transform': secDeg,
   '-webkit-transform': secDeg,
   '-o-transform': secDeg
  });
 }

 /* call the function every second */
 setInterval( setClock, 1000);
 });

This is all there is to it. A working example can be seen at the top of this page. I would like to repeat myself: this is not a good way to add an analog clock to your website. It's just a way of showing what can be done with CSS3. Here is the complete code used to make the clock.

The HTML

The CSS

The JavaScript

If you have any comments or questions, please let me know.

Speed drawing/painting

A while ago I was going over some videos on YouTube.com looking for different drawing techniques and I came across quite a few speed drawings (paintings). For those of you who don't know what speed drawing is, the simplest explanation is to watch one. I don't know the exact definition of the term, but from what I know it's the action which involves the filming of an entire drawing process, playing it at high speed and completing it with an appropriate sound track. The result is a very good insight in how these people work and a very good knowledge resource.

As I said, the videos show you almost the entire drawing process, so you can see how sketching, coloring, shading and highlighting is done. If you want to start learning drawing or even if you are looking to improve your skills, these show you how certain things can be done. They don't beat a drawing class, but they're a good intro and even a better motivation tool.

If you look around on the Internet you'll definitely find all sort of websites dedicated to this form of art, some might call it. One of my favorite is http://www.speed-painting.com. The website owner is Nico Di Mattia and that's the place I've seen some of the best videos. On YouTube (probably even on other video sharing servers) you can find speed drawings of artwork created in the almighty Adobe Photoshop, but you can find some that are real amazing, like the one I've embedded below. It's entirely created using MS Paint. That's right, the lame one that comes with Windows. Just watch the video and you'll see what someone with proper skills and a lot of patience can achieve.

Comic Books

A source of entertainment for both kids and adults. The nice graphics and the catching story, the continuous battle between good and evil, the inner feelings of the main character, the funny conversations or depicted situations, the drama, the action, the comedy, ahhh...

Comic books give many of us a great way to relax, to forget about the mundane things and return to that time of our life when superheroes and super villains were real and they used their powers to fight each other. It the time when kids look for idols and role models. People they want to be. It's the time of happiness and inner peace when you don't feel the everyday worries and stress. I think that's mainly one of the reasons adults still enjoy reading them. It gives them back something they lost or they don't want to lose: their childhood.

Believe it or not but, but the first known comic book appeared in Europe in 1837 and was published in several languages. It's name was "The Adventures of Obadiah Oldbuck" and it was a lot different than the comic books we know today. Back then there were no balloons, the text being situated under the panels, describing the story. There were more like books than comics, but those are the pioneers. The first comic books were published like part of a magazine. Soon the newspapers noticed the increased attraction in the phenomenon and started to publish short stories as a weekend bonus. In 1897 "The Yellow Kid in McFadden's Flats" comes out, introducing the phrase"comic book" printed on the back cover and the balloon as a mean to show what the characters are saying. For a while, this was considered to be the first comic book. Thus the basis of the industry was created.

The first real monthly comic book("Comics Monthly") was printed in 1922 and lasted for 12 months. Walt Disney released in 1931 the first Mickey Mouse comic book named "The Adventures of Mickey Mouse". After that several others followed. Around this period characters like Flash Gordon, Dick Tracy and Tarzan made their appearance in comic books. Different companies sponsored or ordered comic books to promote their name or products, increasing people's attraction for this kind of literature.

In 1938 "Action Comics #1" comes out and the world is changed forever. Why you ask? What was so special about this comic? It featured Superman. Yes people, that's how old Superman is. This was the first time ever the main character of the book has amazing powers, like bouncing bullets of his chest, flying, running faster than a train, leaping buildings and other cool stuff. Superman was the first in a long line of heroes end villains that had supernatural powers. A year later another great hero is created, Batman. Allthough he doesn't have any super powers, he has great gadgets and he's a man with secrets and tragedies in his life, not as pure as Superman.

A sad chapter in this history is Dr. Fredric Werthham, a psychologist that considered comic books to be bad for children. He started a seven year study on the effects of the comics on children. In 1954 he published "Seduction of the Innocent", in which he states that these are a major cause of juvenile delinquency and that children got wrong ideas about life, the laws of physics and social status. In 1954 the "Comics Code Authority"(CCA) was created following an U.S. Senate investigation in which Werthham was called to testify. This way the comic book companies had to follow a set of guidelines. The ones that qualified received the approval stamp. Many comics went out of, mainly horror ones business. Others transformed into magazines, the CCA having no authority over them. With time the approval stamp got smaller and smaller. These days the CCA lost it's importance and comics may or may not have the approval. It makes no difference.

Shortly after, comics got back on track. Many more superheroes were created by both DC Comics and Marvel: Conan The Barbarian, The Fantastic Four, Daredevil, The X-Men, Hulk, Iron-Man, Spiderman to name just a few. New stories and new adventures were invented. The 70's changed the way comics were created. They took a more serious approach and touched issues as drug abuse, pollution and racism. The characters and stories became more complex to reflect the changes in the social climate. At first they were not approved by the CCA, but seeing the support from the public their guidelines changed. This in turn gave the opportunity to create new comics that were previously denied by the CCA.

In my opinion comics have kept up with the new trends in media. In fact movies have been and continue to be released featuring famous Marvel and other heroes. Great titles like "Blade", "Hulk", "X-Men", "X2: X-Men United", "X3: The Last Stand", "Daredevil", "Fantastic Four", "Fantastic Four: Rise of the Silver Surfer", "Spiderman" 1,2 and 3, "Ghostrider" and so many others continue to hold a top place in the box office, reviving or creating new adventures for our heroes. Cartoons also make people aware of the complexity of the characters and their background, bringing back to life their old stories in an animated and revamped way.

Whatever other people might say, you're never too old for comic books. It's actually like learning history. Old comics and heroes have been around longer than many of us. And they most certainly have a greater history than many of the contemporary literature characters. Just reading all those comic books is like reading entire novels, spanning over almost a century and depicting the social environment and events of the time. They are more that just drawings. They are truly a form of art.

Graffiti. Art?

Wikipedia: Graffiti (singular: "graffito," although the plural is more common) is the name for images or lettering scratched, scrawled, or more usually spray-painted on property that does not belong to the artist. Graffiti is often regarded by others as unsightly damage or unwanted vandalism.

Most of us already knew that. But what I didn't knew was that graffiti has such a longer history. Apparently it's been around since ancient times, being found in Greek(Ephesus) and Roman cities (Pompeii), as well as Mayan (Tikal), viking and varangian. During Renaissance, many of the great artists of the time carved or painted their names on the ruins of Nero's Domus Arena, thus giving birth to the grottesche style of decoration.

Although many people associate modern graffiti with hip-hop culture, graffiti predates it and has it's own culture and history. It seems that modern graffiti appeared around 1969 and reached it's peak in the mid 70's. Most of the places it was found at that time was on train and subway wagons. Some of the most notable names from that time are Stay High 149, Hondo 1, Phase2, Stitch 1, Joe 136, Junior 161, Cay 161, Barbara 62 and Eve 62. Taki 183, even if he is not the first graffiti artist, he is the first that attracted media attention.

In the period between late 1970's and early 1980's new forms and styles of graffiti developed. After 1985 it started to stagnate and because of several actions took by the US Government and the Transit Authority, who wanted to remove all traces of graffiti from their train wagons, it started to "move to the streets". This way modern graffiti, the one that each and everyone of us knows, is created.

But is graffiti a form of art? It certainly has been used for more than just defacing public or private property. Since ancient graffiti appeared, it's been used to send a message to others, being of political, social or personal nature. In modern time graffiti is used for showing others that the author has been there, much like marking territory. It's been also used for transmitting political messages, expressing an opinion, approval or disapproval for a new law, decree or election. Messages like "stop the war!" or "make piece!" are present in many of these pieces. Thus the author is expressing something through his creation. Isn't that art?

If you still disagree, know that artists like Crash, Daze and Lady Pink have their works exposed in the Brooklyn Museum, as part of the "contemporary art" gallery. Australian art historians ranked some of their local graffiti as visual art. Oxford University Press states in their "Australian Painting 1788-2000" states that graffiti has a key place in contemporary visual culture.

Still not convinced? I can't blame you. Seeing a beautiful white wall filled with awful writings is horrible. But if you see a boring white wall decorated with beautiful graphics and colors, showing a strong message that you agree with is awesome. And just so you don't have a narrow view on the entire subject, you can visit Wikipedia and find out more information about graffiti (this post was largely based on information found there). For a huge graffiti gallery, you can visit http://www.graffiti.org or http://www.puregraffiti.com

The beauty of fractals

A friend of mine told me a few years ago that instead of a classic desktop background, he prefers a fractal image. I had no idea what a fractal is and I didn't give it to much attention. After a while I saw a book about fractals. The colorful drawings on the cover drew my attention :) I started to read it and I remained astonished. The complexity that a simple mathematical function can generate and the beauty of the designs that can come out of that left me speechless. I afterwards realized that I knew the principles of how a fractal works long before. Ever heard of "recursive"? It's a programming term used when a function calls on itself. That's mainly how fractals work. They repeat the same function a number of given times or, if you wish, an infinite number of times. But that will most surely crash your computer eventually. This way the pattern it draws is the same every time, but the position, size, direction, color or other attributes it has may differ.

So what is the definition of a fractal anyway? Well, according to Wikipedia, a fractal is "a rough or fragmented geometric shape that can be subdivided in parts, each of which is (at least approximately) a reduced-size copy of the whole". Got that? To put it simple, if you zoom in on a portion of a fractal, no matter how many times, you see approximately the same pattern, over and over again. Voila. The best graphical representation of infinity.

The term of "fractal" was given by BenoƮt B. Mandelbrot, also known as the "father of fractal geometry". One of the most popular fractals bears his name, the Mandelbrot set.

Fractals also seem to be present in nature, but they are an approximate and finite form. You can find them in clouds, snow flakes, crystals, lightning, ferns, broccoli, cauliflower and even blood vessels.

But what are they good for, other than beautiful and colorful graphics? They're good for a lot of things. The fact that nature uses them in so many situations proves that a fractal is something that offers great advantages. But they are also used in domains such as art(computer generated, African art, painting), signal and image compression, seismology, computer and video game design, medicine and so many others.

The fractals have been around for such a long time and people have come across constantly. Thanks to the introduction of computers, they are now more accessible to everyone and a real science has developed to study and better understand them. This way we'll see them applied in more and more areas. The possibilities that they offer are endless.

Sidewalk drawings

Do you remember when you were kids we used to draw on the sidewalk, using colored chalk? Those were great times. I remember there were drawing contests and kids were drawing all sorts of flowers, people and houses. We would finish a box of chalk faster than a bag of candy. It seems that drawing on the concrete is a lot funnier that drawing on paper. But that was a long time ago.

Still there are people that don't seem to have gotten over it. And even more, they took it to the next level. Julian Beever is one of those people. He managed to turn "pavement drawings" into a real form of art, creating stunning graphics. Using a special drawing method, he creates the illusion of 3D.

SwimGlobe

The downside is that you have to look at them from a particular angle, otherwise you notice the distortion that is needed to create the effect.

Pool - Wrong viewGlobe - Wrong view

For these and other great graphics created by Julian Beever, you can visit his website.