How to import properties of external API in Script #
I am using Script # inside Visual Studio 2010 to import the API for an HTML5 Canvas element.
Works great for things like FillRect (), MoveTo (), LineTo (), etc. I have declared the following interface and then I can generate code in C #. Then Script # converts it perfectly to JavaScript.
public interface ICanvasContext
{
void FillRect(int x, int y, int width, int height);
void BeginPath();
void MoveTo(int x, int y);
void LineTo(int x, int y);
void Stroke();
void FillText(string text, int x, int y);
}
I want to include a StrokeStyle property that accepts a simple string, but I can't see how to do that with the interface. The following interface properties create a prefix in JavaScript, causing it to crash. The resulting JavaScript will not conform to the HTML5 Canvas API.
string StrokeStyle { get; set; }
string Font { get; set; }
The previous property will create this JavaScript:
ctx.set_strokeStyle('#FF0');
How can I get Script # to create simple canvas context assignment properties without the get_ / set_ prefix?
a source to share
Received! I used an interface that works for some cases, but when I needed this field, I had to switch to an abstract class to avoid getting a compilation error.
public abstract class Canvas : DOMElement
{
public abstract CanvasContext GetContext(string dimension);
}
public abstract class CanvasContext
{
public abstract void FillRect(int x, int y, int width, int height);
public abstract void BeginPath();
public abstract void MoveTo(int x, int y);
public abstract void LineTo(int x, int y);
public abstract void Stroke();
public abstract void FillText(string text, int x, int y);
public string StrokeStyle;
public string Font;
}
These two abstract classes allow me to use the HTML5 Canvas API from Script #, for example:
public class MySample
{
public void DoWork(string canvasId)
{
Canvas canvas = (Canvas) Document.GetElementById(canvasId);
CanvasContext ctx = canvas.GetContext("2d");
// ctx.FillRect(10,20,100,300);
ctx.BeginPath();
ctx.MoveTo(10, 10);
ctx.LineTo(100, 300);
ctx.MoveTo(20,10);
ctx.LineTo(559,300);
ctx.StrokeStyle = "#F00";
ctx.Stroke();
}
}
a source to share
One quick note -
With script # 0.6, which is now available and available for download at http://projects.nikhilk.net/ScriptSharp , you will see the Canvas API out of the box in Script.Web.dll.
Hope it helps.
a source to share