Tuesday 4 June 2013

HTML5 in Gadgets on iGoogle

Did you know gadgets can use html5? The key is in the doctype. Normally the doctype of a gadget isn't mentioned. Specify the html5 doctype in the gadget and it will be used when the gadget is rendered. Let's look at a quick example using the popular canvas drawing API:

<?xml version="1.0" encoding="UTF-8" ?>
<Module>
 <ModulePrefs title="html5 canvas">
 </ModulePrefs>
 <Content type="html" view="home,canvas">
  <![CDATA[
   <!DOCTYPE html>
   <script>
    var demo = {
     init: function() {
      var drawcan = document.getElementById('drawarea');
      if (drawcan.getContext){
       var context = drawcan.getContext('2d');
       var xmax = drawcan.width;
       var ymax = drawcan.height;
       context.strokeRect(0,0,xmax,ymax);
       for (var dotix = 0; dotix < 100; ++dotix) {
        var x = Math.random() * xmax;var y = Math.random() * ymax;
        context.strokeStyle = "#888888";
        var blueness = Math.floor(Math.random() * 256);
        context.fillStyle = "rgb(10,90,"+ blueness +")";
        context.beginPath();
        context.arc(x,y,5,0,Math.PI*2,true);
        context.closePath();
        context.stroke();
        context.fill();
       }
      }
     }
    };
 
    gadgets.util.registerOnLoadHandler(demo.init);

   </script>
   <canvas id="drawarea" width="150" height="150"></canvas>
  ]]>
 </Content>
</Module>


HTML5 in Gadgets on iGoogle

The top of the apparatus still has the accepted XML prolog because the apparatus blueprint is, as always, an XML document. The html, in this case html5, is central a CDATA block. The CDATA block agency the anatomy of the html5 agreeable is appealing abundant abandoned if parsing the XML. iGoogle doesn't do annihilation added for affinity with html5; appearance specific to html5 will still alone plan in browsers that abutment them. Gadgets accept the aforementioned cross-browser affinity apropos as any added web page. Accept a attending at the If can I use adviser for an abstraction of affinity of appearance beyond browsers.
The agreeable of this apparatus is appealing straightforward. It includes a canvas element. In the init adjustment it tries to get a cartoon context. If it's acknowledged (meaning the browsers supports html5 canvas) it will draw a rectangle about the extents of the aspect again draw 10 about placed dots inside. Use this apparatus as a starting point to get your own html5 apparatus running.
And yes, this apparatus has a canvas in your canvas so you can cede if you render.