SoFunction
Updated on 2025-03-01

Javascript+canvas to create nine-grid applet


/*
*canvasid:html canvas tag id
*imageid:html img tag id
*gridcountX: x-axis picture segmentation number
*gridcountY: the number of y-axis pictures divided
*gridspace: Grid space
*offsetX: x*y grid relative to canvas(0, 0) X coordinate offset
**offsetX: x*y grid relative to canvas(0, 0) Y coordinate offset
*isanimat: Whether to enable animation display
 */
function ImageGrid(canvasid, imageid, gridcountX, gridcountY, gridspace, offsetX, offsetY, isanimat) {
    var image = new Image();
    var g = (canvasid).getContext("2d");
    var img=(imageid);
    =("src");
    (image, 0, 0);
    var imagedata = (0, 0, , );
    var grid_width = / gridcountX;
    var grid_height = / gridcountY;
//Animation
    if (isanimat) {
        var x = 0,
            y = 0;
        var inter = setInterval(function() {
            (imagedata, gridspace * x + offsetX, gridspace * y + offsetY, grid_width * x, grid_height * y, grid_width, grid_height);
            x < gridcountX ? x++ : x = 0;
            if (x == 0) {
                y < gridcountY ? y++ : y = 0;
            }
        }, 200);
        y == gridcountY ? clearInterval(inter) : null;
} else { //Non-animation
        for (var y = 0; y < gridcountY; y++) {
            for (var x = 0; x < gridcountX; x++) {
                (imagedata, gridspace * x + offsetX, gridspace * y + offsetY, grid_width * x, grid_height * y, grid_width, grid_height);
            }
        }
    }
}