0

I am planning to write an ASP.NET/C# based browser game which runs on an ASP.NET server, and uses HTML 5 Canvas for graphics (2D bitmaps, no 3D graphics).

Is it possible to send Bitmaps (png/gif) and other data (a dictionary of and 2D Array[int, string]) to the HTML 5 Canvas, and send MouseClick Coordinates back to the ASP.NET server?

1
  • 2
    The question is too general. A short answer would be "yes, it is possible" but you should ask about something specific. Commented Dec 30, 2012 at 21:08

1 Answer 1

2

Load a single image to canvas

You have this canvas

<canvas id="myCanvas"></canvas>

Load an image by creating a new Image object and adding the image onto the canvas once the image has loaded. The drawImage() method renders the image object onto the canvas.

<script type="text/javascript">
    var context = $("#myCanvas")[0].getContext("2d");
    var img = new Image();
    img.src = "some-image.jpg";
    img.onload = function() {
        context.drawImage(img, 100, 100);
    }
</script>

Get the position of the mouse in the window and send the coordinates to your processing page via AJAX:

<script type="text/javascript">
    $(document).bind("click", function () {
        // Add a click-handler to the image.
        $("#myCanvas").bind("click", function (e) {
            var $canvas = $(e.target);
            var offset = $canvas.offset();
            var xpos = e.clientX - offset.left;
            var ypos = e.clientY - offset.top;

            $.post("Process.aspx", { x: xpos, y: ypos } );
        });
    });
</script>

Load an array of images to canvas

Load an array of images and then call a function on each image when it's loaded.

The images will appear in a grid.

// array of images
var imgArray = [
    "image01.jpg", "image02.jpg", "image03.jpg",
    "image04.jpg", "image05.jpg", "image06.jpg",
    "image07.jpg", "image08.jpg", "image09.jpg"
];

var preArray = [];    // array of preload images
var count = 0;    // count of loaded images

for (var i in imgArray) {
    preArray[i] = new Image();
    preArray[i].src = imgArray[i];
    preArray[i].onload = function() {
        count++;
        // when the last image was loaded
        if (count == imgArray.length) {
            // draw preload array to the grid
            imageLoaded(preArray);
        }
    }
}

function imageLoaded(img) {
    // grid for canvas
    var xpos = 100;    // margin-left
    var ypos = 100;    // margin-top
    var offset = 70;    // offset for images
    var cols = 3;    // number of columns in a grid

    //creates a grid
    for(var i in img) {
        var xoffset = xpos +  offset * (i % cols);
        var yoffset = ypos +  offset * Math.floor(i / cols);
        context.drawImage(img[i], xoffset, yoffset);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. Also how could I send an above mentioned [int, string] array to the Javascript?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.