Pages

Monday, 21 March 2011

Web Accessibility

Today's post will be about a recent presentation on Web Accessibility, conducted by Lucy Dodd. Dodd worked as an accessibility specialist at BBC and therefore has exceptional knowledge on the subject. The presentation tackled some basic difficulties that users might have when using a web site.

I had never given any though of how a website would look to a person with disabilities. The presentation talked about different disabilities that users might have and the difficulties they may encounter. Different methods exist, that web site developers can implement to improve accessibility to these users. In the presentation, different tools help developers achieve these tasks where mentioned. These tools include software that can test web sites for their accessibility. Some of the methods illustrated are quite simple to achieve but sometimes they are not implemented simply because developers are external to these user accessibility problems.

There were several disabilities discussed but they can be categorized into: Auditory, Cognitive and Neurological, Physical, Speech and Visual disorders. Some of the solutions are easy to think of, for example a good choice of colors and large fonts for people with slight vision disorders, and subtitles or video descriptions for users with hearing disorders. Other solutions are harder to think of, especially because you have to know what technologies are available to provide the appropriate accessibility. People with no Vision use text readers which read out the text contained in the websites. If the content of the web site is not well structured, people using text readers may find it difficult to find what they are looking for.

Standards are important when creating a website. The Web Accessibility Initiative (WAI) is a group that provides a list of accessibility standards and Guide lines that should be followed when creating a website. The page http://www.w3.org/WAI/intro/people-use-web/browsing provides a paragraph that describes briefly the software and hardware used to improve interaction with the web. Along with the screen readers which I already mentioned, other technologies can be used. People with low vision may use screen magnifiers such that text can be large enough for them to read. Users who cannot use keyboard or mouse may use voice recognition software and selection switches to allow them to browse through the web. Since these users use different hardware, it is important to make website that work with all types of inputs such that they do not rely on one type of hardware only.

Usage of external technologies such as flash and scripting languages may make pages look better but may also decrease usability for certain users. For example, if text in flash objects is not read by screen readers, people with no vision would not be able to use the website. A lot of animations may also disturb some users, so websites should avoid using to many animations, or provide a way to stop them. The same things apply to the use of sound and music.

Another thing which I had never though of, is the level English used in a website. The content should use plain and simple English such that everyone can understand it. It is not the first time you enter a website and get confused on what to do next. Well to people with lesser knowledge of the English language or to people who have lesser knowledge of computers, orientation in these sites may be even harder.

I think that web developers should be aware of the different difficulties users might have and should also use the different guidelines which can be easily found on the internet. Since web is for every one, it should be also accessible to everyone when possible. For obvious reasons these guidelines are more important to heavily used websites but this dos not mean they should be ignored when creating small websites. This presentation introduced me to an important perspective which I never though of.

Monday, 14 March 2011

XML Files

Introduction

This week I will talk about XML and its uses, but first I will make a brief introduction the XML task. The first part of the task involves creating an XML file that can keep the following information about student projects:
  • Student Name
  • Student ID
  • Project Title
  • Project Category
  • Abstract
  • Date Submitted
The use of both elements and attributes is necessary for this exercise. The XML code must be verified. A DTD schema must be created such that the content of the XML file can be validated through DTD validation.

Introduction to XML and related Types

XML is basically a form of text file that uses a structure to contain information. XML stands for Extensible Markup Language. The reason for this name is that XML unlike HTML has no defined set of tags, you must define your own tags. The possibility of creating customized tags can be used to help define data and thus allow for easier data transmission, validation and interpretation between applications and organisations. If structured correctly, XML can be easily readable even to non computer expert users as XML was designed to be self-descriptive.

XML is a way to structure and send data, but does nothing on its own. You cannot load a piece of XML into the browser and expect the browser to understand it. You can however transfer information between applications that understand the structure of a particular XML file. XML has a tree structure in the sense that it has only one parent element which can have many child elements, which can have their own child elements and so on.

Since XML has allows for the liberty of creating ones own structure, validating the XML file content is not enough when it is used for communication between different systems. The structure must also be verified such that it matches the one expected on the receiving end. This is were DTD kicks in. DTD defines the document structure with a list of allowed elements and attributes. DTD can be placed inside the XML file or linked by an external reference. An alternative to DTD  is the XML Schema. An XML Schema is a description file of a type of XML Document.

The XML Document


1
2
3
4
5
6
7
8
9
10

<?xml version="1.0"?>
<project>
   <student name="Mark" ID="762" >
      <projecttitle>Web Development</projecttitle>
      <projectcategory>IT</projectcategory>
      <projectabstract>usage of Javascript and CSS Styling</projectabstract>
      <submitteddate>2011-12-12Z</submitteddate>
   </student>
</project>

The table above shows the XML structure I choose for this particular exercise. As it can be seen in the table, project is the first element  of the document. The element student is the child of project and contains child elements of its own. Student has two attributes which are; name and ID. Assuming projects are per student, project's title, category, abstract, and submitted date are contained within student. I chose this particular structure because I find it easy to read and understand.

DTD (Document Type Definition)


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

<!DOCTYPE project [
<!ELEMENT project (student)>
<!ELEMENT student (projecttitle,projectcategory,projectabstract,submitteddate)>
<!ELEMENT projecttitle (#PCDATA)>
<!ELEMENT projectcategory (#PCDATA)>
<!ELEMENT projectabstract (#PCDATA)>
<!ELEMENT submitteddate (#PCDATA)>
<!ATTLIST student name CDATA #REQUIRED>
<!ATTLIST student ID CDATA #REQUIRED>
]>


The table above shows the DTD Schema of the XML Document illustrated in the previous section. <!ELEMENT is the tag that describes the elements. It should be immediately followed by the elements name and a pair of brackets. The brackets should contain the name of the sub-elements or the type of data expected. PCDATA stand for parsed character data which means that the text inside the elements will be considered as mark up and any tags inside the text will be expanded. After the declaration of the elements, the attributes need to be declared. <!ATTLIST is the tag used for attributes. The name of the element having the particular attribute is then entered along with the attribute name. In this case CDATA is used. This type of data is threated as normal characters and is not parsed. The text #REQUIRED imply that the element is required and must be included. The DTD was tested with a validator offered by w3schools.com.

XML Schema


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
   <xs:element name="project">
        <xs:complexType>
             <xs:sequence>
                  <xs:element name="student" >
                       <xs:complexType>
                           <xs:sequence>
                               <xs:element name="projecttitle" type="xs:string"/>
                               <xs:element name="projectcategory" type="xs:string"/>
                               <xs:element name="projectabstract" type="xs:string"/>
                               <xs:element name="submitteddate" type="xs:date"/>
                           </xs:sequence>
                           <xs:attribute name="name" type="xs:string" use="required" />
                           <xs:attribute name="ID" type="xs:string" use="required"/>
                       </xs:complexType>
                  </xs:element>
              </xs:sequence>
        </xs:complexType>
   </xs:element>
</xs:schema>


In Addition to the DTD, I created the respective XML Schema. XML Schema is a more powerful way to define your XML document structure and limitations than DTD. These schemas are XML documents themselves which reference the XML Schema Namespace, and even have their own DTD. They improve from DTD as they also check for a wider range of element types which are common to todays programming languages. Writing this type of schema is slightly more difficult and results in a larger volume of code than DTD.  The schema shows that an element project of type complex-type contains an element student. this element contains, a sequence of other elements and two attributes. 'use="required"' is used instead of #REQUIRED.  On line 15 the element submitteddate is declared as date. The date is accepted in yyyy-mm-dd format. In line 8 in the XML file, the submitted date is 2011-12-12Z, where Z shows that the time is UTC time. The XML schema and XML Document was tested at http://xmltools.corefiling.com/schemaValidate/.

Conclusion

Both DTD and XML Schema are good validators that can verify the correct structure of your XML file. An important factor in the choice of which technology to use, is obviously the type of validation required. In certain situations DTD could not test for all the requirements and thus should be avoided. On the other hand DTD could avoid some of the complication provided by the XML schema and can be ideal for some situations.

Thursday, 10 March 2011

Running man in JavaScript

Introduction to The Task

Hello again and welcome to this week's blog post. As hinted in the title the task for this week involves creating an animation of a running man. The animation must be able to change speed and on the click of a button (or other event) an object must be thrown at the runner. Adding to this task, I decided to include some other features of my own.

Creating The Animation

To start the animation, I used an already written script found in the notes. In this script, images were loaded into an array, and the array was then looped at small time intervals making the image appear moving. The animation is triggered by a button called run. The code provided had a small bug that when the run button is hit more than once, the animation would become unstoppable. The first thing I did to avoid this error is to disable the run button once the running process begins.

For the animation, I found an interesting free GIFF image which I downloaded and converted into a sequence of PNG images using an online tool at http://www.online-image-editor.com/. I used the images to create a running man animation. I also created a copy of these images and altered them to create the second animation for throwing an object. Finally to allow the throw object animation, I created a button called throw that alters a variable that controls the looping process. More exactly I made a loop to run through the first 8 images only so that the man in the animation appears to be running, but when the throw button is clicked, the animation loop continues till the last image showing the final part of the animation. The throw animation involves the runner avoiding a tin can thrown in the direction of his head and passing above a man hole left opened.

The final part of the exercise involves altering the running speed of the animation. The speed can be altered simply by changing the time interval between the sequence of images being displayed. To get the chosen speed, I used the HTML select input such that the user can select from a list of different speeds. Speed can also be altered when an animation is already running. To alter the speed animation using the pre-existing code, the animation must be stopped, and restarted with the new time interval. Therefore when speed is changed, the looping process stops and restarts automatically. This process is transparent to the user.


Additional Features

The first feature I wanted to add was a slider or track bar, that tracks the position of the animation. At first I used the Range input to do this, but I soon realized, that this input type is only available in HTML5 and works only in chrome. Instead of giving up, I decided to create my own range input using purely HTML and JavaScript.  First I created images for the slider and slider piece. 

Then I researched how to do drag and drop such that I could slide the blue slider piece where I wanted. I found Several code for drag and drop capabilities and after I finished my little research I started creating my own code that allowed the slider piece to only move horizontally and only in a particular range.

The first thing to do is to determine what inputs does the browser support. Some browsers do not support all the features or use different syntax while other browsers might understand all the commands or react differently to the same command. To avoid unexpected behavior, I used a boolean value to determine which functions I should use in the particular browser. Using the bool variable nn6 as shown below, the script would choose the appropriate functions to run  based on the the support of the document.getElementById or document.all.
var nn6=document.getElementById&&!document.all;

Another variable is used to determine whether the blue slider piece is being dragged or not. The onmouse down event triggers a function that sets the variable to true and calls another function which I called moveMouse. This function determines the position of the mouse and verifies that it is within the slider range. If the mouse goes out of range the slider piece stops mooving. OnMouse down event the dragging variable to false and the dragging object to null, such that the slieder piece is no longer effected by the mouse movement. for the  code to work the piece image must have an absolute position in the CSS style.

Adding to the drag and drop features I  included another function that allows clicking on any part of the slider range, to obtain the image at that position. This feature will be later explained in this blog. While running, the piece slider is also moved to show the position of the current runner position during the animation.

Since I was dealing with time and animation positions, I decided to use cookies such that the browser remembers the animation position of the runner when the user clicks stop and closes the browser. I found the code to create cookies on http://www.w3schools.com/   but the code was not working or at least I tough so. Then I found out that my code works perfectly, but chrome does not allow local cookies to be saved, so I had to test my page on a web server. When the browser starts, it searches for the animation position in the cookies. If found, the animation starts from that position even if that position is part of the throw object animations.

Finally I added up some sound of footsteps to the animation. Inserting an embed element to the html, using Java script does the trick. Not all browsers support music natively so I added support to the Windows media player plugin for firefox. This plugin works also on chrome, and allows sounds to play on a wider range of browsers. Note that browsers that do not support this plugin, but support the format of the sound natively, can also  play the footsteps. To add support to the firefox plugin, one simply needs to add  type='application/x-mplayer2' as Embed type. The embed type adds a windows media player to the page which allows users to control the music, but in this case the media player is invisible. The music starts because the autostart value of the embed object is set to true

Determining Exact Mouse Position

To determine positioning of objects, I used  several techniques to get the x coordinates of the objects involved. An important part of the code is from line 176 to 178, as that code determines whether the mouse is within the slider range. It does that by comparing the x coordinates of the slider and slider piece. It also compares the slider piece's x coordinate with the slider x coordinate plus its width.


Clicking on the Slider Image

When a mouse click is detected on the slider image, the function below is triggered. Line 144 determines the position of the blue slider piece from the slider' left offset position, which is its x coordinates. In the following lines, the width of the slider is divided by the number of possible image positions (excluding position 0) to get the distance between each position. The current piece position is divided by the previous calculation, to obtain a rounded integer that gives the image number to display corresponding to the area clicked. Finally line 149 sets the piece position to the clicked area.



Piece Image Movement while the Animation is Running

The function marathon is executed repeatedly in time intervals, to obtain an animation. To explain briefly, the code below increments the image number every time it is executed. In the case the last image is displayed, the image number is reset to the first. The piece position is calculated based on the image number in this case stored in curRunner. This is done by finding the distance between each position and multiplying it to the number of positions. To find the actual left margin the the piece position works on, the x coordinates of the slider image must be added to the previous calculation's answer. The calculation is shown in lines 136 to 138.



Final Corrections

After I implemented all the features mentioned above I needed to correct some minor errors which started to bother me. The first problem occurred on window resizing. Since the piece position is absolute, when the screen size changed, all elements changed positions, but the slider piece's position remained the same. To avoid this error I added the windowOnResize and page onload events such that when they are triggered, the piece position gets recalculated. I also corrected some piece positions like in line 149 by decreasing or increasing the left margin of the piece element. In this case I added -4.

When I uploaded the site to an actual web server, Images started to load slowly. I found a method that solves this problem. I loaded images in memory by using new Image(254,266) method which creates an image with the given dimensions. Then after creating the image I assigned the actual image path to the image array. This was all done when the page is loaded, such that on page load, all images are already cached in memory.

To avoid errors some buttons are disabled, and enabled depending on the processes running. This feature can be seen in the screenshots.

Some Screenshots


You can try the animation at http://mark-galea.webs.com/ or download it from http://www.megaupload.com/?d=08M78QK0.

Wednesday, 2 March 2011

Introduction to JavaScript

Introduction

A week has passed since my first posts and it is now time to blog about my experiences in the second week on my journey in web application development. For this week’s task we were given a Web page that contains JavaScript that acts as a Loan calculator. Our task consists in altering and adding up to the already existing script to add features to the Loan Calculator. Improving the visuals using CSS style sheets is also part of the exercise.

In the task provided, the use of the website http://jsfiddle.net/ was suggested as a working platform. This website creates a developing environment which takes in HTML, CSS and JavaScript to output the resulting webpage. The HTML, CSS and JavaScript are inputted in separate boxes and not as a single HTML file. I tried using JFiddle and did most of the JavaScript there, but I found using a text editor being better. However the idea of separating the code seemed very good therefore I separated my code into different files.

Task Development and Explanation

It was not the first time that I had peeked into some JavaScript but I had never written anything in it. For starter, I downloaded a good text editor called “Programmer’s Notebook” that can highlight keywords for many different languages. This way it is much easier to create code and find errors.

The Loan Calculator provided takes in the amount of the loan, the annual percentage rate of interest and the repayment period in years as input. The monthly payment, total payment, and the total interest paid are the results of the Calculation.

As part of the exercise we had to add the number of payments and the date when the loan ends as Outputs together with a checkbox that adds discount to the formula. Well the number of payments is easy to achieve since it only involves multiplying the payment period by the number of months in a year. The date calculation is a little trickier. Assuming the starting date is the date the computation occurs, I added up the number of months to the current date by using the formula in line 58. To make it more interesting I assumed that the repayment period in years could be a decimal. I used Math.ceil() such that if the number of payments is not a whole number, it would be converted into the next bigger Integer available. The Reason for using Math.ceil instead of Math.round is because payments are usually done in the end of the month and the extra days are considered as another month. Lines 60 and 61 show how I managed to output the date in short Date format.

Next in line was the discount. To compute the discount, I simply deduced the total discounted payment from the total undiscounted payment. The final part of the exercise suggested that each student creates a feature of his own. Since I wanted to learn JavaScript starting from the basics, I created a new textbox that takes in the amount of discount. This input box is enabled and disabled depending on the checked property of the checkbox controlling the discount. The discount cannot be greater than 50%. If so an alert is triggered. When the Discount checkbox is disabled, no discount is computed and the discount box is disabled and also cleared. Finally I downloaded a CSS template as a reference and I used it to make the webpage look alike while adding a touch of my own.


Final Comments

JavaScript is a language that uses syntax similar to other popular languages but its structure is different. It can be quite difficult to manage due to the way it manages variables. In fact in JavaScript variable types are not declared making it very easy to get confused. Another disadvantage is that JavaScript is downloaded and executed on the client’s side and therefore it is unsafe as it can be easily altered from the receiving end. On the other hand JavaScript can help create a better user experience.