Tales of a Developer Advocate

Developer Relations @ Google

  • .Net Developers: SVN vs Mercurial pt2

    • 25 Aug 2010
    • 0 Responses
    •  views
    • Edit
    • Delete
    • Tags
    • Autopost
    So, after some thought, some replies (mostly favouring SVN) I have chosen to go with Mercurial as the VCS for the project that I am working on.

    The reason being, is that although I won't be hosting on Codeplex, they support Mercurial and there are plenty of tools that .Net developers can use to bind to the SCM and the fact that it is a distribute system means it wins in my eyes.  I will need decent branching support and I have never been a fan of SVN's branching mechanism.

    I am willing to be educated more if you have any compelling reasons for me not to go with Subversion.
    • Tweet
  • .Net Developers: SVN or HG?

    • 24 Aug 2010
    • 3 Responses
    •  views
    • Edit
    • Delete
    • Tags
    • Autopost
    Just a quick question for the .Net developers out there, both Windows and Mono.

    Given a choice of Subversion or Mercurial, what would be your preferred source control service and why?  Which helps you work the best?  Which has the  least impact on your workflow?

    Sorry guys, I know that some of you may use others SCM's, but I am just interested in these two?
    • Tweet
  • Using HTML5 Canvas with Drag and Drop (setDragImage)

    • 11 Aug 2010
    • 0 Responses
    •  views
    • canvas dnd drag and drop html5 javascript
    • Edit
    • Delete
    • Tags
    • Autopost

    Using HTML5 Canvas with Drag and Drop (setDragImage)

    I know a lot of people complain about Drag and Drop (DnD), but it is not all that bad. At least we have a platform to work off for the future.

    We have recently seen a lot of improvements to Gmail which now includes additional support for Dragging in to Gmail to upload your docs and files, and also dragging attachments out of Gmail to the user file system. I like both of these pieces of functionality, they are subtle but introduce

    One thing that I see from the jQuery UI world is that it makes DnD very easy to implement but also very pretty. Whilst HTML5 DnD has support for attaching any arbitrary element to the drag operation, in practice I have never seen a browser implement it, so we are left to our own devices on what we want to show to the user during a drag operation.

    I have produced a very (rough) sample – http://html5samples.appspot.com/canvasToDrag.html. There is still a lot of work for me to do with my sample (it needs to be a lot prettier). It is very simple to use, select the negatives that you wish to “develop” and drag them into the development area. You will see that there is a “fan” effect on the drag icon. I must warn that this only works on Webkit browsers (so Chrome and Safari – actually, the drop doesn’t work in Safari yet)

    This is all enabled through DataURI’s. I take the selected images and add them to the canvas (adding a little rotation and scaling for effect). Once the canvas is completed I simply call .toDataURL() on the canvas element and add it to a temporary image element. This image element is then used as in the call to the setDragImage method

    // Code inside the dragStart event.
    
    var uris = []; // A list of img uris, so I know what to display after the drag
    
    var img = document.createElement("img");
    img.src = canvas.toDataURL();
    
    e.dataTransfer.setData("text/plain", "Text to drag");
    e.dataTransfer.setData("text/uri-list", uris.join("\n"));
    e.dataTransfer.setDragImage(img, 128,128);

    It is pretty simple, and can allow you to do some nice effects. Things that I can’t do in HTML5 Drag and Drop is animate the element being dragged. I would love to be able animate the canvas as the user drags the elements around the screen, maybe adding a bit of inertia to the photographs as they are dragged around.

    • Tweet
  • DOM TreeWalker

    • 8 Aug 2010
    • 1 Response
    •  views
    • html javascript
    • Edit
    • Delete
    • Tags
    • Autopost

    I really wanted to get a reference to Walker Texas Ranger in to the title, but I really couldn’t think of anything that cool. If you can think of a great Chuck Norris reference leave a comment, I am all chins!

    It always amazes me that there is so much to HTML that is still not being exploited by developers.

    One pattern I see regularly is recursive descent through the DOM to find particular TEXT nodes that contain a given string so that the container element can be manipulated.

    It is not that recursion is slow, if your DOM is complex enough you could hit stack overflow errors (although it is pretty unlikely), it is that there are a lot of edge cases when parsing the DOM that you need to code in.

    A little known DOM function is available that makes developing applications that need to scan the DOM easy. It is called Tree Walker, created through the createTreeWalker function on the document.

    You can create a tree walker very quickly using the following Javascript:

    document.createTreeWalker(document.body, NODE_FILTER.SHOW_TEXT, function(node) { return NodeFilter.FILTER_ACCEPT; }, false);
    
    while(treeWalker.nextNode()) console.log(treeWalker.currentNode);

    The above code is given a root node of document.body, a filter of what to show (only Text Nodes in our case), and a function that returns if the node should be returned (essentially a filter).

    An interesting point to note is that the Filter function is only called when iterating over the treeWalker.

    This is actually a really cool feature, the currentNode property of the Tree Walker contains DOM objects, so you can start to do some really advanced processing, you could highlight the current node, replace its text or remove it – really anything you want. This is significantly simpler than managing the recursion yourself.

    As a more concrete example, lets use this to find all twitter user names on a page and then automatically make these a twitter link. It could be done using recursion pretty simply, but I need something fun to show you.

    var re = new RegExp(); // This isn't accurate RE
    re.compile("@([A-Za-z0-9_]*)");
    var walker = document.createTreeWalker(
      document.body,
      NodeFilter.SHOW_TEXT,
      function(node) {
        var matches = node.textContent.match(re);
    
        if(matches) { 
          return NodeFilter.FILTER_ACCEPT;
        } else {
          return NodeFilter.FILTER_SKIP;
        }
      },
      false);
    
    var nodes = [];
    
    while(walker.nextNode()) {
      nodes.push(walker.currentNode);
    }
    
    for(var i = 0; node=nodes[i] ; i++) {
      node.parentNode.innerHTML = node.parentNode.innerHTML.replace(re, "@$1") }

    A live example is on my sample site

    The theory is, that User-Agents can optimize the access to the DOM better than you can recursively descend through the DOM. So, where would I use this? The first thing that springs to mind is that it is ideal for Chrome extensions. Many Chrome extensions traverse the DOM looking for pieces of text, or particular patterns inside nodes that aren’t available via CSS Selectors.

    More information can be found on Mozilla’s Developer site

    • Tweet
  • Canvas on the Background

    • 8 Aug 2010
    • 1 Response
    •  views
    • Edit
    • Delete
    • Tags
    • Autopost
    I was reading the documentation about -webkit-gradient on the Apple HTML documentation site and I stumbled across something that I had never seen before.  I thought it was so cool, that I decided to write a blog post about it.

    With the introduction of CSS3 into many top-line browsers there is now the ability to manipulate the background-image of any element, specifically gradients and svg.  One of the things that is missing is canvas as an element for the background.

    In 2008! WebKit introduced(http://webkit.org/blog/176/css-canvas-drawing/) a CSS attribute value called -webkit-canvas(id).  When attached to a background style allows you the developer to have programatic access to the background-image through canvas. Believe me when I say "This is Powerful".

    I must warn that this is specific to WebKit only at the moment.  I would love to see other browsers add this support in as it is super powerful.

    So lets see this in action.

    In this post I will do one simple sample (http://html5samples.appspot.com/backgroundCanvas.html) and all it will do is draw a square into the background of a div:

      <html>
        <head>
          <style>
          div {
            background: -webkit-canvas(test);
            float:left;
            width: 100;
            height: 100;
          }
          </style>

          <script>
            function onLoad() {
              var ctx = document.getCSSCanvasContext("2d","test", 100,100);
              ctx.fillStyle = "blue";
              ctx.fillRect(10,10,90,90)
            }

            (function() {
              document.addEventListener("DOMContentLoaded", onLoad);
            })();
          </script>
        </head>
        <body>
          <div>This is a div</div>
        </body>
      </html>

    Pretty cool!  Well actually the concept is cool, that demo is pretty ugly :) You can do some pretty awesome demos with this.  You could have a game or some super cool animation running in the background. I think you could get around the lack of background-opacity support in CSS3.

    I will do some more samples soon.

     

    • Tweet
  • About

    I help developers build really cool products on the Web.

    I work for Google as a Developer Advocate in London, specializing in Chrome, HTML5 and the Chrome Web Store.

    231036 Views
  • Archive

    • 2012 (5)
      • February (5)
    • 2011 (30)
      • July (1)
      • June (4)
      • May (10)
      • April (4)
      • March (1)
      • February (5)
      • January (5)
    • 2010 (35)
      • December (15)
      • November (5)
      • October (2)
      • August (5)
      • July (8)
    • 2009 (1)
      • January (1)
    • 2008 (8)
      • December (1)
      • April (2)
      • March (3)
      • February (2)
    • 2007 (17)
      • December (1)
      • September (1)
      • August (5)
      • May (1)
      • March (2)
      • February (4)
      • January (3)
    • 2006 (153)
      • November (3)
      • October (7)
      • September (11)
      • August (9)
      • July (9)
      • June (14)
      • May (11)
      • April (32)
      • March (23)
      • February (26)
      • January (8)
    • 2005 (274)
      • December (7)
      • November (43)
      • October (63)
      • September (54)
      • August (66)
      • July (13)
      • June (10)
      • May (10)
      • April (6)
      • January (2)
    • 2004 (1)
      • August (1)

    Get Updates

    Subscribe via RSS
    TwitterFriendfeedLinkedIn