Thursday, 23 June 2011

Week 18 - Mobile Phone Web Development

Today we continued on HTML5, but this time for mobile phones, where we discussed on the evolution of mobile devices. In today's lab session we tried geolocation on our mobile phones, to check if our mobile supports geolocation or not.

Mobile Device Evolution

  • Basic Mobile Phones - used only to call someone and send SMS
  • Low-end Mobile Devices - have a poor web browser support, memory was limited, no touch support and some of then had a basic camera or music player
  • Mid-end Mobile Devices - have a basic HTML-browser support, sometimes they have 3G, camera is better, music player, application support and games
  • High-end Mobile Devices - Have advanced features, camera have a higher mega pixel, have Bluetooth and also have a good web support
  • Smartphones - full browser and html support, Wi-Fi supprt, 3G or 4G, music player GPS, Bluetooth, accelerometer and touch support
  • Non-phone Devices - Wi-Fi support, browsers and other features

Browsing Types

  • Focus Navigation - In focus navigation, the user will use the cursor keypad to scroll the website and navigate between the links.
  • Cursor Navigation - In cursor navigation, there will be a mouse cursor on the screen which can be moved using arrow keys.
  • Touch Navigation - In touch navigation, the user can navigate either using a finger or using a stylus. When fingers will be used, it might not he that precise. These devices allow the user to gestures that the device can detect to perform actions easily.
  • Multitouch Navigation - In multitouch navigation, users may select more than one item at the same time.

Geolocation

Geolocation identifies an object's geographic location, which can be a radar, a computer terminal connected to the Internet or a mobile phone. Since this lesson was about mobile web development, I will be testing if geolocation works on my Nokia 5800 mobile.

The code below shows one way of geolocation using javascript. The userLocated() function is used to get the position of the device. Line 9 is used to get the latitude coordinated while line 10 is used to get the longitute coordinates. Line 11 is used to get the timestamp of that location. The locationError() function is used so that if there will be an error, an alert of the error will appear.
1:  <html>  
2:  <head>  
3:    <title>Geolocation</title>  
4:  </head>  
5:  <body>  
6:  <script type="text/javascript">  
7:  navigator.geolocation.getCurrentPosition(userLocated, locationError)  
8:  function userLocated(position){  
9:     var lat = position.coords.latitude;  
10:    var long = position.coords.longitude;  
11:    var timeOfLocation = position.timestamp;  
12:    document.write(lat + "<br/>");       
13:    document.write(long + "<br/>");  
14:    document.write(timeOfLocation);   
15:  }  
16:  function locationError(error){  
17:    alert(error.code);  
18:  }  
19:  </script>  
20:  </body>  
21:  </html>  
Geolocation Coordinates
At first I saw nothing on my mobile's browser, but then I installed the opera browser on my mobile and as shown in the above image have been shown on my mobile. The first number (35.9164085) is the latitude, the second number (14.4811542) is the longitude and the third number (1308932361589) is the time-stamp. I tried out another example of geolocation found in www.html5demos.com/geo where the location was found by showing you a map (see image below). This was found using the IP address, where the location was not that accurate.
Geolocation Map

Conclusion

Today's lesson was a very interesting one. Next week our lecture will be on Facebook development.

Saturday, 18 June 2011

Week 17 - HTML5, CSS3 & Javascript

As discussed in my last post, this week we started a new topic that is HTML5, CSS3 and Javascript, that is, the future.

HTML5

HTML5 is the next version of HTML, where new features have been added to replace scripting. HTML5 is not yet official and although browsers still do not have full HTML5 support, new HTML5 features are added in their latest versions. Some of the new features are:
  • A canvas tag have been added for drawing
  • A video and audio tag have been included for media
  • Content specific tags have been included such as header, footer, article, section and nav
  • New form controls have also been added such as calendar, time, date, email and search

<video> & <audio> Tags

To embed a video, an external plug-in such as Flash are used in HTML. In order to reduce the need for these plug-ins, the video and audio tags have been added to HTML5. To embed an audio and a video the following code is added, where the format I used for audio is '.mp3' (line 5) and the format I used for video is '.mp4' (line 2).
1:  <video width="320" height="240" controls="controls">    
2:     <source src="c.mp4" type="video/mp4" />  
3:  </video>  
4:  <audio width="320" height="50" controls="controls">    
5:     <source src="ab.mp3" type="audio/mp3" />    
6:  </audio>  
Output audio and video tag

When I tested out in Google chrome, they worked perfectly, but in Internet Explorer and Mozilla Firefox, these did not work. This might be the case that both Internet Explorer and Mozilla Firefox still do not support these tags.

Form controls

In the form tag, new input types have been added in HTML5 such as date, email, search and so on. With the use of these input types, there is more validation in the form, that is scripting for validation is also reduced. The following code shows some of these input types:

1:  <input class="fstyle" name="rname" type="text" size="20" required>  
2:  <input class="fstyle" name="remail" type="email" size="20" required>  
3:  <input type="date" min="1990-08-14" max="2011-06-14" value="1990-08-14"/>   
4:  <input type="text" x-webkit-speech />    

In lines 1 & 2, the form attribute required is added. This is used so that if the user leaves the text field empty and the submit button is clicked, a message that the field is empty will be displayed near the text field. In line 2, the e-mail input type is used. When using this input type, if the user enters something in the field that does not contain '@' and '.com', a message that it is not a valid email address will be displayed when the user clicks on the submit button. Another thing I found out is that there is also speech recognition using the text input type and x-webkit-speech as shown in line 4. I tried it out but it still needs improvement as I tried alot of words through the microphone but the output was not as expected.

Form Controls

CSS3

CSS3 is the latest standard of CSS, where new features have been added, to make the styling of the web page more interesting.

Text Effects

word-wrap and text-shadow are two new features in CSS3. In the word-wrap text property, the text will wrap, that is if the word is too long, it also splits in the middle of a word. The text-shadow text property specifies the horizontal shadow, the vertical shadow, the blur distance and the color of the shadow.
1:  <html>  
2:  <head>  
3:  <style type="text/css">  
4:  h1  
5:  {  
6:    font-family: arial;  
7:    text-shadow: 2px 1px 1px #0000FF;  
8:  }  
9:  </style>  
10:  </head>  
11:  <body>  
12:  <h1>Stephanie Vella</h1>  
13:  </body>  
14:  </html>  
In the above code, line 7 shows the text-shadow property, where the horizontal shadow is 2px, the vertical shadow is 1px, the blur distance is 1px and the color of the shadow is #0000FF (blue).
Text Shadow Output 

Animation

Also in CSS3 one can create animations using the @keyframes rule. In the code below, the initial background colour will be blue (line 8). Lines 19 - 22 shows that at 25% the colour will be turned into green, at 50% the colour will be turned into yellow and at 100% the colour will be turned into red, using the @keyframes rule. It will take 10 seconds to change from one color to the other using the animation property (line 9). Web browsers Safari and Chrome require the prefix -webkit.
1:  <html>  
2:  <head>  
3:  <style type="text/css">   
4:  div  
5:  {  
6:     width:185px;  
7:     height:60px;  
8:     background: blue;  
9:     animation:myan 5s;  
10:    -webkit-animation:myan 5s;   
11:  }  
12:  h1{  
13:    font-family: arial;  
14:    font-size:20px;  
15:    padding:20px;  
16:  }  
17:  @keyframes myan   
18:  {  
19:    0%  {background:blue;}  
20:    25% {background:green;}  
21:    50% {background:yellow;}  
22:    100% {background:red;}  
23:  }  
24:  @-webkit-keyframes myan   
25:  {  
26:    0%  {background:blue;}  
27:    25% {background:green;}  
28:    50% {background:yellow;}  
29:    100% {background:red;}  
30:  }  
31:  </style>  
32:  </head>  
33:  <body>  
34:    <div><h1> Stephanie Vella<h1></div>  
35:  </body>  
36:  </html>  
Animation

Selectors

Selectors
Selectors in CSS are used to select which elements should be styled. For example in the following code line 4 selects every ol element that is preceded by a p element, therefore lines 17-22 will have a background colour of #9263fa.
1:  <html>  
2:  <head>  
3:  <style type="text/css">   
4:  p~ol  
5:  {  
6:    background:#9263fa;  
7:    width:60px;  
8:  }  
9:  </style>  
10:  </head>  
11:  <body>  
12:  <ol>  
13:   <li>Java</li>  
14:   <li>PHP</li>  
15:   <li>ASP</li>  
16:  </ol>  
17:  <p>List</p>  
18:  <ol>  
19:   <li>Java</li>  
20:   <li>PHP</li>  
21:   <li>ASP</li>  
22:  </ol>  
23:  </body>  
24:  </html>  
For more selectors one can look at http://www.w3schools.com/css3/css3_ref_selectors.asp

Javascript

There are also new features added in Javascript, such as web storage, web SQL storage, Offline Application cache, web workers and web sockets. I will be discussing on two new features; Web Storage and Web SQL Database Storage.

Web Storage

Web storage is an improvement of cookies, but falls under client-side scripting. The following code shows the count of how much you have visited that page. If the page will be refreshed, the counter will increase. Also if you close the browser and open again, the counter will continue. This is done using the localStorage object, where data is stored having no time limit. When using sessionStorage object, data will only be stored for one session.
1:  <!DOCTYPE HTML>  
2:  <html>  
3:  <body>  
4:  <script type="text/javascript">  
5:  if (sessionStorage.visitcount)  
6:       {  
7:       sessionStorage.visitcount=Number(sessionStorage.visitcount) +1;  
8:       }  
9:  else  
10:       {  
11:       sessionStorage.visitcount=1;  
12:       }  
13:  document.write("Visited " + sessionStorage.visitcount + " times");  
14:  </script>   
15:  </body>  
16:  </html>  

Web SQL Database Storage

Another feature of Javascript in HTML5 is Web SQL Database Storage, where the database can be accessible from JavaScript. The code below is used to create a new table named 'Table1Test', creating a new record, update records, delete records and to delete a table.
1:          function createTable() {  
2:          db.transaction(function(tx) {  
3:           tx.executeSql("CREATE TABLE Table1Test (id REAL UNIQUE, text TEXT)", [],  
4:             function(tx) { log.innerHTML = '<p>"Table1Test" created!</p>' },  
5:             onError);  
6:          });  
7:         }  
8:         function newRecord() {  
9:          var num = Math.round(Math.random() * 10000); // random data  
10:          db.transaction(function(tx) {  
11:           tx.executeSql("INSERT INTO Table1Test (id, text) VALUES (?, ?)", [num, document.querySelector('#todoitem').value],  
12:             function(tx, result) {  
13:              log.innerHTML = '';  
14:              showRecords();  
15:             },   
16:             onError);  
17:          });  
18:         }  
19:         function updateRecord(id, textEl) {  
20:          db.transaction(function(tx) {  
21:           tx.executeSql("UPDATE Table1Test SET text = ? WHERE id = ?", [textEl.innerHTML, id], null, onError);  
22:          });  
23:         }  
24:         function deleteRecord(id) {  
25:          db.transaction(function(tx) {  
26:           tx.executeSql("DELETE FROM Table1Test WHERE id=?", [id],  
27:             function(tx, result) { showRecords() },   
28:             onError);  
29:          });  
30:         }  
31:         function dropTable() {  
32:          db.transaction(function(tx) {  
33:           tx.executeSql("DROP TABLE Table1Test", [],  
34:             function(tx) { showRecords() },   
35:             onError);  
36:          });  
37:         }  
Transactions are used to rollback, that is, if a transaction fails the updates will not be done, it will be as if the transaction never happened. The '?' are used as placeholders where it will get the data depending on the user input. For example in line 26, if we want to delete the record 'CMT3313', it will check for the id and the '?' will be replaced with '7892'. Line 3 shows the statement to create the table, line 21 shows the statement to update the table, line 26 shows the statement to delete from table and line 33 shows the statement to drop the whole table. In the newRecord() function (lines 8-18), records with random values will be added to the tables, and the insert statement will be used as shown in line 11.

Web SQL database
Chrome's Developer Tools

Conclusion

As you can see, although HTML5 and CSS3 are not yet fully released, one can still see how alot of work will be reduced with these new features, and I am sure that as time goes by more interesting new features will be added. If you want to know more about HTML5, CSS3 and Javascript, you can go to this link http://slides.html5rocks.com, which consists of a presentation that have been created using HTML5.

Monday, 13 June 2011

Week 16 - Linden Scripting Language 3

This week was the last week where we talked on second life. We concluded this topic and discussed what we could to in the next weeks. In this post I'll be discussing three scripts that can be used in second life: teleporting, sending email and loading URL.

Teleporting

First, the target coordinates have been inserted, using a variable of type vector as shown in line 1. The llSetText() function shown in line 4 is used to display text on the object, where "Teleport" is the text displayed and <0,1,0> is the text colour (where in this case, it is green). The llGetRot() function will return the rotation of the avatar. The llSitTarget() function consists if a vector offset and rotation. In this case I did not want the avatar to sit down after teleporting; therefore the llUnSit() function shown in line 9 is used.
1:  vector target=<200,200,27>;  
2:  default{  
3:    state_entry(){  
4:      llSetText("Teleport",<0,1,0>,1);  
5:      llSetSitText("Teleport");  
6:      llSitTarget(target * (ZERO_ROTATION / llGetRot()),ZERO_ROTATION / llGetRot());  
7:    }  
8:  changed(integer change){  
9:    llUnSit(llAvatarOnSitTarget());  
10:    }  
11:  }  

Sending Email

The following script is used so that when the avatar touches the object, an email will be sent to me:
1:  string email_address = "vella.stevie@gmail.com";   
2:  default  
3:  {  
4:    state_entry() {  
5:      llEmail( email_address, "Second life", "Testing email" );  
6:    }  
7:    touch_start( integer num_detected ) {  
8:      integer i = 0;  
9:          do  
10:      llEmail( email_address, "Object Sender", llDetectedName(i) + "touched the object" + "\nKey: " + (string) llDetectedKey(i) );  
11:      while(++i < num_detected);  
12:    }  
13:  }  
In line 1, a variable of type string is declared, where the email address of the receiver will be typed, in this case, it is my email address. The llEmail() function, shown in line 5 consists of the email address, the subject and message. This will be sent to me and the subject is "Second life" and the message is "testing. This will be sent each time a new state is entered.
First Email
When the object is touched, another email will be sent, but this time in the message, it will show you who touched the object, using the llDetectedName() function and also returns the key of the object, using the llDetectedKey() function.
When Object is Touched

Loading URL

Another thing which you can do in second life is interacting with the web, using the llLoadURL() function when the avatar touches the object. When using this function a dialog will be shown to the avatar, in this case to load the web page http://stevie-89.blogspot.com, which is this blog with message "view stevie's blog".
1:  default  
2:  {  
3:    touch_start(integer total_number) {  
4:    llLoadURL(llDetectedKey(0), "view stevie's blog", "http://stevie-89.blogspot.com/");  
5:    }  
6:  }  
Loading URL

Conclusion

In the next weeks new topics will be introduced in class. These new topics will be HTML5, JavaScript, CSS3, mobile web development, facebook development, custom PHP libraries, cloud computing and web security.

Sunday, 5 June 2011

Week 15 - Linden Scripting Language 2

This week, more Linden Scripting Language have been done during the lecture, where the different types of variables have been explained and more examples on what scripting can be done in second life was given.

Variables

Linden Scripting language is a strongly typed language; variables must be declared by type. The following are the types if variables used in LSL:
  • Integer - is a whole number
  • Float - is a decimal number
  • String - is a sequence of characters
  • Key - is used to identifysomething in second life
  • Vector - are three floats that are in the form <x, y, z>
  • Rotation - is made up of four floats <x, y, z, s>
  • List - a list of other data types that are heterogeneous

Counter

A simple task I did to experiment with variables is creating a counter, where variable cnt is being used:
1:  integer cnt;  
2:  default  
3:  {  
4:    state_entry()  
5:    {  
6:      cnt = 0;  
7:    }  
8:    touch_start(integer total_number)  
9:    {  
10:      cnt ++;  
11:      llSay(0, "Count: " + (string)cnt);      
12:    }  
13:  }  
In the above code, the initial value of the counter is set to zero as shown in line 6. In the state_entry() function, the variable cnt is set to zero and each time the object will be touched, the touch_start() function will be called. The variable will then be inecremented and displayed. In order to display the variable, type casting is used to convert to string. Below shows the result when the object will be touched.

Sitting

I also built a static object which is a chair that allow the avatar to sit properly. That is, instead of 'right click' and and select the 'Sit Here' command, a script have been created. Three variables of a float type as shown in lines 1-3 have been declared, where together they make up a vector. The llSitTarget() function is used to make the avatar sit down, where it consists of a vector offset and rotation (line 8). The llSetSitText() function is used to display an alternative text insted of the default 'Sit Here', when you right click. At first the avatar was not sitting properly one the chair, but after a while playing around, I managed to make the avatar sit properly. One thing I had to change is the values of the x, y and z. When the value of x is increased or decreased, the avatar sits on the object forward or backwards. When the value of y is increased or decreased, the further left or right the avatar sits on the object. When the value of z is increased or decreased, the avatar sits higher or lower on the object. Another thing I had to do was to rotate the avatar to 90 °, where I had to use the llEuler2Rot() function as shown in line 8.
1:  float x = 0;                 
2:  float y = 0.3;   
3:  float z = 0.3;  
4:  default  
5:  {  
6:    state_entry()  
7:    {  
8:      llSitTarget(<x,y,z>,llEuler2Rot(<0,0,90> * DEG_TO_RAD));  
9:      llSetSitText("Sit Av");  
10:    }  
11:  }  
Avatar Sitting