Friday, March 15, 2013
Sunday, February 10, 2013
HTML5 Video
Until now, there has not been a standard for showing a video/movie on a web page.
Today, most videos are shown through a plug-in (like flash). However, different browsers may have different plug-ins.
HTML5 defines a new element which specifies a standard way to embed a video/movie on a web page: the <video> element.
Browser Support
Internet Explorer 9, Firefox, Opera, Chrome, and Safari support the <video> element.
Note: Internet Explorer 8 and earlier versions, do not support the <video> element.
HTML5 Video - How It Works
To show a video in HTML5, this is all you need:
Example
<video width="320" height="240" controls>
<source src="movie.mp4" type="video/mp4">
<source src="movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>
<source src="movie.mp4" type="video/mp4">
<source src="movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>
Try it yourself »
The control attribute adds video controls, like play, pause, and volume.
It is also a good idea to always include width and height attributes. If height and width are set, the space required for the video is reserved when the page is loaded. However, without these attributes, the browser does not know the size of the video, and cannot reserve the appropriate space to it. The effect will be that the page layout will change during loading (while the video loads).
You should also insert text content between the <video> and </video> tags for browsers that do not support the <video> element.
The <video> element allows multiple <source> elements. <source> elements can link to different video files. The browser will use the first recognized format.
Video Formats and Browser Support
Currently, there are 3 supported video formats for the <video> element: MP4, WebM, and Ogg:
| Browser | MP4 | WebM | Ogg |
|---|---|---|---|
| Internet Explorer 9+ | YES | NO | NO |
| Chrome 6+ | YES | YES | YES |
| Firefox 3.6+ | NO | YES | YES |
| Safari 5+ | YES | NO | NO |
| Opera 10.6+ | NO | YES | YES |
- MP4 = MPEG 4 files with H264 video codec and AAC audio codec
- WebM = WebM files with VP8 video codec and Vorbis audio codec
- Ogg = Ogg files with Theora video codec and Vorbis audio codec
MIME Types for Video Formats
| Format | MIME-type |
|---|---|
| MP4 | video/mp4 |
| WebM | video/webm |
| Ogg | video/ogg |
HTML5 <video> - DOM Methods and Properties
HTML5 has DOM methods, properties, and events for the <video> and <audio> elements.
These methods, properties, and events allow you to manipulate <video> and <audio> elements using JavaScript.
There are methods for playing, pausing, and loading, for example and there are properties (like duration and volume). There are also DOM events that can notify you when the <video> element begins to play, is paused, is ended, etc.
The example below illustrate, in a simple way, how to address a <video> element, read and set properties, and call methods.
Example 1
Create simple play/pause + resize controls for a video:
The example above calls two methods: play() and pause(). It also uses two properties: paused and width.
Try it yourself »
For a full reference go to our HTML5 Audio/Video DOM Reference.
HTML5 Video Tags
| Tag | Description |
|---|---|
| <video> | Defines a video or movie |
| <source> | Defines multiple media resources for media elements, such as <video> and <audio> |
| <track> | Defines text tracks in mediaplayers |
Saturday, February 9, 2013
Drag and Drop In HTML 5
Drag and Drop
Drag and drop is a very common feature. It is when you "grab" an object and drag it to a different location.
In HTML5, drag and drop is part of the standard, and any element can be draggable.
Browser Support
Internet Explorer 9, Firefox, Opera 12, Chrome, and Safari 5 support drag and drop.
Note: Drag and drop does not work in Safari 5.1.2.
HTML5 Drag and Drop Example
The example below is a simple drag and drop example:
Example
<!DOCTYPE HTML>
<html>
<head>
<script>
function allowDrop(ev)
{
ev.preventDefault();
}
function drag(ev)
{
ev.dataTransfer.setData("Text",ev.target.id);
}
function drop(ev)
{
ev.preventDefault();
var data=ev.dataTransfer.getData("Text");
ev.target.appendChild(document.getElementById(data));
}
</script>
</head>
<body>
<div id="div1" ondrop="drop(event)"
ondragover="allowDrop(event)"></div>
<img id="drag1" src="img_logo.gif" draggable="true"
ondragstart="drag(event)" width="336" height="69">
</body>
</html>
<html>
<head>
<script>
function allowDrop(ev)
{
ev.preventDefault();
}
function drag(ev)
{
ev.dataTransfer.setData("Text",ev.target.id);
}
function drop(ev)
{
ev.preventDefault();
var data=ev.dataTransfer.getData("Text");
ev.target.appendChild(document.getElementById(data));
}
</script>
</head>
<body>
<div id="div1" ondrop="drop(event)"
ondragover="allowDrop(event)"></div>
<img id="drag1" src="img_logo.gif" draggable="true"
ondragstart="drag(event)" width="336" height="69">
</body>
</html>
It might seem complicated, but lets go through all the different parts of a drag and drop event.
Make an Element Draggable
First of all: To make an element draggable, set the draggable attribute to true:
<img draggable="true">
What to Drag - ondragstart and setData()
Then, specify what should happen when the element is dragged.
In the example above, the ondragstart attribute calls a function, drag(event), that specifies what data to be dragged.
The dataTransfer.setData() method sets the data type and the value of the dragged data:
function drag(ev)
{
ev.dataTransfer.setData("Text",ev.target.id);
}
{
ev.dataTransfer.setData("Text",ev.target.id);
}
In this case, the data type is "Text" and the value is the id of the draggable element ("drag1").
Where to Drop - ondragover
The ondragover event specifies where the dragged data can be dropped.
By default, data/elements cannot be dropped in other elements. To allow a drop, we must prevent the default handling of the element.
This is done by calling the event.preventDefault() method for the ondragover event:
event.preventDefault()
Do the Drop - ondrop
When the dragged data is dropped, a drop event occurs.
In the example above, the ondrop attribute calls a function, drop(event):
function drop(ev)
{
ev.preventDefault();
var data=ev.dataTransfer.getData("Text");
ev.target.appendChild(document.getElementById(data));
}
{
ev.preventDefault();
var data=ev.dataTransfer.getData("Text");
ev.target.appendChild(document.getElementById(data));
}
Code explained:
- Call preventDefault() to prevent the browser default handling of the data (default is open as link on drop)
- Get the dragged data with the dataTransfer.getData("Text") method. This method will return any data that was set to the same type in the setData() method
- The dragged data is the id of the dragged element ("drag1")
- Append the dragged element into the drop element
Content Courtesy: http://www.w3schools.com
Monday, January 21, 2013
Classes Are Blueprints for Objects
A class describes the behavior and properties common to any
particular type of object. For a string object (in Objective-C, this is
an instance of the class
In the same way that multiple buildings constructed from the same blueprint are identical in structure, every instance of a class shares the same properties and behavior as all other instances of that class. Every
Any particular object is designed to be used in specific ways. You might know that a string object represents some string of characters, but you don’t need to know the exact internal mechanisms used to store those characters. You don’t know anything about the internal behavior used by the object itself to work directly with its characters, but you do need to know how you are expected to interact with the object, perhaps to ask it for specific characters or request a new object in which all the original characters are converted to uppercase.
In Objective-C, the class interface specifies exactly how a given type of object is intended to be used by other objects. In other words, it defines the public interface between instances of the class and the outside world.
NSString), the class offers
various ways to examine and convert the internal characters that it
represents. Similarly, the class used to describe a number object (NSNumber) offers functionality around an internal numeric value, such as converting that value to a different numeric type.In the same way that multiple buildings constructed from the same blueprint are identical in structure, every instance of a class shares the same properties and behavior as all other instances of that class. Every
NSString instance behaves in the same way, regardless of the internal string of characters it holds.Any particular object is designed to be used in specific ways. You might know that a string object represents some string of characters, but you don’t need to know the exact internal mechanisms used to store those characters. You don’t know anything about the internal behavior used by the object itself to work directly with its characters, but you do need to know how you are expected to interact with the object, perhaps to ask it for specific characters or request a new object in which all the original characters are converted to uppercase.
In Objective-C, the class interface specifies exactly how a given type of object is intended to be used by other objects. In other words, it defines the public interface between instances of the class and the outside world.
Thursday, August 2, 2012
50 characteristics of a great software developer
- Passionate; loves computers and programming, takes an interest and thinks about things even outside working hours.
- Curious; wants to understand new things, researches unfamiliar terms.
- Humble; recognizes that other people are smart and have great ideas and knowledge, respects relationships more than technology.
- Creative; sees ways to do things that others don’t see, comes up with better ways of doing things, goes beyond.
- Friendly; easy to get along with, does not sabotage or bring down team morale.
- Fast learner; can quickly research, understand and use unfamiliar software technologies, tools and languages.
- Focus; works towards completion of tasks with minimal distraction, avoids taking tangents.
- Comprehension; can make sense of software requirements and understand what it is that needs to be built, able to grasp the “mental model” of the internal structure of a software application.
- Logic skills; ability to devise logical solutions for programming problems.
- Pragmatic; able to make a value judgement about what is really important, values practical outcomes and getting the job done, avoids gold plating.
- Not dogmatic; willing to change their mind and see things from the perspective of someone else, values the intellect of others. Not a jerk.
- Workman like; willing to do the drudge work as well as the exciting work.
- Thorough; puts in the 10% more needed to do a great job rather than an adequate job.
- Intellect; able to grasp very complex computing concepts, able to develop very sophisticated code, able to do “the hard stuff”.
- Energy; productive, motivated, strong work ethic, gets a lot of work done in the available working time.
- Practices; writes lots of code, especially in the early years.
- Persistence; sticks at it, takes the time needed to get something done or to learn something new.
- Flexible; adaptable, happy to take new directions, happy to work with new technologies, happy to try new things, happy to change priorities.
- Thirst for knowledge; actively self educates, reads and researches, willing to learn from others, always believes there is always much more to learn.
- Expert knowledge; has superb knowledge of, and has thoroughly researched the primary programming languages (typically 3 or fewer), object models and frameworks that they do most of their day to day programming with.
- Deep knowledge; has an in-depth understanding and experience in some small number (typically fewer than 10) programming languages and related technologies.
- Broad knowledge; has passing familiarity with a very wide range of programming languages and related computer technologies.
- Ability to write; can string words together to communicate. Client emails, co-worker emails, documentation, emails, proposals, blog posts, tweets.
- Knowledge of computer science fundamentals; object oriented programming, design patterns, algorithms and data structures, how computers work at a low level, hardware, operating systems, networking, databases & much more stuff.
- Verbal communication; able to explain their own thought process, can explain complex concepts, can participate in discussions with team members, can communicate with customers/users and other non technical people.
- User oriented; can empathise with users, understands where the users are coming from and what is most important to them.
- Software design and architecture; can design class structures, can design API’s, can design subsystems within an application, or can design entire application architectures.
- Quality oriented; understands software testing, writes tests for their code where appropriate, understands the concept of test driven development, meets organisational expectations for testing & quality, feels satisfied by a job well done.
- Balances coding priorities; knows when code should be written primarily for robustness, maintainability, reusability, speed of development, execution performance, scalability, security, polish, presentation, usability or some other factor.
- Problem solving; knows how to attack a problem and has the tenacity to solve even very hard problems, uses appropriate debugging tools.
- Development tools; understands their development tools, compiler/IDE and knows how to get the most out of them.
- Seeks simplicity; understands the danger in complexity, prefers simple solutions.
- Interested in the field; Knowledge of the industry, trends, directions, history.
- Avoids re-inventing the wheel; able to look at a problem, analyse it, work out what class of problems it comes from, can find patterns, libraries, algorithms, data structures or other pre-existing solutions that might fit the problem well and reduce the need to write code.
- Honest; can admit mistakes, unafraid to admit they don’t know something.
- Detail oriented; pays close attention. Avoids missing things, not sloppy or half-baked.
- Understands the lifecycle of software development; the roles played by developers and other people in that process.
- Manages own workload; able to prioritise their own tasks, willing to adapt to change.
- Cares about maintainability.
- Uses source control.
- Appreciates peer review; does not feel threatened or insulted by peer feedback.
- Groks; is able to read source code and learn what it is doing.
- Understands performance; able to optimise and write fast code when appropriate, knows how to avoid common performance problems.
- Writes clean code; readable, well formatted, appropriately commented code.
- Understands requirements specifications; able to make sense of software requirements, knows how to resolve questions and ambiguities, understands the relationship between requirements and testing.
- Follows coding standards; where there is such an expectation.
- Wants to be working on this project, at this company; a programmer is unlikely to do a great job if they are working on a project they don’t enjoy, or working at a company they don’t like.
- Strong research skills; good at ferreting out information: digging through documentation, searching the web, reading reference guides, release notes, discussion forums, mailing lists. Knows how to find answers.
- This slot reserved for suggestions - anything I’ve missed?
- Goto 49.
Sunday, May 27, 2012
Parallel Programming
Parallel Programming in the .NET Framework
.NET Framework 4
Many personal computers and
workstations have two or four cores (that is, CPUs) that enable multiple
threads to be executed simultaneously. Computers in the near future are
expected to have significantly more cores. To take advantage of the
hardware of today and tomorrow, you can parallelize your code to
distribute work across multiple processors. In the past, parallelization
required low-level manipulation of threads and locks. Visual Studio
2010 and the .NET Framework 4 enhance support for parallel programming
by providing a new runtime, new class library types, and new diagnostic
tools. These features simplify parallel development so that you can
write efficient, fine-grained, and scalable parallel code in a natural
idiom without having to work directly with threads or the thread pool.
The following illustration provides a high-level overview of the
parallel programming architecture in the .NET Framework 4.
Friday, April 27, 2012
Accessing Attributes With Reflection
The fact that you can define custom attributes and place them in your
source code would be of little value without some way of retrieving
that information and acting on it. C# has a reflection system that
allows you to retrieve the information that was defined with custom
attributes. The key method is GetCustomAttributes,
which returns an array of objects that are the run-time equivalents of
the source code attributes. This method has several overloaded versions.
For more information, see Attribute.
An attribute specification such as:
[Author("H. Ackerman", version = 1.1)] class SampleClass
is conceptually equivalent to this:
Author anonymousAuthorObject = new Author("H. Ackerman"); anonymousAuthorObject.version = 1.1;
However, the code is not executed until
SampleClass is queried for attributes. Calling GetCustomAttributes on SampleClass causes an Author
object to be constructed and initialized as above. If the class has
other attributes, other attribute objects are constructed similarly. GetCustomAttributes then returns the Author
object and any other attribute objects in an array. You can then
iterate over this array, determine what attributes were applied based on
the type of each array element, and extract information from the
attribute objects.
Here is a complete example. A custom attribute is defined, applied to several entities, and retrieved via reflection.
[System.AttributeUsage(System.AttributeTargets.Class |
System.AttributeTargets.Struct,
AllowMultiple = true) // multiuse attribute
]
public class Author : System.Attribute
{
string name;
public double version;
public Author(string name)
{
this.name = name;
version = 1.0; // Default value
}
public string GetName()
{
return name;
}
}
[Author("H. Ackerman")]
private class FirstClass
{
// ...
}
// No Author attribute
private class SecondClass
{
// ...
}
[Author("H. Ackerman"), Author("M. Knott", version = 2.0)]
private class ThirdClass
{
// ...
}
class TestAuthorAttribute
{
static void Main()
{
PrintAuthorInfo(typeof(FirstClass));
PrintAuthorInfo(typeof(SecondClass));
PrintAuthorInfo(typeof(ThirdClass));
}
private static void PrintAuthorInfo(System.Type t)
{
System.Console.WriteLine("Author information for {0}", t);
System.Attribute[] attrs = System.Attribute.GetCustomAttributes(t); // reflection
foreach (System.Attribute attr in attrs)
{
if (attr is Author)
{
Author a = (Author)attr;
System.Console.WriteLine(" {0}, version {1:f}", a.GetName(), a.version);
}
}
}
}
Subscribe to:
Posts (Atom)