.NET to Javascript compiler

C# code   Html page   Start
class Cube : IDisposable
{
    internal static IDisposable CreateInstance(HtmlCanvasElement canvas, HtmlElement copyright)
    {
        copyright.InnerHtml = "<span></span><a></a>";
        copyright.Children[0].InnerText = "Source code provided by ";
        HtmlAnchor anchor = (HtmlAnchor)copyright.Children[1];
        anchor.InnerText = anchor.Href = "https://www.tutorialspoint.com/webgl/webgl_cube_rotation.htm";
        return new Cube(canvas);
    }

    readonly HtmlCanvasElement canvas;
    readonly WebGLRenderingContext gl;
    readonly UInt16[] indices = new UInt16[] {
        0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7,
        8, 9, 10, 8, 10, 11, 12, 13, 14, 12, 14, 15,
        16, 17, 18, 16, 18, 19, 20, 21, 22, 20, 22, 23
    };

    readonly Matrix4 view_matrix = new Matrix4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, -6, 1);
    readonly WebGLUniformLocation Pmatrix, Vmatrix, Mmatrix;
    readonly WebGLBuffer index_buffer;
    double time_old = 0;
    Matrix4 move = Matrix4.Identity, view = Matrix4.Identity;


    IDisposable disposable;

    Cube(HtmlCanvasElement canvas)
    {
        this.canvas = canvas;
        gl = canvas.QueryWebGL();
        gl.NotNull("WebGL ist not supported");

        // Create and store data into vertex buffer
        WebGLBuffer vertex_buffer = gl.CreateBuffer();
        gl.BindBuffer(BufferTarget.ARRAY_BUFFER, vertex_buffer);
        gl.BufferData(BufferTarget.ARRAY_BUFFER, new Float32Array(
            // Bottom face
            -1, -1, -1,
            1, -1, -1,
            1, 1, -1,
            -1, 1, -1,

                -1, -1, 1,
                1, -1, 1,
                1, 1, 1,
                -1, 1, 1,

                -1, -1, -1,
                -1, 1, -1,
                -1, 1, 1,
                -1, -1, 1,

                // Right face
                1, -1, -1,
                1, 1, -1,
                1, 1, 1,
                1, -1, 1,

                -1, -1, -1,
                -1, -1, 1,
                1, -1, 1,
                1, -1, -1,

                -1, 1, -1,
                -1, 1, 1,
                1, 1, 1,
                1, 1, -1), BufferUsage.STATIC_DRAW);

        // Create and store data into color buffer
        WebGLBuffer color_buffer = gl.CreateBuffer();
        gl.BindBuffer(BufferTarget.ARRAY_BUFFER, color_buffer);
        gl.BufferData(BufferTarget.ARRAY_BUFFER,
            new Float32Array(5, 3, 7, 5, 3, 7, 5, 3, 7, 5, 3, 7,
                1, 1, 3, 1, 1, 3, 1, 1, 3, 1, 1, 3,
                0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,
                1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
                1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0,
                0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0),
            BufferUsage.STATIC_DRAW);

        // Create and store data into index buffer
        index_buffer = gl.CreateBuffer();
        gl.BindBuffer(BufferTarget.ELEMENT_ARRAY_BUFFER, index_buffer);
        gl.BufferData(BufferTarget.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), BufferUsage.STATIC_DRAW);
        WebGLShader vertShader = gl.CreateShader(ShaderType.VERTEX_SHADER);
        gl.ShaderSource(vertShader, "attribute vec3 position;" +
            "uniform mat4 Pmatrix;" +
            "uniform mat4 Vmatrix;" +
            "uniform mat4 Mmatrix;" +
            "attribute vec3 color;" +//the color of the point
            "varying vec3 vColor;" +

            "void main(void) { " +//pre-built function
                "gl_Position = Pmatrix*Vmatrix*Mmatrix*vec4(position, 1.);" +
                "vColor = color;" +
            "}");
        gl.CompileShader(vertShader);

        WebGLShader fragShader = gl.CreateShader(ShaderType.FRAGMENT_SHADER);
        gl.ShaderSource(fragShader, "precision mediump float;" +
            "varying vec3 vColor;" +
            "void main(void) {" +
                "gl_FragColor = vec4(vColor, 1.);" +
            "}");
        gl.CompileShader(fragShader);

        WebGLProgram shaderProgram = gl.CreateProgram();
        gl.AttachShader(shaderProgram, vertShader);
        gl.AttachShader(shaderProgram, fragShader);
        gl.LinkProgram(shaderProgram);
        bool linked = gl.IsProgramLinked(shaderProgram);
        /* ====== Associating attributes to vertex shader =====*/
        Pmatrix = gl.GetUniformLocation(shaderProgram, "Pmatrix");
        Vmatrix = gl.GetUniformLocation(shaderProgram, "Vmatrix");
        Mmatrix = gl.GetUniformLocation(shaderProgram, "Mmatrix");

        gl.BindBuffer(BufferTarget.ARRAY_BUFFER, vertex_buffer);
        var position = gl.GetAttributeLocation(shaderProgram, "position");
        gl.VertexAttributePointer(position, 3, NumberType.FLOAT, false, 0, 0);
        gl.EnableVertexAttributeArray(position);
        gl.BindBuffer(BufferTarget.ARRAY_BUFFER, color_buffer);
        var color = gl.GetAttributeLocation(shaderProgram, "color");
        gl.VertexAttributePointer(color, 3, NumberType.FLOAT, false, 0, 0);
        gl.EnableVertexAttributeArray(color);
        gl.UseProgram(shaderProgram);

        gl.Uniform(Pmatrix, Matrix4.CreatePerspective(40.0.ToRadians(), ((double)canvas.Width) / canvas.Height, 1, 100));
        gl.Uniform(Vmatrix, view_matrix);
        Animate(0);
    }

    public void Dispose()
    {
        if (disposable != null) disposable.Dispose();
    }

    void Animate(double time)
    {
        disposable = HtmlWindow.Current.RequestAnimationFrame(Animate);

        double dt = time - time_old;
        move = move.Rotate(dt * 0.005, 0, 0, 1);
        move = move.Rotate(dt * 0.002, 0, 1, 0);
        move = move.Rotate(dt * 0.003, 1, 0, 0);
        time_old = time;
        gl.Enable(Capability.DEPTH_TEST);
        gl.DepthFunc = Depth.LEQUAL;
        gl.ClearColor = new WebGLColor<double>() { Red = 0.5, Green = 0.5, Blue = 0.5, Alpha = 0.9 };
        gl.ClearDepth = 1;
        gl.Viewport = new Rect() { Width = canvas.Width, Height = canvas.Height };
        gl.Clear = ClearMasks.COLOR_BUFFER_BIT | ClearMasks.DEPTH_BUFFER_BIT;
        gl.Uniform(Mmatrix, move);
        gl.BindBuffer(BufferTarget.ELEMENT_ARRAY_BUFFER, index_buffer);
        gl.DrawElements(DrawMode.TRIANGLES, indices.Length, NumberType.UNSIGNED_SHORT, 0);
    }
}

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
  <meta http-equiv="x-ua-compatible" content="IE=11;IE=10;IE=9;IE=8;IE=7" />
  <title>WebGL.html: Html5 WebGL</title>
  <script type="text/javascript" language="javascript" src="js/webgl.js"></script>
</head>
<body onload="StartScript()">
    WebGL.html: check Html5 WebGL features&#160;
  <input type="button" value="&lt;Back" onclick="Back()" />
  <input type="button" value="Next&gt;" onclick="Next()"/>
  <div style="border:solid 1px black;width:500px;height:500px">
    <canvas id="canvas" draggable="false" style="width:500px;height:500px" width="500" height="500">Sorry, your browser does not support canvas element</canvas>
  </div>
  <div id="copyright"></div>
 </body>
</html>
© 2009 JScriptSuite. All rights reserved.