HTML5 canvas and the memory game

This week I finally had the chance to start looking at this book I bought a few months ago,  it’s called The essential guide to HTML5. The book is based on a bunch of exercises (games really) using JavaScript and HTML5 Canvas, ready to put into code and test. It doesn’t get into theory but for those looking to get things working quickly, this is definitely a good one.

So I decided to make the “Memory Game” to learn about the basics of canvas:

So, what is canvas? and what you can do with it?

<canvas> is a HTML5 element used to draw graphics using JavaScript (most of the times). Imagine a blank paper where you can draw shapes, images and text. When adding  it to your page it exposes one or more rendering contexts which are used to create and manipulate the content that will be displayed. The demo uses the 2d context.

Canvas are mostly used for creating graphics, animations, games and images compositions.

The basic code for using and initialising a canvas will look like this:

HTML code

<canvas id="mainCanvas" width="900" height="600">
Sorry, your browser doesn't support the HTML5 element canvas.
</canvas>

The id wil be used to initialise the canvas using JavaScript, the width and height to set the size of it and the text inside will be used as a callback when the browser doesn’t support it (check this useful link to see the list of browsers and versions that support canvas).

JavaScript code

var canvas = document.getElementById('mainCanvas');
// check if browser supports it
if (canvas && canvas.getContext) {
ctx = canvas.getContext('2d');
if (ctx) {
// Start drawing here
}
}


Start drawing!
If you want to learn more about canvas check out this link. It’s from the Mozilla developer’s website and it helped me a lot with the theory behind canvas and all the things you can create. As you will see, there is a lot of possibilities and all depends in what you want to achieve.

Check out the demo
To see the memory card demo click here and its javaScript code here. Bear on mind this is a basic example only for practice purposes =)

 

Posted in Canvas, HTML5 | Leave a reply

CSS: box-sizing, so glad I’ve found you!

So you’re writing some CSS for your website. You are about to “theme” an element, let’s say a DIV, you set a width to it in order to look as you want. Then you add some content inside and most likely you will add some padding (to add some space).

In the CSS “box-model” what will happen is that the width of the element (and the height) will be increased due to the padding applied. If the width was set to 100px and you added 20px padding, the rendered element will measure 140px (100px + 20px padding left + 20px padding right)

But 140px doesn’t fit in your layout! (that’s why you set it to be 100px in first place!) You will need then to re-mesure all over again and start decreasing widths all around your CSS…. in short: a bit of a nightmare!

But hey! did you know there is a MAGIC property that allows you to add padding and borders to your elements without even bothering about the expanding issue explained above?

It’s called box-sizing, and what it does is to subtract pixels from the original size (instead of adding them). To use it just add these lines to the element you want to keep the size you are specifically setting:

#main {
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
width: 100px;
padding: 20px;
}

There is a great article by Paul Irish suggesting the idea of using the property to the universal selector, meaning all elements will have it. I personally find it brilliant, but it’s up to you to use it that way.

Unfortunately, as you may have already guessed, this only works for new browsers (IE8+) but that’s not that bad, isn’t? :)

Posted in CSS3 | Leave a reply

Romina Amaro Photography

Hello everybody! It’s been a while since my last post. Just wanted to share this website, it’s a beautiful portfolio from a very talented friend. I helped her putting the site together but the photos stand out by themselves.

If you like the photos, feel free to share them using the social buttons inside of each gallery or pinning them on Pinterest! Big Thanks!

This is the link: www.rominaamaro.com

If you are interested in the WordPress theme used in the website, check it out here.

Posted in Photography, Wordpress | Leave a reply

The magic of the “contenteditable” HTML attribute

How come I’ve never heard about the “content editable” HTML attribute before?  Apparently it’s been around for a while. Even the 5.5 version of IE supports it! (yes…. 5.5!!)

I’m really happy to have learned about it. From a developer point of view I can see myself using it for plenty of things where jQuery would have been my first choice. However, without jQuery the attribute could be a bit pointless but let’s first explain what this all about.

So what is the contenteditable attribute?

The contenteditable attribute is an enumerated attribute whose keywords are the empty string, true, and false. The empty string and the true keyword map to the true state. The false keyword maps to the false state. In addition, there is a third state, the inherit state, which is the missing value default (and the invalid value default). – WHATWG

In plain words, if an element has the contenteditable attribute set to true, the browser will make that element editable, and viceversa.

In code:

<div id="text-container" contenteditable="true">Some text here</div>

So imagine having a picture where people can add comments wherever they want just by clicking on it (Flickr style) or edit content in your website by clicking it. That’s the really cool part of it. Just one attribute set and you are ready!

To achieve this, in a proper way, we will need some kind of communication with the server, especially if our intention is to save the content and persist it, and that’s why Ajax exists! :)

I’ll be posting an example shortly to show how to do that but meanwhile, have a look a this small demo.

 

Posted in HTML | Leave a reply

The New Year 2012 is coming…

… and I haven’t written any resolutions yet! I’m not going to fool anyone:  I’ve never done such a thing.

Personally I believe opportunities come up when you least expect them, sometimes you don’t even notice. And by doing a written “plan” is like you’re following step by step things that probably will never happen or you will be so focus on them that you will miss some others.

Of course I have some things in my mind I would like to achieve and the only thing I need in order to do it is a healthy body and mind. So that’s the only thing I really ask every year for my family, friends and myself.

I wish you all the best for the next year. I will try to keep this blog updated next year (this sounds like a resolution!!!!!)

Silvia.

Posted in Article, Freebies | Leave a reply

Planning my story

Planning my story

Another wallpaper inspired by a Sunday. This is, in fact, a new year resolution… still planning.
PS.- The pic was taken in a sunny freezing day in Brighton – UK

Posted in Freebies | Leave a reply

How to preview a file before submitting a form?

There are tons of different solutions out there to achieve this. Specially using jQuery.

Your are filling up a form, upload a picture and, there it is: a preview of the file before submitting it. Simple!

Continue reading

Posted in jQuery | 2 Replies

Wise words by Steve Jobs

Today a genius has gone, but his legacy will live forever.

I really hope these words get translated and spread all over the world. From a truly inspiring human being: Steve Jobs.

Continue reading

Posted in News | Leave a reply

Today’s wallpaper goes to… Rafa!

No, I’m not spanish but I have a huge respect and passion for sports from that country (except the bullfighting, which I doubt we can call it “sport”). And one of my favourite sportsman is Rafael Nadal.

His titles speak by themselves but what I like the most about him is his way of winning -and losing!!!-, and the big respect he always shows to his opponents,  outside and inside the court.

So today I’ve made this wallpaper, celebrating the fact that Rafa won today his Davis Cup match against Tsonga (therefore the Spanish team going to the finals in December).

Vamos Rafa!

Posted in Freebies, Wallpapers | Leave a reply

Viva la vida!

Ok, my friend just told me there is a Coldplay song with this title. Let’s clarify I didn’t know that or at least that was not the inspiration for this little piece of work (I still love Coldplay!)

This is just another Sunday afternoon work reflecting my current mood. I hope you like it!

Viva la vida

Posted in Freebies, Wallpapers | 1 Reply