Introduction

Warning: IE10 for Windows 7 has a problem with POST Ajax requests and until this is fixed by Microsoft Canvas Control Library will not support IE10 on Windows 7. IE10 on Windows 8 is working fine.

Work Around to above Warning:

The meta tag I had recommended to use before was with IE=edge. This still stands unless you are on Windows 7 and automatic updates has updated IE to version 10 for Windows 7. If you are on Windows 7 with IE 10 then to continue working with IE change the meta tag attribute from IE=edge to IE=EmulateIE9. Basically as this describes the browser will now work like an IE9 browser in which case your Canvas Control Library ajax Post calls will not break and your website will run on this client.

Changes to article that caused this new version:

Since the article is very long as it documents the entire system of Canvas Control Library I will use this section to describe what has changed in the article
from the last version to this current one so you do not have to go through the entire article again. Towards that end here are the changes:

All controls have tab stop capability except those that don't need it which are scrollbar and wordprocessor controls. Updated ASP.NET/C#, Jsp/Java and PHP server side logic also.

Added the ability to tab change focus between textbox's. This was painfully obvious in the login screen of Web Wunder Mail example where there were 3 textbox's and you had to click to get focus after filling out the first one. Now you can just tab. Support for combobox also but its not yet meaningful. All controls that have a meaningful need for tabbing to will get included. Also updated the how to on custom control creation section to include how to include your custom control in tab stopping process.

Currently if you want to use controls like Button or ComboBox in HTML5 you have the HTML5 elements to use and place on your web page. These controls that come free with HTML5 are limited in the extent to which they can be made fancy or as in ComboBox if you wanted the text and dropdown list to contain images for backgrounds or complex controls you would find this to be quite hard. Another new element that has been introduced in HTML5 is the canvas element. This element allows for drawing whatever you want on it using JavaScript. The current situation though is there are no controls you can use that are available that are purely based on the canvas. To plug this gap I have created a Canvas Control Library with all the common controls and advanced ones like TreeView which due to the nature of canvas allow for great control over the graphical presentation of the controls and their item contents. So in the case of the ComboBox example with my Canvas Control Libraries ComboBox adding images to the background of the text area and list drop down is quite easy. Also animated graphing and charting controls have been ported to first class Canvas Control Library controls.

The main example is the FormsBased.aspx (ASP.NET/PHP on Apache/PHP on IIS/PHP on Ubuntu Linux) if you look at this you will notice the following differences between this new system of mine and the current normal old way of doing web pages with form user input requiring server postbacks and showing the new results on the client:

You do not move away from the page it acts like a Windows Application.

It has better usability the user can input information and get back information after the server has processed it in a quicker fashion that is more like the usability experience of a Windows WPF application.

Since you aren't downloading whole web pages over and over as you click your way to booking a ticket in the example its faster and more responsive to the user. So performance gains in other words.

The final seller is you are free of the limitations graphics presentation looks wise that you will be if you use regular HTML5 controls to build your forms based websites pages even if you are the best CSS3 skinner on the planet. So you can provide the best look and feel and the only limit is your artistic imagination not the technology which has been the limit so far.

The simplest element is Label control and to add a label to your canvas you would make the following JavaScript call from your web page after the canvas element on which you mean to draw the label as follows:

So what this means is that the id of the canvas for example "canvas1" is passed in which is the canvas on which you want to draw is the first parameter to the function call. The second and third parameters x, y is the point location of the top left corner of the label you want to draw at on the canvas. The width and the height of the label. The text parameter is the text you want to display for the label in this case the label when drawn will display text Label 1 at location 150, 50 on the canvas1 canvas. The textColor is the color of the text you want drawn. The textHeight is essentially the same as the textFontString font point value which in this case is 12 as we are using 12 point Ariel font for drawing the label text. The drawFunction is an override so that if you wanted to draw something custom you could supply your own JavaScript canvas drawing code to draw the label in any custom way you like. The depth value is the layer on which the control will be drawn and normally you want to use the highestDepth variable value which is defined and maintained in the Canvas Control Libraries internal JavaScript code. You can also make the label into a hyperlink and supply the parameters. Look at button examples which provide using the extra hyperlink parameters.

The first example will draw a button on canvas1 at 10, 10 with a width of 100 pixels and a height of 40 pixels with blue 12 pt Ariel font button label text Google Search. The only new thing is that when you click the button you go the URL provided www.google.com. We do not provide a custom drawing JavaScript function so we set the drawFunction parameter to null. The parameters bottomColorStart, bottomColorEnd, topColorStart and topColorEnd define the bottom half gradient and top half gradient of the background of the button. The borderColor parameter draws the border line in that color. When isnewbrowserwindow is set to 1 as in the second example you provide all the parameters to customize the new browser window to display the URL in the case of second example www.codeproject.com. In the third example you don't use hyperlinks so you don't provide any of those parameters and just provide a custom javascript click function to do custom logic in the case of the example it pops up an alert box.

Themes the beautification begins with 2 themes for buttons 1 & 2 with gloss and the screenshot for theme 1 is shown below:

The screenshot for theme 2 is shown below which is a more complex theme:

In the createGrid function one of the new parameters is rowData which is simply an array which contains an array for each row which contains the cell text to draw for each cell. The next array headerData is an array containing the header cell text to draw for each header row. One of the advantages is that the header row stays put on scrolling the contents of the grid and only the rowData contents rows and cells that is scroll. You can custom draw the cell contents by passing in a JavaScript function which draws them for the drawRowDataCellFunction which in the examples case we use the default drawing method supplied by passing in null. You can do the same for drawing the header cells which in the examples case is null so default drawing method will be used. I have supplied a cellClickFunction which simply pops up a message box informing you which cell at which row you clicked on the grid but you can change this of course to do whatever complex business or game logic you need. The dataRowHeight is the height of each row which has to be uniform and fixed and in the example is set to 20 pixels. Same for the header row height which is set to 30 pixels. The columnWidthArray allows you to pass in an array containing the width's of all the columns in your grid. The rest of the three parameters concern how to draw the border of the grid. The last 6 parameters are all used to gradient paint the header and alternate row backgrounds.

In the case of a ListBox all you have to do is create a grid with only 1 column. That is why I have not provided a separate control for ListBox.

The data parameter is an array containing the list of strings to display in the dropdown list area. Some of the new parameters here are drawTextAreaFunction, drawButtonFunction, drawListAreaFunction which are all passed in as null in the example using default drawing capabilities. You can override these to completely customize the drawing of the ComboBox.

Some of the new parameters are alignment which is right now only possible to set it to 0 and is ignored in the code and will draw the radio buttons in a horizontal line. Later on you will be set it to 1 and have it draw the radio buttons vertically stacked. The groupname parameter is the name of the radio button group for easy identification and is test1 in the example. The labels parameter is an array containing the labels text for each radio button and will be drawn in the order supplied. The selectedid parameter is the index into the array to start with that is selected.

The new parameters here is the imgurl which as it suggests is the URL link path to the image to draw and clickFunction is the function to be executed when the image is clicked which in the case of the example pops up a message 'You clicked the image'. Again this image like button and label can be turned into hyperlinks. Look at button for a description of how to specify the optional parameters.

The nodes parameter replaces the data parameter from the old version. You do not create this yourself any longer you instead use the helper function addChildNodes(nodes, parentnode, imageurl, expanded, label, customextrainfo). Also each node format is { TreeviewNodeInstancesParentNode: parentnode, TreeviewNodeInstancesRootNodes: nodes, ImageURL: imageurl, Expanded: expanded, ChildNodes: new Array(), Label: label, CustomExtraInfo: customextrainfo }. This is self explanatory. The clickNodeFunction is the JavaScript function to execute when a node is clicked which in the case of the example just pops up a message giving the id col index value of the node. Now the node array can have an additional element the URL of an icon image to display so for example a node will be [2,1,'Test',1,'MyImage.png']. If you are using icons then hasicons should be 1 and you must provide width and height and all icons therefore obviously must be same width and height or there will be dispay errors.

Color is the color of the progress bar. The maxvalue parameter is the maximum value or when progress is 100% which is the width of the control. The minvalue parameter is 0 in this example but if you wanted to treat say 30 out of 100 as showing a progress bar at 0% which is no length then this parameter can be useful. The currentvalue is the value of the progressbar and you can use the setProgressBarCurrentValue function to change this value whenever you want to show a different percentage of progress the progressbar is measuring for you. The windowid parameter for this function is returned by the createProgressBar function so you can save this in a variable and pass it in when needed.

You can specify the size of the handle on the slider throught height and handlewidth parameters. The maximum value and the minimum value and the current value which is the position of the handle on the slider have to also be provided as parameters.

The panel creation first example is nothing other than a rectangle with a gradient filled as specified by the parameters. To add child controls to the panel you use the registerChildWindow function. In this case I have attached a slider and a progress bar controls to the panel. As you will notice in the screenshot below they are clipped to the region defined by their parent control the panels boundaries. The second and third examples show how to make an expandable collapsable panel. The second example starts the panel expanded and the third collapsed.

The month and year is supplied as full month name example July and year number example 2012 through the visibleMonth and visibleYear parameters. The selectedDay parameter when supplied will highlight that day on the calender as being selected which changes as the user clicks on a day. The width and height of a day cell is supplied through dayCellWidth and dayCellHeight parameters. The height of the header where the month name and year with buttons is supplied by the headerHeight parameter. dayDateActive... parameters supply the information to draw the dates for days that are in the visible month. dayDateInactive... parameters for those dates on the calender that are not in the visible month. today... parameters supply how to draw today's date. The ondayClickFunction supplies an ability for you to do custom logic when a day is clicked on the calender. dayLabel... parameters supply how to draw the header names of the days example Mon, Tue, etc.

All the parameters are pretty much same as calendar control. The difference is the calenderHeight and not height determines the dropdown calendar height. The height defines the height of the textbox area and corresponding size of the button which is square. Three new parameters textboxAreaTextColor, textboxAreaTextHeight, and textboxAreaTextFontString all define how the text which is the selected date in dd/mm/yyyy format will be drawn.

A screenshot of the datepicker control with the dropdown calender showing is provided below:

The data parameters is an array of arrays. Each bar is defined by a label, a number and the color of the bar. The maxvalue is the parameter for the maxvalue of the y-axis. The nummarksy is the number of marks on the y-axis. Title parameters supply how to draw the title. The barwidth is the width of each bar drawn per label supplied in the data array. The axislabel... parameters supply how to draw the axis labels and the label inside each bar. The marginleft parameter is how much space to leave to the left of the y-axis. The gapbetweenbars parameter defines the gap between each bar. The barClickFunction allows you to do some custom action when a bar is clicked. In the example it pops up an alert which gives the label text for the bar clicked.

The data parameters is an array of arrays. Each pie slice is defined by a label, a number and the color of the pie slice. The actual size radius of the pie is calculated as the height of the control minus the title text height minus two times the label text height minus 24 which is basically a height spacing of four pixels around title and bottom and top possible labels. So if you want a smaller or bigger circle just adjust the height of the window. The slice click function is what is called when the user clicks on a pie slice and you are provided with the canvas id, window id and the index into the data array for which data elements slice was clicked. In the example it pops an alert saying you clicked GM, Ford or Chrysler's pie slice.

A screenshot of the example pie chart follows with no slice selected i.e. clicked on by the user:

A screenshot follows for the Chrysler pie slice clicked on by the user for the example:

To add an animated multi line graph control to your canvas you would make the following JavaScript call from your web page after the canvas element on which you mean to draw the multi line graph as follows:

The data parameters is an array of arrays. Each line is defined by an array of x, y values and a color of the line. You can provide multiple such sets for multiple lines or just one set for one line. The click function gets a value i which is the index of the line set that was clicked on. So you can execute custom logic depending on which line the user clicks on which is a different data set.

A screenshot for the multi line graph example with last optional parameter missing as this uses numbers and not labels on the xaxis follows:

The screenshot for the second example which uses labels on the x axis so it sets the last optional parameter to 1:

The data parameter is an array of arrays. The first element in the main array which is 0 in the example is the minimum value and the second which is 100 in the example is maximum value. The second element in the main array is an array which defines the first arc from left which in the case of the example goes from value 0 to 30 and is of color red fill. The third element is an array which defines the second arc on the gauge which is from value 30 to 60 range and is yellow. The next element is again an array defining the last arc which in the example is from value 60 to 100 and is green. The next element in the main array is 10 which is the number of major marks and the next one 5 which draws marks between each major mark. Finally the last element is the value to point at which in the case of the example is 80.

The data parameter is a simple list of numbers which will be drawn out one on each axis per number. The maxvalue parameter should be the maximum value for one axis. The colorstr is the color you want to paint for the radar region.

To add an animated line area graph control to your canvas you would make the following JavaScript call from your web page after the canvas element on which you mean to draw the line area graph as follows:

The data parameter is an array of arrays which start with the x value and the set of y values for that x value in an array. The limitation that each x must have consistent number of y values. The second element of the main array is a array of colors to be used for the regions and must be the same number of colors as the number of y values per x value. If you want to have labels i.e. text on the x-axis as in the second example then just provide islabeledonxaxis as 1 and if you are providing numbers then don't supply it as all as in the first example which uses numbers on the x-axis.

A screenshot of the example follows for the first example with numbers:

A screenshot follows for the second examples which uses text on the x-axis values:

Notice the only difference between the two charts is the labels on the x-axis because the x values are different between the two charts.

The data parameter is an array of arrays in which each array has an open, close, low, high values. If the open value is less than the close value then the candlestick will be filled if not it will be outlined. You can provide a color for the candlesticks.

The data parameter is the same format as it was for pie charts so please reference that. The inner radius parameter defines the inside radius of the hole in the middle. As doughnut chart is essentially a pie chart with a hole in the middle of it. marginsides is how much space to leave to the right and left of the doughnut drawing. The legend width parameter is how much space on the right side to use for the legend. The sliceClickFunction is raised when the user clicks a slice and the parameters are the same as pie chart.

A screenshot of the example with no slice clicked is shown below:

A screenshot of the example with the Chrysler slice clicked is shown below:

The data parameter is an array of arrays one per stacked bar starting with the label for that bar and then an array of values which determined the length of the bar versus the maximum value supplied by maxvalue parameter and the color of that bar segment. It is a bit tricky to adjust the width of the bars so what you want to do is if you want a wider bar increase both barwidth and gapbetweenbars parameters whoever always gapbetweenbars should be less than barwidth. In this example the barClickFunction supplied simply gives a message displaying the bar label for the bar that was clicked.

To add a bars mixed with labeled line chart control to your canvas you would make the following JavaScript call from your web page after the canvas element on which you mean to draw the bars mixed with labeled line chart as follows:

The tablabels is an array of strings as each tab must have a name. The number of tabs is the length of the tablabels array in the order specified in the string list. The selectedTabID is the index into the tablabels array for the starting tab.

In the example the first thing you do is save the Window ID of the tab control in the example in a variable called tabwindowid. You then get all the properties of the tab by using getTabProps. Then for each control you want to add to a particular tab you call registerChildWindow. This function takes the canvas id name as first parameter, the second parameter is the Window ID of the child controls window which in the case of the example is a direct call to create an image control and the last parameter is the tablabels array tab index into the PanelWindowIDs property of the tab properties you received. Because each tabs control area is a panel which the tab maintains by changing its hidden status on clicking the tab label header. In the case of the example you can check that only the image for that particular tab is shown plus when clicked it gives the correct alert for the image clicked on the tab control.

The first thing of course is to supply a url to load the image from. In this case I use an India map since I am from that country however you can use any image does not have to be a map that you need to put pins or pan and zoom. The pinxys is an array in which each element of the array is [x, y, radius, color]. The x and y is relative to the top left corner of the image being 0, 0. You can use Gimp or any image manipulation software to find the x, y pixel co-ordinates within the image to supply for each point you want a pin. The radius is of course the radius of the pin to be drawn at that x, y co-ordinate on the image. The pinClickFunction is invoked when the user clicks a pin and you are supplied with the standard canvas id and window id and an index i into the pinxys array. In the case of the example I make it simple by just saying the name of the city whose pin you clicked on. If hasZoom is 1 then you will be able to zoom in and out with the mouse wheel. This will work with any browser such as Google Chrome which supports onmousewheel event which is what I use. The imagetopleftxoffset and imagetopleftyoffset is the point on the image to start showing the portion of the image in the window at startup. Again it follows the rules of pinxys co-ordinates. Scale is whatever scale you want to startup with if the scale is too small or your starting window into the image is off image you will not see an image at all so be careful and pick meaningful values i.e. ones which will crop a portion on the image without going off it. The scaleincrementfactor is by how much per mouse wheel move do you want to zoom in and out so in the example I choose 0.1 so it scales slowly on mousewheel as my image for the example is not very large.

The data format is complex because each item in the menu's text can be configured to any font and size specification. Basically each menu item is defined as an array whose first element is the text label to display in the menu. The second element is the color of the text. The third element is the height of the text. The fourth element is the font specification. The fifth element always set it to 0 as it is used internally by the code to determine if the menu items sub menu if any is to be displayed. The fifth element is the function to invoke when the user clicks the menu item. The sixth and final element is an array of the same menu item element arrays as defined here. If the item has no sub menu with child menu items then set this sixth parameter to null. The orientation parameter is not implemented as yet but that allows for menu bars on the bottom and sides when implemented.

A screenshot of the example menu bar with a sub menu item clicked on is shown below:

The Watermark properties define what the textbox will look like when it is empty which will display the watermark message. The Text properties are information on how to draw the text the user types into the textbox. The maxChars limits how many chars you can type in. The allowedCharsRegEx is a regular expression against which whatever the user types will be checked character by character. If its an invalid character it wont effect the value of the text. The Password properties are if the text should be hidden and what letter to use. The border properties are used to draw a border around the textbox. The Shadow doesnt work right now but is meant to put a dropdown shadow for the control. The Edge properties define if the textbox will have rounded corners. The gradient properties define if you want a gradient for the background of the textbox. You can use the image properties to insert an image into the background of the textbox. The auto completion parameters limit the user optionally to only type values of text from the given array of strings. The initialText can be supplied with some text to initially display as though the user typed it. The caretColor is the color used to paint the caret. The selectionColor is used to paint the background of the selection. If you want this text box to have focus on the form initially set the hasFocusInitially to 1.

The imageURLs is an array of URL's to the images to display. The fadeStart value is an alpha number which is between 0 and 1 and determines the extent to which the image is faded out before the next image is displayed. The fadeEndValue is again between 0 and 1 and this is the maximum or final alpha value the image will be displayed at. The fadeStepValue is how to increment which basically determines the fade rate of the image i.e. how fast or slow it will fade. The holdForTicks is the number of ticks draw calls the picture will display at maximum alpha which in my example is completely opaque 1. The clickFunction is your custom code to do when the current image is clicked in the example I alert the URL of the image that was clicked on basically given access to the properties and the index of the image which is current which was clicked you can do anything including a server side postback. The overlayimages is 1 if you want the previous image to fade smoothly out into the current or next image.

The imageURLs is an array of URL's to the images to display. The direction is either 1 or 0. If it is 1 then it will slide the images sideways if it is 0 it will slide the images vertically. If stepIncrement is positive and direction is 1 then it will slide rightways if it is negative then will slide leftways. If stepIncrement is positive and direction is 0 then it will slide it bottom to top and if negative top to bottom. The holdForTicks is the number of ticks to hold the image before it begins to slide again. In the example clickFunction an alert message is displayed with the index of the image which is index into imageURLs array.

To add a boundary fillable map control to your canvas you would make the following JavaScript call from your web page after the canvas element on which you mean to draw the boundary fillable map control as follows:

The fillpoints is an array of arrays. Each array has the format of the first 2 elements being the x, y co-ordinates on the map to start filling at. The next 2 elements are the x, y co-ordinates top left corner of the bounding box within which to do the filling and of course the next 2 elements are the x, y co-ordinates of the bottom right hand corner of the bounding box. The next 4 elements is the RGBA in 0 to 255 values of the original color and of course the next 4 elements are the RGBA color to replace it with. The image WorldMap.png is an outline drawing with countries borders lined out and my image was otherwise white background. Of course the filling algorithm is available in a seperate function should you want to combine this with any control if you so choose to. Since there is already a map control which is scalable, zoomable and with pins and labels I did not make this map control do all that as it can be easily combined if you read the code.

If you want to do partial fills then the fillpoints array element has an additional 3 elements the first is orientation which if 0 fills horizontally along x in which case of course the next to elements are the x minimum and x maximum to fill upto. Be careful with the number since I am giving you full control you must know your points within the image or it will lead to unknown results. Similarly if orientation is 1 then next element is y minimum and thereafter y maximum. Basically the starting point you provide for you partial fill should be between the bounds horizontal x minimum and x maximum or vertical y minimum and y maximum or it will produce unknown results.

The following example is how to fill horizontally from left to right for america only in blue color:

To add a multi line label control to your canvas you would make the following JavaScript call from your web page after the canvas element on which you mean to draw the multi line label control as follows:

createMultiLineLabel(elemId, 'mll1', 250, 400, 200, highestDepth, 0,
'This is a very long sentance to test word wrap. My name is \nAkshay Srinivasan\n and ' +
'this is a multi line label control demo. It is a control that is one of the many controls ' +
'in Canvas Control Library which is a system of building web pages that has been made ' +
'possible by the new HTML5 element called canvas.', '#000000', 12, '12pt Ariel', 5, 1);

createMultiLineLabel(elemId, 'mll1', 250, 800, 200, highestDepth, 1,
'<NT>This is a </NT><N><C>#FF0000</C><F>16pt ' +
'Ariel</F><T>very long</T></N><NT> sentance to test word wrap. ' +
' My name is </NT><N><C>#00FF00</C><F>bold italic 14pt Tahoma<' +
'/F><T>\nAkshay Srinivasan\n</T></N><NT> and this is a multi line label ' +
'control demo. It is a control that is one of the many controls in Canvas Control Library which' +
' is a system of building web pages that has been made possible by the new </NT><N>' +
'<C>#0000FF</C><F>10pt Ariel</F><T>HTML5</T></N><NT>' +
' element called canvas.</NT>', '#000000', 16, '12pt Ariel', 5, 1);

This control auto determines the height so this is one of the few controls where you don't specify the height. The hasMarkup if set to 0 will then render the text parameter as is. If it is set to 1 then it will assume you have markup in the text parameter which is a bunch of tags which you can see in the second example:

<NT> - This is normal text which means it will use the text color, height and font from the parameters supplied to the function call.

<N> This means this is specially formatted text which has a bunch of tags with which to specify the text formatting:

<C> - This is the color of the text

<F> - This is the font string

<T> - This is the actual text string

Also to specify a line break you can use \n in the text string.

The lineSpacingInPixels is the gap between the lines to add. The wordSensitive parameter if set to 1 will take a word which is too long to fit at the end of the line and look for the space before it and break it at that point onto a new line. If this is set to 0 then words will break in the middle on a new line with no dash.

This control allows you to now be able to make regular paragraph like text without resorting to regular HTML5 text so that you can stay within the system.

Screenshot of simplex text (not marked up) follows:

Screenshot of marked up text that is text with different font metrics and colors:

The word processor control is essentially a multi line text box. As of now marked up text is not available so hasMarkup has to be 0 until the markup code is functioning. So it is plain text as defined by the textColor, textFontString metrics supplied. Works much like textbox control but is multi line instead of just single line. In the example I allow any characters by setting allowedCharsRegEx to .+. One of the limitations is that the height of the word processor/multi line textbox has to be for optimal presentation results a multiple of double the margin plus number of lines multiplied by the sum of text height plus the line spacing in pixels.

To add a virtual keyboard control to your canvas you would make the following JavaScript call from your web page after the canvas element on which you mean to draw the virtual keyboard control as follows:

The virtual keyboard control has an array keys which defines the keys the keyboard will layout for you. A key is defined as an array in which the letter is first element, then width of the key, height of the key and optionally an index to another keyboard. A keyboard array is an array of rows which holds arrays of keys as defined previously. The keys array is finally an array of keyboard arrays. So this way you can index into your keyboard array to have special keys that switch keyboards. I recommend as in the example you pass in null for keys which will get you the default keyboard. The example defines a simple keypress function which alerts the letter of the key pressed on the virtual keyboard.

A screenshot of the virtual keyboard is shown below:

Screenshot of virtual keyboard after '12#' button is pressed which indexes off to another layout for the virtual keyboard is shown below:

Additionally checking goes on for textbox and word processor control if the browser is on a Android or iPhone/iPad/iPod and will display accordingly. However caution is to be taken when using this as response on old devices such as iPad2 and even an HTC Sensation XL Android phone is bad. Maybe on more powerful devices such as Samsung Galaxy S3 and iPhone 5 it might be usable. But I do not have access or the means to purchase these platforms so some feedback from the lucky ones to own the more powerful smartphones and tablets would be nice. The problem I think is that the Javascript engine in these mobile devices browsers is not fast enough or just that processor cannot handle crunching another layer of abstraction Javascript in the mobile browser.

The new window drawing system has much improved response on my iPad2 and HTC Sensation XL Android phone is now acceptable usability wise though it is not as fast and responsive as the native browser HTML5 elements or the native keyboard.

The splitter is a line that sits between some windows horizontally or vertically. If width is greater than height it means its a horizontally aligned splitter. What the splitter will do is it will figure out what windows are within 2 pixels of its line and adjust their width or origin as the splitter is dragged.

To add a simple XML viewer control to your canvas you would make the following JavaScript call from your web page after the canvas element on which you mean to draw the simple XML viewer control as follows:

The simple XML viewer control at its bases uses the treeview control to accept some XML supplied by the user and draw it out as a tree. There is an example in the download of this which works. The XML must start with <root> tag and end with </root> tag basically well formed XML that is understood by the DOMParser in javascript is all it supports.

The parameter numstars is the number of shapes you want shown. Maxvalueofallstars if the total values of all stars being filled value. Then the color to fill the shape/stars with is provided in rgba. If you are using standard star then the size of the star its width and height are same by default. Then the spacing between shapes/stars drawn is provided. If you are partial filling a star for a fractional value then this will be 1 if not 0. One of the things to note about partial complex shape filling is that when filling horizontally the middle of the image should be completely within the outline shape provided for standard star this is the case. For the duck outline I have used what would be more appropriate is that the graphics artist raised one leg of the duck along the middle horizontal. Editable basically means clickable and value changeable on clicking. The next parameters defines the label which will be 'value out of max value of all stars'. If you want to offset the starting position of drawing the stars then provide something other than 0 for this parameter. Initialvalue is just the value to start with for filling the stars/complex shape. If you want a thicker outline line for empty stars then provide something greater than 1. Starorientation is how to draw out the stars 0 means horizontally 1 means vertically. Fillorientation is how to fill a star horizontally 0 vertically 1. Iscustompattern means you are going to provide an image with an outline shape like my duck example. Customfillpoint is defined in BoundaryFillableMap so refer there on how to construct the array. Basically you provide the point within an empty shape to fill your complex shape. Then the width and height of the image. Then the rgb value of the background color. If you want to do something custom on clicking pass in your custom click function here. Then how many decimals to round the displayed value if there is a label.

Screenshot for the examples above in that order top to bottom:

Window Draw system changed for performance on mobile devices

The new system of drawing out the windows is not to draw all the windows on the canvas over but to use the function invalidateRect to invalidate an area. All windows then falling in that area are the only ones redrawn. This change is reflected in the new animation system as well as only the animated window will be redrawn to this effect you have to supply a windowid now to the registerAnimatedWindow function call. This makes though your code more complex as can be seen in the FormsBases.aspx example which is now has more complex code logic to redraw effected windows which the system would not know got changed.

The above JavaScript uses 4 images by the name of Bombay.png, Pune.png, Madras.png, Delhi.png. You can use any images with different file names long as you change the image URLs in the switch case statement in function drawListAreaCustom.

The current concept on how forms work with web pages is that when you click for example a button that requires a post back to do something on the server side what will happen with regular web page technology is that all the controls values will be sent to the server plus in the case of ASP.NET viewstate the state of the controls as well. The control values will be unwrapped by the PHP or ASP.NET module and a php or aspx server side page and function will be invoked in which your code will make some changes. Then a whole new web page is sent back to the client web browser.

In this new system I have invented what happens is that when you do something with a control for example a button that requires some code to be executed on the server side all the controls state and values are wrapped up and sent across to the server side in an AJAX call which goes to an ASP.NET web page which has included the CanvasControlLibrary.cs file that has all the support. Your C# function whose name you provide is invoked you make all the changes to the control values and states in the example change the text of a label. Then all the controls state and values are wrapped up in XML and sent back to the same page the client browser is on since its an AJAX call and then unwrapped replacing all the old values of the control states and values with the new ones you changed server side. The UI is redrawn and the changes you made to the controls reflected on the same web page. The client never leaves the web page on a post back to the server side to go to another web page which is the normal practice.

So here is the client side code which is a button that invokes a server side C# function on an aspx page:

The label declaration in JavaScript function call is normal. The button on the other hand when it is clicked calls a JavaScript function called invokeServerSideFunction in which all the magic of a post back happens. The function first parameter is the name of web page on which your server side code is at. The next is the name of the function on the aspx page to invoke server side. The third and fourth parameters are just the canvas id and the window id to identify on the server side which control raised the event on the client side. The fifth is a JavaScript function which you code that will be invoked after the post back is completed so you can make further changes on the client side if you wish.

On the server side aspx page in your page load event you have call the server side class and then after it is instantiated invoke the server side function which is the ClickMe C# function on the page. In this example a simple call to get the label control and change its text value to Did Postback. Then you invoke the canvas control library server side class to send all your changes back to the client.

When you download and run the example on clicking the button the label is changed server side from showing text Label 1 to Did Postback. I have kept the example very simple but with this system you can create fully functional pages. In the case of wanting forms all you do is have a startup panel with your controls on it. When you click the button and submit all the information gathered you keep a second panel with all the controls on it which is the form you show and hide the first form. With this ability you can build fully functional web pages with server side post back custom code and build any current form based web pages in the new Canvas Control Library.

It uses pretty much the same page Default.php as Default.aspx as there is no PHP or C# code in either. When you click the button the postback happens to code listed above. Here all the stuff is done in PHP class file so you have to include it. You instantiate the library as a class object and do pretty much the same as in the server side ASPX page. Again only a label is changed for the demo.

This example is a web page which uses the Canvas Control Library and its forms based system to create a simple web page for a fictional Cinemas called Fake Cinemas. There is a title image logo of the cinema theater company on the top of the page. There is a menu bar which has links to various other pages which at this point is not implemented to do anything. It has movie theaters in four cities in India which are Mumbai, Delhi, Bangalore, and Chennai. The purpose of the starting page is for the customer to select the city which then populates the names of the theaters in that city in then the customer selects the theaters. When the customer selects the multiplex images showing the posters of all the movies playing at the theater and show times is displayed. If you click on a show time label it gets highlighted and a textbox control and button for payment is added. The textbox control is for the number of tickets and only accepts numbers to be typed in it. When you fill it out and click the payment button it does a server side postback and a message is displayed telling you which theater, how many tickets, what movie name and showtime you have booked tickets for. Without further ado here is the client side code:

What is going on in here is the first thing the canvas element is declared. Then the registerCanvasElementId function call JavaScript is invoked client side to initialize the Canvas Control Library system. The next thing a handy form creation function form1 is declared which creates an Image, MenuBar and 2 ComboBox controls one for the city and one for the theaters in the selected city. The function returns any valid windowid of one of the controls on it. The form1 is invoked and all the controls are instantiated. Then invokeServerSideFunction JavaScript function is called which called the server side AjaxEx1.aspx page via of course AJAX call and invokes on the ASP.NET page the C# function InitializeForm1. The next thing a JavaScript function cityChanged is declared which invokes another server side function on page AjaxEx1.aspx called onSelectCityChanged. When the user selects the multiplex then a server side call is made to onSelectCinemaChanged this sends back information in an array of the image urls of the posters along with show time information. This array is then passed to client side JavaScript function showMoviesForCinema. In here the first thing is we destroy any old controls displaying movie posters and show times for the last selection if any. We then proceed to create image control to display the first movie poster and then labels for each show time and iterate through the params array. We finally make a call to draw to show the new controls.

In page load the server side class as defined by CanvasControlLibrary.cs is instantiated. The C# function InitializeForm1 all it does is fill the Select City combo box with the name of the cities. The onSelectCityChanged function gets a reference to the select city combo box to find out which city was selected and a reference to the select cinema combo box which it then fills in depending on which city was selected. Now what will actually happen is instead of hardcoding the names of the cities and cinemas you could do a call to a RDBMS for the values and fill from there or a web service from a remote system like SAP. In onSelectCinemaChanged all we are doing is passing an array back to the client side containing the image URL's of the movie posters to display along with show times. When you click on a time label under the movie it gets highlighted and 2 new controls are added to the page a textbox to hold the number of seats and a button to purchase tickets with by doing a server side postback. The server side code simply gets the information passed from the client browser to it about which city, theater, movie, show time and number of seats builds a simply builds a message string and passes it back to the client browser which displays a success message. In a real world site you would of course go through payment gateway logic etc.

A screenshot of the page follows which will keep changing as I work on the example:

After you have selected Delhi and Delhi Nagar 1 the movies with their posters and show times are shown. Example screenshot follow below:

The following is a guide in how to do it in ASP.NET. Essentially in the web page which has your HTML and Javascript you override the render method and make a call to the static method CanvasControlLibrary.StartSession passing in the HtmlTextWriter. What this will do is hook you up with a session object. Then your ajax callback ASPX page for example I am going to use the simple ClickMe function and change it to simply check if the CurrentSessionObj.Data object has any data if not add a key and a value which is a simple counter. If it exists increment and set the labels value to it. Now as a test you can pull up the page and hit the button and the number in the label will count up by the clicks. Pull up another page or browser and do the same and you will have different number since they are different sessions and the values are different and it will display correctly. So the following is the code for the HTML/JavaScript page in the code-behind file:

This technique is a substitute if you want to use it for the normal ASP.NET sessions as it is lighter and much faster. For PHP just use regular PHP sessions which supports AJAX calls as PHP does not support global vars which will stick due to the nature of design of PHP which makes a separate process for each request.

The first thing about your control is its basically visually something you will draw in a window. Also unless the control is merely visual i.e. only presents data it might have some interaction like doing something on click events. A simple example of this is the Button control.

So when you got to create the button control the first thing you do is you setup an array to hold the controls properties. In the case of button control it is the follow array:

var buttonPropsArray = new Array();

Since there will be multiple canvases and multiple controls in this case buttons you save the properties windowid and canvasid in addition to the properties which is an object. You then need to get the properties for you control so you can manipulate it and even reference it for when you have to draw the control. So you setup a getYourControlTypeNameProps() function in the example of button it looks like the following:

Now the next thing to do is come up with a create function so that the user can create an instance of your custom control. There are some minimum parameters such as canvasid, controlNameId (this is the id name of the control used to uniquely identify the user instance of this control in the CCL subsytem), the window co-ordinates as given by x, y, width, height and the depth the layering of the window which defines which windows it will be on top of under as given by the depth parameter. So the minimum create function signature would be like the following example:

However your control just as in the example case of button will require some additional information to customize the control according to the users wishes. In the case of button it would the text to be displayed on the button various parameters such as width and height and color. Event based information as in the case of the button what would you like to happen when the button is clicked. So you create additional parameters that collect this user information and pass it along to the create control function. Also to allow Object Oriented access to the create control function you would add an OO wrapper. The naming convention is CCLCustomControlTypeName. The simplest example follows:

Why it is a wrapper is because of the Initialize parameter which just calls the underlying create control function passing in the parameters. You can allow the end user of your control to use OO creation of your control or directly call the javascript function both ways will be supported. If you want to provide defaults do not put them in the OO prototype property level instead code for it inside the create control function call so both ways will be supported.

The process of create control function call is essentially setup the window this is a call to createWindow function call in the example of button it is the following:

One thing to note here is the string 'Button'. You should name your control something that the underlying system will use to identify a control of your type. You want to save the window id which uniquely identifies the controls instances window and pass it back at the end of the create control function. The next thing to do is fill the passed in parameters to an object with properties and then add that to the array you created to hold the control properties. In the case of the button control it is the following:

Now you have the minimums of the control at this point. But it does nothing as it doesnt draw anything. So the most important thing any normal control will want to do is draw something that is a representation of that control the end user can see on the web page. For this you call the registerWindowDrawFunction which accepts the canvasid, windowid and a function that will be called everytime CCL needs the control to be redrawn on the web page or canvas element it belongs to. You can also do an optional thing in your parameters to create control function you can provide the user of the control with an optional custom drawing function they can pass in which will be called instead to draw the control. The button example is the following:

You can draw your control using the normal javascript canvas drawing functions as per HTML5 specification. Try here to stick to the minimum that all browsers support. Then in the case of the button example you have a click function which basically does the action specified calls a user supplied click function to execute or acts like a hyperlink calling another web page etc. One small discussion I will include which the click button example does not provide which you will need in more complex controls is to figure out if the x y coordinates of the click falls within a rectangle. You should always divide your controls different clickable areas into rectangular regions. In many controls I have created I used the ClickExtents array which I fill in the draw function. You then loop through these checking if the click fell in the rectangular region for example in the following code:

The logic is simple if x is greater than the topleft corner of the rectangular areas x co-ordinate and less than the topright coordinate x value then it falls inbetween which means there is a click hit in the x axis. The same check is done for y co-ordinates. If both are tru you have a hit in the rectangular region and now you can perform some click function code specific to the control.

The other thing when dealing with objects that are self referential do not include them in the properties of the control which will go through the postback system as it will die due to infinite looping. The other thing not to include is the actual image binary data or any kind of binary data in a property that will go back thought the postback system. Instead store such data locally as they will always be available as we never move off the page and wont be lost during ajax postback calls.

So the postback system is first step there is a call to invokeServerSideFunction which cause the javascript on the client side do to an ajax server side call. It then calls getEncodedVariables to wrap all the properties and their values in all the control arrays and send them as POST data on the ajax call to the server side. So the first thing you have to do for your control is add code in getEncodedVariables function so that your controls properties and values are sent across as well. So for example in the case of button here is its code addition to work with the postback system within getEncodedVariables function call:

Then we arrive on the server side and all this data is unwrapped for use in say C#. So now you have to modify CanvasControlLibrary.cs file. This happens in the UnwrapVars function. In the case of button the code is:>/p>

Notice something that is going on the name of the button control properties is called buttonPropsArray. So on the javascript side we did in the pseudo xml '[buttonPropsArray]' so now what the C# server side code is doing its keyed into this as a XML node name which it uses reflection to then recurse over a class which you have to define in the case of button it is CCLButtonProps and also you have to create an array to hold it which button does and code for button is the following:

Now sometimes we pass back arrays and sometimes even objects so for objects define their type as not string but List<object> and for javascript objects as LightWeightDictionary types. You can also see object is fine too as Tag property of button uses this. It does not matter as all the reflection code logic will handle the rest transparently for your control.

After whatever happens custom C# logic on the ajax aspx page holding your end user of the controls code it is time to wrap up all the controls and send the long pseudo xml string back to the client that made the ajax request. All that action happens in SendVars. So now of course you have to add support for your control in this function. The button example code is the following in the SendVars function:

The other thing that will need to be changed is the function getControlPropsByControlNameID. This basically allows the user to pass in the unique control name id and get back the properties in the server side code. In the case of button example here is the code:

Now you come back to the client the web browser and the javascript unwraps the XML into the controls on the web page setting their properties etc. So here to you have to modify the javascript client side function UnWrapVars. Here there is logic to restore lost properties that had function logic which was saved before the ajax postback call was made and now needs to be restored. So there is a loop in which you need to add support for your control. An example of this is shown below for button:

If you are making a control that needs tab stopping then in your creat control function add a parameter tabstopindex and pass it into the createwindow function call for your control. If you have a complex control like combobox which has multiple windows then you also need to pass in to the createwindow function your windowid for the main window that represents your control. In the case of combobox its the textareawindowid where the text is that you selected or was preselected for you by default. You also need to update your OO call for the new parameter and save the parameter to your properties for the control instance. So this means on the server side you will have to update the class definition for your control's properties to also include this new tabstopindex property. Then your control will have tab stop support.

Now you have a fully supported control. It will draw to the canvas web page it will interact with the user it will send information back via the ajax postback system to the server side for additional manipulation and the changed data and properties will be reflected back from the server side to the client side web page.

This is the process to create a custom control for Canvas Control Library as while I have provided many controls you will always need your skies the limit of your imagination custom control for your unique web page and now you can do it easily.

Latest version as of 12 August 2012 of Google Chrome, Safari, Opera, Firefox, SeaMonkey, Maxthon, and IE9/IE10 are supported which basically means all major browsers are supported now. For a easy visual summary the icons of the supported browsers is shown below:

There is support for iPad, iPhone, and iPod now.

For IE9 to work you have to include the following meta tag in your web page HTML: