Monday, 11 May 2015

Miss Sunshine and HTML Canvas: Let's Draw Some Circles!

Miss Sunshine and HTML Canvas: Let's Draw Some Circles! Your browser does not support the canvas element.
I've realized that canvas combined with JavaScript is a powerful HTML element to create shapes, make animation and games.

I've created Miss Sunshine just with circles and lines. Let's dissect the code. First, create the canvas with <canvas> </canvas>. Get the context of canvas with variable of the canvas.getContext("2d") and store it in a variable.

Now the drawing begins! Create an object literal and store infos about what color to use, how circles and lins are drawn and the name of the function. Here is the syntax:

var x= {y:detailled information about the object}

We draw Miss Sunshine's head with the arc(x-coordinate,y-coordinate,radius,starting angle,ending angle) method. Now, a little bit of trigonometry is required.
Relax, just a little bit.
The total circumference of a circle (360°) is 2π thus to draw a circle the value of the starting angle is 0 and the ending angle 2π. Paint Miss Sunshine with your favourite color with fillyStyle="your color" and fill her with fill(). Here eyes and lips are created the same way. The lips are created with two small half circles and one big one. A half circle has the 180° (π) therefore the starting angle is π and the ending angle is 2π (or 0 to π depending on the direction).

Roll over the canvas and make her sky blue!

HTML and JavaScript Code

Friday, 24 April 2015

HTML-Code Converter

HTML-Code Converter

The code is

HTML-Code
"I just want a simple HTML-code converter!".

If you write some text in your HTML code,characters like < or > are not shown correctly because those characters are reserved for the code. There are plenty of good sites showing all the HTML codes but I just wanted to find the corresponding code quick and easy thus I've created this converter.

After I've made the converter look nice with CSS I've coded the converting system with JavaScript. Get the text in the input field(id "input") with the code

var x =document.getElementById("id of the input field").value;

Don't forget to put value at the end otherwise the information of the input field will not be stored. Now, we want to output the corresponding HTML code of the character you've typed in. Anykind of number or alphabet is called a "string". A character is a also a string so we look at the JavaScript string methods.

charCodeAt()

That's the string method we've been looking for! Because it converts strings to HTML codes.Write

variable of the input field.charCodeAt()

to output the corresponding code.

Now, let's look at the button. It is invoked through 2 events which are mousedown and mouseup. If you click the mouse which is the "mousedown" event, the code is outputed and the border style of the button is changed to inset so that the button looks like it's pushed down.

I've you finish clicking which is the "mouseup" event, the border style is changed to its initial style which is outset. Finito, now we've created a simple HTML-Code converter.

HTML, CSS and JavaScript Code

Wednesday, 22 April 2015

Pastel Colored Title Design with Flex

Pastel Colored Title Design with Flex C a n d y
I love CSS because you can make all kinds of stylish stuff with it and it's so easy.

This time, I've tried some candy-like title design thus using round pastel colored shapes. The word "candy" is one span element including span child elemtsof each alphabet with different id's.

So how do you make the round shapes?

One way is to create seperate div elements and define height, width and other details for each element. But there is an easier solution to make same sized shapes. The answer is the CSS property flex.

Write display:flex in the parent element (id="candy") flex:some number in the child elements. This number defines the width of the element. It's annoying but to make this property compatible with other browsers you need the prefixes -webkit-flex for Google and Safary, -ms-flex for IE. I hope that we will not need these prefixes someday!

HTML and CSS Code

Thursday, 16 April 2015

Drawing Rectangles With HTML Canvas

Drawing Rectangles With HTML Canvas Your browser does not support the HTML5 canvas tag.
x-coordinate:

y-coodinate:

width:

height:

Recently, I discovered how powerful the HTML canvas element is. You can conviniently draw all kinds of shapes, make animations or even create games with it.

In the example above, create any kind of rectangle by typing in some values inside the input field. Create the canvas element with canvas. Optionally, define the width and height of it.

A canvas without drawing abilities is not very usefuel. In the JavaScript code we specify what we want to do with it. getContext("2d") allows you to draw lines, rectangles, circles and much more! Get the data of the canvas element with document.getElementById("id of the canvas"), return the object of it with the getContext("2d") method and start drawing.

This time, we want to draw a rectangle so we use the fillRect() method. The syntax is as follows:

fillRect(x-coordinate, y-coordinate, width, height)

The values, typed in by the user, is stored in document.getElementById ("name of id").value. All this is fired with an onclick event. The rectangle is erased with the clerRect() method.

The JavaScript and HTML Code

Monday, 13 April 2015

Simple Word Count With JavaScript

Simple Word Count With JavaScript
This word count does not work inside this blog!Please copy&past the code in the text area to a text file and save it as a html file.

Here I've created a simple word count system with JavaScript.

I didn't expect that the code was so simple. What we need is a for-loop, RegExp object and a match() method. That's it.

Put the text in the text field and start counting its words by clicking the button "count". The text field is created with the textarea element of HTML. The "count" button has the "onclick" attribute and fires the "count()" function.

Now, let's look at the JavaScript code inside the count() function. We get the value inside the "textarea" with the following syntax:

document.getElementById("id of the element").value

We've got the data of the text typed inside the text area and next we want to count the words. So what's a word? To keep it simple, in this example a word is defined as a string containing one or more alphabets or numbers which is expressed by the metacharacter\w+. We want to look at the whole text therefore we use g for a global match. Here is the syntax:

id of the textarea element.match(/\w+/g)

Since we want to look at these word character chunks, we store the statement above in a variable. In the code example it's stored into "output". Now comes the for-loop to count each single chunk. The variable i stands for one chunk and to count them all we use the increment operator (i++). The counting should be continued regardless of the number of words therefore we need an infinite loop described by the statement i<(i+1). The brakets are necessary otherwise 1 is not added to the variable i.

Still, we have one problem. Metacharacters other than words are counted as "undefined". We just want to count the words so we have to get rid of "undefined". The break statement comes in handy. The syntax is as follows:

if (output==undefined){break;}

This statement removes all results marked as "undefined".

HTML and JavaScript Code

Thursday, 9 April 2015

Smiley With Eyes Popping Out

Smiley With Eyes Popping Out
It's always those Sunday afternoons when you are totally bored and have stupid ideas. This time, I've created a smiley face with its eyes popping out when you put your mouse over it.

The head, eyes, black eyes and mouth are all made of div elements. The mouth is made of a circle which is covered by a yellow rectangle creating its moon-like shape.

By changing the width of the eye div elements ("lefteye38", "righteye38") the pop out effect is created. Here is the code for it:

@keyframes name of the animation
{
from{width:20px;}
to{width:50px;left:-40px;}
}

Note that not only the width is changed but also the left property to create the protruding effect. The "mouseenter" event invokes the keyframe animation. You have to write
addEventListener and attachEventListener in order to make the event in all browsers work including IE. Don't forget to write "mouseenter" for addEventListener and "onmouseenter" for attachEvent. The code gets longer and longer because we also want to make the event touchscreen-compatible so we add "touchstart"for "mouseenter" and "touchend" for "mouseleave".

By putting the mouse over the smiley head the animation should be invoked. Here is the code for it:

function name of the function (){
id of the element.style.animation="your animation 1s normal forwards";
}

Always put the addEventListener and attachEvent in the body element otherwise nothing will fire!

HTML, CSS and JavaScript Code

Thursday, 2 April 2015

Drop Down Menus With CSS and JavaScript

Drop Down Menus With CSS and JavaScript
Countries
Last year, I started a blog about travel.
I posted and posted and I had problem:
My landing page got so messy with all this posts.
How do we organize a messy site?
Drop down menu is the answer!

In order to create a drop down menu, you need the addEventListener of JavaScript. In the example above, when you put the mouse pointer on Europe or Asia which are put inside the div element "dropdown37_1" another box including the country names will appear.

Each box is created with a div comprising the country names which are also put inside div elements. Always nest the lists inside the parent element otherwise the event will not function properly.

First, I tried to make 3 parent boxes but it got all jiggly when I put the pointer on the boxes. Only the "dropdown37_1" div element should be visible so the other elements' visibility are set to hidden with "visibility:hidden" in the CSS code. Through the events "mouseenter" and "mouseleave", the elements become visible or invisible.

Here is the syntax for the event:

var x=document.getElementById ("the ID of the affiliated element");

x.addEventListener("mouseenter or mouseleave",
function(){details about what CSS property needs to be changed;},
false};
Much to my chagrin, the code didn't work on my iPod!
Thankfully, the solution was easy. Just add another code replacing "mouseenter" with "touchstart" and "mouseleave" with "touchmove". Maybe it's not the best solution but it did work properly. Well, for all the details check out the code below.

HTML, CSS and JavaScript Code