Archive for the ‘Javascript’ Category

Ray Tracer in JavaScript

Thursday, January 19th, 2012

Ray Tracer in JavaScript

Just when you thought you had seen everything that could impress in JavaScript, along comes another crazy application.. Now we have a ray tracer that creates very realistic 3D scenes right in your browser.
Ray tracing is one of the most fundamental of 3D rendering techniques, in the sense that it attempts to simulate the way a real scene is created by the light rays that enter your eye. The only difference is that a ray tracer generally works in the reverse direction to real light rays – that is from the camera out to the scene. The program has to trace a the path of a light ray from the camera out into the 3D model and see what it hits. From the color of what it hits, you can work out what color the pixel it intersects in the camera should be. It is simple in theory but it can be tough to implement and it is a real test for the hardware that runs it.

Christopher Chedeau and Gauthier Lemoine have implemented an, admittedly simple, ray tracer in JavaScript. Using WebGL and a few useful tools such as glMatrix, Code Mirror and CoffeeScript. It only renders four object types – plane, sphere, cylinder and code but the principles it uses are general . It handles point source lighting and textured objects. One nice touch is that it uses progressive rendering which makes watching it build up a scene far less boring.

(more…)

WinRT JavaScript – Templates & Data Binding

Thursday, January 19th, 2012

WinRT JavaScript – Templates & Data Binding

The Template object is useful in its own right but add it to data binding and you have a easy to use way to present data to the user. It also happens to be the easiest way to find out about data binding in general.
Articles on WinRT JavaScript
Getting Started
WinControls
Templates & Data Binding (this article)

In the previous part of this series of articles on WinRT JavaScript we looked at how to use WinJS custom controls. In this third part we look at one of the more interesting custom control – the Template and introduce the databinding mechanism.
The Template is useful whenever you need to create multiple copies of the same HTML and content. You could, for example, use it to generate the lines in a table structure. This is useful but not super useful. However, if you add data binding to the Template object it all becomes so much more useful. With data binding you can not only “stamp out” lines in a table, you can also automatically fill them in. The databound Template is a natural way to create any table or list of data automatically and with minimal fuss.
So let’s get started.
It is assumed that you know how to work with a WinRT JavaScript project and know the basics of HTML/CSS as described in Part One. It is also assumed that you know how custom controls work as described in Part Two.
The Template

The basic Template control is a very simple thing. All it does is to contain any HTML you care to fill it with. When the Template control is initialized its only action is to hide its contents. This may sound like a strange thing to do but you don’t want the Template itself showing on your page until you are ready to make a copy of it and place it where you want it.
Start a new blank project and edit the default.html page to load the following scripts. The only new one from part two is binding.js which is needed for the Template object:





Next we need to create a Template element using the standard approach introduced in Part Two, i.e. using the data- attribute. As the template represented by the Template object is composed of whatever HTML you care to put between its tags, let’s just use a Div with some dash characters to represent a horizontal line – not particularly useful but easy to understand.
Our Template in HTML is just:

"WinJS.Binding.Template">
————–

If you run the program at this point you will see a single line of dashes at the location of the Template div – after all why would you see anything else?!

To make the Template do its magic we have to invoke the JavaScript code that scans the DOM and implements the Template Object in JavaScript. All we have to do is call the usual processAll function to convert elements with data-win-control attributes into controls (see part 2). You can add this call to either the onmainwindowactivated or the DOMContentLoaded event handler:
document.addEventListener(“DOMContentLoaded”,
function (e) {
WinJS.UI.processAll();

If you now run the program you will discover that the contents of the Template element have gone missing i.e.you now can’t see the dashed line.

This is general behaviour Templates hide their contents when converted into custom controls.
The next step is to do something useful with our Template.The key idea here is that the Template object has a render method which instantiates the contents of the Template as a DOM sub-tree.
So for example to create an instance of the content of our Template, i.e. a single

with a dashed line for its text node all we have to do is call the Templates render function – but there are two complications. First we need to get the JavaScript object that corresponds to the Template object and has the render method. This is easy:
var template = WinJS.UI.getControl(
document.getElementById(“Mytemplate”));
Now we can call the render method:
template.render();

As is the way with most WinRT methods it does not return the result but a Promise object. The reason is that in principle rendering a template could take a long time and so the render method is implemented as an asynchronous call and so returns a Promise object.
There are many things to say about Promise objects but for the moment the only thing we need to know is that you can use the promise.then() method to call a function when the task is completes successfully and an optional second function to be called if it fails.
So the entire process of rendering the template is:
template.render().then(function(element){

});
and the rendered DOM subtree is returned as the element parameter and we can write some code to work with it.
So now we have a rendered Template what do we do with it?
In most cases the answer is that we add it to some existing element in the HTML. For example if we add a div with id Display:

and now after the render method completes we can simply add the result to the DOM within the div:
var display=
document.getElementById(“Display”);
display.appendChild(element);
The entire HTML is:

"WinJS.Binding.Template">
————–


and the JavaScript is:
document.addEventListener(“DOMContentLoaded”,
function (e) {
WinJS.UI.processAll();

var template = WinJS.UI.getControl(
document.getElementById(“Mytemplate”));
template.render().then(
function(element){
var display=
document.getElementById(“Display”);
display.appendChild(element);
});
});
Now if you run the program you will once again see a dashed line:

(more…)

HTML5 vs Flash – How Does It Affect You?

Monday, October 11th, 2010

HTML5 vs Flash – How Does It Affect You?

The manner in which most videos are currently played through browsers these days is through a Flash plugin. This works pretty well but Flash unfortunately requires a lot of computing power. A new web standard HTML5 is trying to change that.

HTML5 has been designed with audio and video codecs which should take less processing power than an equivalent Flash player. Independent tests have shown that this is generally so although not entirely across the board. As with most new technologies, things are not always clearcut.

However even with improvements in processing efficiency from HTML5, it should not be assumed that it will completely replace Flash or even have a significant impact, especially on rich web content. Flash still has many advantages such as:

~ Better sub pixel resolution and anti aliasing
~ It’s good excellent developer tools (far more extensive than HTML5)
~ Flash has a vast array of good looking and impactful fonts
(more…)

HTML5 – The End of PSD to HTML Conversion Services?

Sunday, October 10th, 2010

HTML5 – The End of PSD to HTML Conversion Services?

In 2009 the World Wide Web Consortium (W3C) decided to concentrate their efforts on HTML5, a new revision of HTML, rather than XHTML2. HTML5 is strongly supported by Apple, Opera, Mozilla, Microsoft, Google and other leading IT companies and web browser developers. All this means that HTML5 is becoming a mainstream of the Internet.

Among peculiarities of HTML5 there are backwards compatibility and forbearing error handling. In other words, with HTML5 websites written in previous versions of HTML/XHTML-CSS will not need an urgent update to be correctly displayed by even the most modern browsers.

Now the main question for PSD-to-HTML conversion providers:
Doesn’t this all indicate that PSD-to-HTML services will lose their importance?

Really, PSD-to-HTML companies declare “Code compliance with the latest W3C standards” as one of the main promises and advantages, but with HTML5 there will be no need in such a strict validity.
(more…)

Mozilla moves into Open Web gaming

Thursday, September 9th, 2010

Mozilla moves into Open Web gaming

WEB BROWSER OUTFIT Mozilla has started up a division to develop games that can be played on web browsers.

Mozzarella has labeled it Mozilla Labs Gaming and the cunning plan is to use the technologies that make gaming on the Open Web possible.

According to the website, the Mozilla Foundation is inviting the wider community to play with cool technology and help establish Open Web as the framework behind gaming across all Internet connected devices.

A spokesman said that modern Open Web technologies have stacks of technologies such as open video, audio, WebGL, touch events, device orientation, geo-location, and fast Javascript engines that make it possible to build complex – and not so complex – games on the web.

He said that since these can all run on modern web browsers, the time is ripe for pushing the technology to game developers.

It seems Mozilla thinks games are a good way to keep people using its software. After all apps worked for the Iphone, even though real games on the PC knocked the spots off ones on the Imac.

The spokesman noted that games and game developers are at the forefront of technology, often pushing the boundaries of what was thought possible.

As part of this the open sourcerors at Mozilla are starting an international gaming competition: Game On 2010.

This competition will be open to all developers interested in creating games using the latest in Open Web technologies. More details about this will be forthcoming.

Source:theinquirer.net