I am planning to explore various possibilities of translation from high-level languages to JavaScript, following the philosophy “JavaScript = Web Assembly”. JSIL seems promising, there are some impressive demos available.
My very first attempt seems to be working. To see it in action, open up the “try JSIL” page, paste my C# code and click “Compile & Run”. Have fun.
using System;
using JSIL;
using JSIL.Meta;
public static class Program {
public static int WJulia = 384;
public static int HJulia = 384;
public static double kat1;
public static double kat2;
public static int cx, cy;
public static void Main () {
dynamic document = Builtins.Global["document"];
dynamic window = Builtins.Global["window"];
var body =
document.getElementsByTagName("body")[0];
var canvas = document.createElement("canvas");
canvas.width = WJulia;
canvas.height = HJulia;
var ctx = canvas.getContext("2d");
var img = ctx.createImageData( WJulia, HJulia );
body.appendChild(canvas);
window.setInterval(
(Action)(
() =>
{
Loop();
Redraw(ctx, img);
}),
25);
}
public static void Loop()
{
kat1 += 0.017;
kat2 += 0.021;
cx = (int)(981 * Math.Sin(kat1));
cy = (int)(983 * Math.Cos(kat2));
}
public static void Redraw (
dynamic ctx,
dynamic img)
{
var px = 0;
int i = 0, j = 0;
int x, y, x2, y2;
int c = 0;
for (i = -2304; i < 2304; i = i + 12)
{
var py = 0;
for (j = -2304; j < 2304; j = j + 12)
{
c = 0;
x = i;
y = j;
x2 = x * x;
y2 = y * y;
while (((x2 + y2) < 4000000) && (c < 31))
{
c++;
y = ((x * y) / 512) + cy;
x = ((x2 - y2) / 1024) + cx;
x2 = x * x;
y2 = y * y;
}
SetPixelColor(
img.data,
4 * (py * WJulia + px),
255,
(byte)(8 * c),
(byte)(8 * c),
(byte)(255 - c)
);
py++;
}
px++;
}
ctx.putImageData( img, 0, 0 );
}
public static void SetPixelColor( dynamic pix, int offs, byte a, byte r, byte g, byte b)
{
pix[offs + 0] = r;
pix[offs + 1] = g;
pix[offs + 2] = b;
pix[offs + 3] = a;
}
}
No comments:
Post a Comment