Using D3 or third party libraries with Polymer – simple example

Off late I have been exploring and working with Polymer a lot. One thing I tried recently was to create a custom element (a custom tag) that has a d3 chart integrated into it. So whenever I use this element in an application the chart renders.

For those who are not familiar with Polymer, let me tell you, it’s a game changer for the next generation of web applications. The web is getting componentized with the new standards that are coming along – Shadow DOM, custom elements, templating, HTML imports e.t.c. Polymer is a polyfill layer or as they call it a SUGARING layer on top of these web standards that allow you create custom web components easily. You do not need to go the the low level API’s to create a custom element. Just use the Polymer library and start creating your own components. Also it provides a heck of other features that can be used, such as two way data bindings. So go over to the Polymer site and check it out.

Coming back to our topic of discussion, one thing that can be a bit confusing to Polymer beginners is to use a third party javascript library with your polymer element. Here I have an example of using the D3 js charting library to create a custom chart component. Let’s call our custom element  my-custom-d3-chart. By the end of this discussion we will have a custom tag – <my-custom-d3-chart />. Yes a custom HTML tag. Our own tag. Yo!!!

Read More »

Animated growing pie chart with Rapahel JS

This is my first post after a long gap – almost one year. During this period I got busy with my regular work, family and other commitments. It was really hard to keep writing although I wanted to do that.
One of the tasks that I worked on a few months back was animating a pie chart as if it is growing from 0 to 360 degrees. And this post will share that with you. So let’s get started….
You might already be knowing that Raphael JS is a great JavaScript library to work with SVG in your web projects. So did I to create the pie chart. I am using Raphael and its features to create the pie chart and animate it. Here’s the demo link. Run this in your browser http://jsphkhan.github.io/growing_pie/

growing_pie

Quick Concepts: 
Most important functionality in the growing pie chart code is defining a custom Raphael paper attribute. So here’s the concept. If you have an attribute that you want to represent as a function and add your logic to it, you can create a custom attribute. For details you can see the official reference here. But here’s how I defined a custom sector attribute that represents a pie slice sector.Read More »

HTML5 SVG Support check using JavaScript – the simple way

My last post talked about how you can detect whether your browser supports HTML5 Canvas or not using simple JavaScript technique. Based on the same principles, this post illustrates how you can detect if your browser supports SVG elements. You may note SVG is Scalable Vector Graphics and HTML5 has native support for SVG tags. The difference with Canvas is that in case of SVG elements, they become a part of the DOM and you can manipulate each SVG elements, whereas in case of Canvas, the entire <canvas></canvas> tag becomes one node in the DOM and anything inside the Canvas <canvas> tag (for eg. a Rectangle) cannot be manipulated individually using standard DOM methods. Browse the web and you will find a hell lot of topics on this.

Alright, now back to the detection code. Just copy/paste this inside your window load / body load or in case of JQuery the $(document).ready() method,

if(!document.createElement('svg').getAttributeNS){
  document.write('Your browser does not support SVG!');
  return;
}

So, if your browser does not support SVG, then it will not process further (in case your application cannot live without SVG). But if you have a back up plan and you can live without SVG in your browser, then you can add an else block along with the if so that you can write your backup logic.

Explanation
Well, the code is very simple. Inside the if condition I create a SVG element using the standard DOM createElement() method. Now this will create a <svg></svg> object. Now next thing I do is to check if the getAttributeNS() method is defined or not. If you browser does not support <svg> then there is no way it will understand getAttributeNS() and that’s the catch.

HTML5 Canvas Support check using JavaScript – the simple way

This is how you can check for HTML5 Canvas support in your browser by using JavaScript, there’s no need of an external library like Modernizer to do a simple check like this. In fact most of the feature checks can be done natively, that’s what I prefer.

Here is the code,

var canvasEl = document.createElement('canvas'); //create the canvas object
if(!canvasEl.getContext) //if the method is not supported, i.e canvas is not supported
{ 
  document.write("HTML5 Canvas Not Supported By Your Browser");
  return;
}

As you can see in the first line I create a HTML5 Canvas element by using createElement() DOM method. As you might know that to work with Canvas, you have to get it’s context object (2d for now), which can be retrieved by the getContext() method. So, if a browser does not support the <canvas> tag or the canvas element, then no way the getContext() method will be defined for it. That’s what I check in the second line – the if condition. Remember that a function/method name is a variable which holds a reference to the function block. So we can check whether it is defined or not, in a normal way like we do for object properties. IE8 doesn’t support HTML5 Canvas. So try this code in IE8 for a quick test.

How can I use it with my code?
Use this code block inside the window load event handler or when the DOM ready event is triggered (using document.onreadystatechange, more). You can modify it as per your need.
Leave behind a comment if you are upset with this attempt.

Previewing JSBin output in mobile devices using Adobe Edge Inspect

When Adobe renamed Shadow to Edge Inspect, one of the major changes that happened was the integration of JSBin into Edge Inspect. What this means is that whatever proof of concepts (also called Bins as per JSBin terminology) you have created with JSBin, you can remotely preview it directly in your mobile devices without any additional set up required.

If you haven’t tried JSBin yet, then let me introduce it to you. JSBin is a free online tool with which you can create HTML based small POC’s, test examples or I should say small mockup kind of applications. You can edit the HTML, CSS, Javascript and see the changes real time in your browser itself. JSBin is kind of a development environment wherein it has the HTML, CSS and JS editors where you write code for your example app. It also has the console panel for viewing javascript console logs and the the Output panel for previewing the result of your code. All these panels are integrated into one place and as you make changes to your code you can see the result in real time. Very handy. I found it useful for my mobile web apps. But then it is a tool which is on the small scale development side. So it doesn’t have all the fancy features of a typical IDE. Once you create your POC’s or Bins you can even share them. So in case JSBin sounds interesting to you, go ahead and start using it. Create your account at http://jsbin.com/ and start making some apps. The interface looks like the image shown below, (all the help can be found online at jsbin.com)
Click to see larger image
Read More »

Replicating the iPhone Swipe Gesture – auto scrolling feature

This is an update to the Replicating touch swipe gesture javascript implementation series that I have been writing for some time now and this time I have tried out the auto scrolling feature. Sometimes users may want a auto scrolling along with the normal swipe gesture feature. This post will talk about it and the changes to code that were made to make it auto scroll. First let’s check out the demo, the demo runs in both computer webkit browsers and mobile webkit browsers.

Demo link: http://rialab.jbk404.site50.net/swipegesture/auto_scrolling/

Now, let’s talk on the implementation,
First the features of this demo-

  • Auto scrolling – the gallery slides change automatically at periodical times. Based on the requirement specify whether auto scroll stops on user interaction or continues.
  • Looping – the gallery loops and is circular.
  • Direction – supports two direction – left or right. Specify which direction the gallery should auto scroll.
  • Click/Tap to URL – click or tap to open URL’s.
  • Swipe gesture – and then the touch based swipe gesture for mobiles is available as well.

Read More »

Adobe Shadow is now Adobe Edge Inspect

In a major move on the 24th of September, Adobe has renamed Shadow to Adobe Edge Inspect and now it has become a part of the Adobe Edge Tools and Services : http://html.adobe.com/edge/
Shadow had been a great success for mobile web developers and designers to remotely debug applications on the mobile device in real time. I had been using the preview version of Shadow for quite a time and has found it really useful, the last one being the Shadow Labs Release 4. So now the preview period of Shadow is ending on the 31st of October and after that Shadow will be invalid. Instead you have to download Edge Inspect and start using it.
You can get Edge Inspect by signing up for a free Creative Cloud membership subscription or a paid subscription. Get a account for yourself at: https://creative.adobe.com/join/starter. The free version allows only one device to be paired with your computer while the paid full version allows multiple devices to be paired simultaneously with your computer. Here’s a video demonstration on the new Edge Inspect release

Read More »

Circular swipe gesture gallery – looping through images

This is another update to the regular swipe gesture gallery that I created in two of my earlier post – post1, post2. This time I have implemented the looping feature for the images. The gallery never ends, instead you swipe it in a circular motion. So, whenever you reach the end of the list, swipe again from right to left across your device’s screen to see the first image and also when you reach the beginning of the list you swipe from left to right of your device’s screen to see the last image again. Try out the demo first and you will get a feel of it. Check out the demo in a webkit desktop browser (Chrome, Safari) or an iOS/Android device’s browser. Click on any image to open a URL as well. So, click/tap to open URL is included.

Demo link: http://rialab.jbk404.site50.net/swipegesture/circular/

An instance of the demo application – with reduced opacity and exposed images, the darker one being the one currently displayed and is inside the wrapper div.

Read More »

Replicating the Swipe Gesture iPhone Gallery for mobile web – images linked to URL

Just a quick update. I have made little changes to the original demo so that now the images are clickable or rather tap-able. So when a user taps on an image he is redirected to a URL. You can specify separate URL’s of your choice for each image. The default behavior of the swipe gesture and replicating it is still there. I have just added the feature of linking the images to URL.

Try the demo in computer or mobile browser (webkit based browsers only): http://rialab.jbk404.site50.net/swipegesture/common/

Tap/Click on any image in the gallery to open a URL. Also you can swipe on the images.

What are the changes made?
On the HTML side, I added the URL’s to the <img /> element in the index.html file, I have added a custom attribute link.

<li>
<img src="img/1.jpg" width="100%" height="100%" link="http://www.google.com"/>
</li>
<li>
<img src="img/2.jpg" width="100%" height="100%" link="http://www.yahoo.com"/>
</li>

Similarly, all the other <img /> elements have a link attribute that specifies the URL to redirect when user taps on that particular image. Note that link is a custom attribute. You can add custom attributes to HTML elements.

On the script part
This is where a little understanding is required. In my previous posts – part1 and part2 I have discusses in details about the swipe gesture and how using touch events I have managed to achieve it. If you see the JavaScript code, I have registered touch events that listen to the user’s finger movements. So if you notice a swipe gesture is actually a combination of

Swipe Gesture = touch start + touch move + touch end

It is only in the touch end event listener that we manipulate the net distance moved and based on that we swipe the images. Now, since we also need a tap/click to URL feature for each image so if you directly add a redirection code in either touchstart or touchend event listeners then every time you try to swipe it will take to to a URL. And then even the swipe is affected. So as soon as you start to swipe it, it will redirect you to a URL, which is exactly what we do not want. Now, if you notice a tap event it is a combination of,

Image Tap (Tap to a URL) = touch start + touch end

So there is no touch move in a tap event for an image. So this is the concept that helps in making the Swipe as well as the Tap to URL work together. When a user swipes across the screen he is actually covering some net distance, so your start point is not same as the end point. But when you tap on an image your start point and end points are same before you release your finger. Hence in a tap the net distance is zero. So based on that I made changes to the event handler for touchend event in the script file,

swipey.wrapper.addEventListener("touchend", swipey.endHandler, false);

And then in the endHandler method I added this,

if(swipey.distanceX == 0) //if the intention is to tap on the image then open a link
{
  var link_url = event.target.getAttribute("link"); //read the link from <img /> element
  window.open(link_url,"_blank");
}

This checks if the net distance is zero. And if it is so, then it reads the link attribute value and then redirects the user to the URL specified by link.

To open links/URL in same window or page – use _self instead of _blank in window.open()

This is it, now the code is capable to handling both user swipes and user tap/click to URL. You can checkout the demo. To download the code follow the link given below.

Download code
You can download the source code here.

Updates/Other related posts
1) Creating a swipe gesture gallery – My initial post on the topic. You can start here.

2) Flickering issue in iPhone/iPod solved.
After the swipey demo was done I was observing a strange flickering issue of the images. If you see the demo link in an iPhone or iPod’s mobile safari browser you would notice it. Test case – swipe the images to the left and you would notice a black flicker on the right side of the browser for a brief moment. This is happening when you swipe all the image for the first time. Second time on wards it is not being seen. Finally I could solve the issue and made a small post on it with the new fixed demo. You can find it here.

3) Common code and example for mobiles and computer browsers – I have developed a common universal code for mobile browsers and computer browsers. Note that when I am saying browsers I mean web-kit browsers – Chrome & Safari in computers, and then the default web browsers in iOS and Android mobiles. The major changes are in the javascript code, where I have automated the user event handling process. What this means is that for mobile devices touch based events are registered and listened to and then for computer browsers mouse based events are registered and handled by the script automatically. This way there is no need to hard code touch events for mobiles and mouse events for computers. The same code works everywhere. Find the post here. There is a demo and also a download link.

4) Circular swipe gesture gallery – I have a new tutorial where I have talked about a circular swipeable gallery. So the images keep on looping. The tutorial also includes a new demo. Read it here.

5) Auto scrolling swipe gesture gallery – Sometimes users may want a auto scrolling gallery along with the normal swipe gesture feature. I have a new post with a demo. Read it here.

Creating a simple List in Sencha Touch 2.0

Creating a simple data list in Sencha Touch 2.0 has changed over the way we used to create data lists in Sencha 1.0.  Changes have been made in the way a data Model is created and the data fields are defined, but the overall concept remains the same.  Here is a quick tutorial along with a demo,

Demo Link : http://jbk404.site50.net/sencha/datahandling/ (might take some time to load, my server is horribly running slow)

Read More »