<canvas id="canvas" width="30" height="30"></canvas>
<script>
var img = new Image();
// This is a 11x11 black and white grid.
img.src = 'resources/grid-small.png';
function drawSubRectScaled(ctx, img, x, y, w, h, scale)
{
ctx.drawImage(img, x, y, w, h, x * scale, y * scale, w * scale, h * scale);
}
img.onload = function()
{
// Draw the image into canvas with 8 fragments like shown:
//
// | 3.66 x 3.66 | | 3.66 x 3.66 | | 3.66 x 3.66 |
//
// | 5.5 x 3.66 | | 5.5 x 3.66 |
//
// | 3.66 x 3.66 | | 3.66 x 3.66 | | 3.66 x 3.66 |
var ctx = document.getElementById("canvas").getContext("2d");
var scale = 30.0 / 11.0;
// Draw the top row.
drawSubRectScaled(ctx, img, 0, 0, 3.66, 3.66, scale);
drawSubRectScaled(ctx, img, 3.66, 0, 3.66, 3.66, scale);
drawSubRectScaled(ctx, img, 7.33, 0, 3.66, 3.66, scale);
// Draw the middle row.
drawSubRectScaled(ctx, img, 0, 3.66, 5.5, 3.66, scale);
drawSubRectScaled(ctx, img, 5.5, 3.66, 5.5, 3.66, scale);
// Draw the bottom row.
drawSubRectScaled(ctx, img, 0, 7.33, 3.66, 3.66, scale);
drawSubRectScaled(ctx, img, 3.66, 7.33, 3.66, 3.66, scale);
drawSubRectScaled(ctx, img, 7.33, 7.33, 3.66, 3.66, scale);
}
</script>