technical stuff, hopefully useful to some
onsdag den 8. maj 2013
Kinect Painting demo - simple setup
I went looking for a simple Kinect demo in which the user's right hand would be tracked and used as a paint-brush. I found some example projects here and there, but they were either rather elaborate with a bunch of functionality not required for a simple demo, or using third-party dll's.
So I put together my own simple thing, which I'm making available for download here.
I must confess to being not particularly impressed with the Kinect itself. The premise of the device is extraordinary, and it does function as the product description promises, but I found it slow in updating. It cannot in its present version be used as a "live" paint-application, as there will be a too noticable delay in its registrering skeleton movement. The LeapMotion device promises more and is probably the way to go for this, until a better, faster Kinect version arrives.
mandag den 29. april 2013
Javascript "static" class - make javascript code more readable
I usually use static classes for my C# helper-functions, when there're no properties to expose, nor any need to inherit from them. So this increases code readability. Javascript, however, doesn't have that feature, so how to immitate it? I usually do the below:
// declare object - recall that 'function' is an object in javascript
var WebServiceFunctions = function() {
}
// declare method on the object
WebServiceFunctions.webServiceMethod = function (parameter1, parameter2) {
// method code here
// return value if any
return xyz;
}
With the above code, I call my helper-method in my code like this:
var resultFromCall = WebServiceFunctions.webServiceMethod("foo","bar");
This has the odour of a static class. Of course behind the scene the "var WebServiceFunctions = function() {}" declaration signifies the creation of an object ("function" is an object in javascript), but I find that given the generic name - 'WebServiceFunctions' - my mind abstracts from this.
The javascript increases in readability, which may be an issue with a multitude of referenced javascript files.
There's a slightly better way, though. If the helper-functions are so generic in nature that they're applicable to many different purposes, they may be used in many different files. As such, the above will declare the "WebServiceFunctions"-object with every import of the javascript file. Better would be to check beforehand if the object has already been created, in which case there's no need to create it again:
(function () {
this.WebServiceFunctions = this.WebServiceFunctions || {};
var ns = this.WebServiceFunctions;
ns.getMobilityPeriodId = function (serviceUrl) {
// function code here
}
})();
The above this.WebServiceFunctions = this.WebServiceFunctions || {}; is a conditional that checks if this.WebServiceFunctions has any value, in which case the already created object is returned, or - represented by the two pipes '||' - a new object is created. var ns is merely a reference, to avoid having to write 'this.WebServiceFunctions' repeatedly.
So that's a way to reference javascript in a 'namespace' sort of way. I find the above helps me keep my references handy and maintain the readability of my code, especially as a project starts to become clotted with javascript. If you wish to go depper into the subject matter, the above is what is known as the Javascript Module Pattern.
tirsdag den 23. april 2013
Simple jquery stacked bar chart
One way of doing an easy and simple stacked bar chart is to style a DIV to a desired length, then have its left-border represent the second data range. If you need more ranges you can always stack several DIVs on top of each other.
Here's example HTML for the graph itself...:
And here's the annotated javascript to add the data as stacked bar charts:
Below is my CSS for the 'Graphs' contaner and the 'mainGraphBarDiv' elements:
The above HTML, javascript and CSS renders like the below:
Here's example HTML for the graph itself...:
<div id="Graphs">
<h2>How are we doing?</h2>
<h4> Exchanges at a Glance</h4>
<p style="clear: both; padding-top: 1em;">
The below bar charts indicate, percentage-wise, the total exchange grant as compared to the allotted application grants.
</p>
</div>
And here's the annotated javascript to add the data as stacked bar charts:
// dummy example data
var exchangeGraphData = new Object([
{ allottedGrant: 70, totalGrant: 90, exchangeName: "exchangeNo1" },
{ allottedGrant: 30, totalGrant: 40, exchangeName: "exchangeNo2" }]
);
// render it
if (exchangeGraphData != false) {
// get width of the graph container. This will ensure our stacked bar charts do not exceed the container width limitation
var widthOfMainBar = Number($('#Graphs').css("width").replace("px", "")) - 16 /* 1em */;
// iterate over the example data
$.each(exchangeGraphData, function () {
// add a stacked bar chart for each 'record'
var div = document.createElement("div");
// add a class-attribute to the bar chart, so as to enable styling it
div.setAttribute('class', 'mainGraphBarDiv');
// add the bar chart to the container
$('#Graphs').append(div);
// establish fill-percentage for 'sub-bar', i.e. the second data range
var fillPercentage = (Number(this.allottedGrant) * 100) / (Number(this.totalGrant));
// translate the fill-percentage into actual pixels
var fillPixels = widthOfMainBar * (fillPercentage / 100);
// ... and apply those pixels as a left-border to the bar chart
div.style.borderLeft = fillPixels + "px solid #00f500";
// lastly, subtract the same number of pixels from the bar chart's width, or the added border will extend the bar chart unnecessarily
div.style.width = (widthOfMainBar - fillPixels) + "px";
});
}
// apply a little animation, if so desired
$('.mainGraphBarDiv').slideToggle(1500);
Below is my CSS for the 'Graphs' contaner and the 'mainGraphBarDiv' elements:
#Graphs {
position: relative;
float: left;
width: 350px;
text-align: center;
margin-top: 5em;
border: 1px dotted white;
padding: 16px;
background-color: gray;
}
.mainGraphBarDiv {
color: #0d0d0d;
text-align: center;
display: none;
height: 3em;
margin: 1em;
background-color: #92ff92;
}
The above HTML, javascript and CSS renders like the below:
How are we doing?
ERAMOB-2012-041 Exchanges at a Glance
The below bar charts indicate, percentage-wise, the total exchange grant as compared to the allotted application grants.
tirsdag den 16. april 2013
iTextSharp - multi-line form filling
For those of us using iTextSharp to generate dynamic PDF documents, one caveat I found was in controlling line breaks. In as much as the PDF standard has supported RTF since version 6.1, I was trying to add line breaks via RTF-code - "\par", i.e. Yet it turns out the iTextSharp parser appreciates the ever reliable System.Environment.Newline.
So format your PDF form field thus...:

... and you'll be able to reference your field and add line breaks thus:
That's all there is to it.
So format your PDF form field thus...:

... and you'll be able to reference your field and add line breaks thus:
PdfReader reader = null;
AcroFields af = null;
PdfStamper ps = null;
reader = new PdfReader(new RandomAccessFileOrArray(Request.MapPath(@"~/App_Data/your_pdf_document.pdf")), null);
ps = new PdfStamper(reader, stream);
AcroFields af = ps.AcroFields;
af.SetField("Your PDF form field name", @"This line is " + System.Environment.Newline + "broken");
// make resultant PDF read-only for end-user
ps.FormFlattening = true;
// forget to close() PdfStamper, you end up with
// a corrupted file!
ps.Close();
That's all there is to it.
onsdag den 3. april 2013
Having an astable trigger a monostable
I wanted to have an astable pulse trigger a monostable, using the 555 timer. The main issue here is in that the trigger-pulse that's sent to the monostable 555 can't be longer than the output pulse, unless you add a 'trigger guard' of sorts.
The below schematic does the trick:

The 555 on the left is the astable, it pulses every approx 4-5 seconds. Its output is sent to the input of the 555 to the right, the monostable, which pulses for a significantly lower duration as opposed to its input pulse.
This could be used for an egg-timer, for example, where a variable resistor sounds a buzzer at a selected interval.
mandag den 25. marts 2013
GridView + DetailsView + no update
Updating a GridView based on values changed in a DetailsView, but not finding the changes reflected in the GridView?
In the DetailView's ItemUpdated-method, make sure you have the the GridView.DataBind(), and make sure that in your Page_Load the initial databind, if any, is wrapped in an 'if (! Page.isPostBack)' clause. Which I didn't have, for specific reasons yet preventing the GridView update.
Hope this helps some-/anyone.
In the DetailView's ItemUpdated-method, make sure you have the the GridView.DataBind(), and make sure that in your Page_Load the initial databind, if any, is wrapped in an 'if (! Page.isPostBack)' clause. Which I didn't have, for specific reasons yet preventing the GridView update.
Hope this helps some-/anyone.
mandag den 11. marts 2013
Adobe Edge Animate - HTML animation made easy
I used the Adobe Edge Animate product to create a fairly advanced - at least compared to some of my previous efforts! - animation for an electronics website, and I wanted to share my favorable impression of it, so here it is.
I'm not a animater by any stretch of the imagination, but I have in my time done a very few number of very small animations, never more than favors to friends for their personal websites and such. Yet all my good intentions always came to naught when I did not have the time or the tools to build an animation as I'd pictured it in my mind: Edge Animate is the first tool I've found that enables me to do just that, and design a reasonable animation in a fraction of the amount of time I'd hitherto spent.
It's still the same windows.setTimeout and such - it's just wrapped in Adobe's genious timeline-friendly fashion.
Check it out below, and if you're curious, here's a link to the animation I made.
I'm not a animater by any stretch of the imagination, but I have in my time done a very few number of very small animations, never more than favors to friends for their personal websites and such. Yet all my good intentions always came to naught when I did not have the time or the tools to build an animation as I'd pictured it in my mind: Edge Animate is the first tool I've found that enables me to do just that, and design a reasonable animation in a fraction of the amount of time I'd hitherto spent.
It's still the same windows.setTimeout and such - it's just wrapped in Adobe's genious timeline-friendly fashion.
Check it out below, and if you're curious, here's a link to the animation I made.
Abonner på:
Indlæg (Atom)