Search code examples
javascriptcanvascell

Adding images by combining cells with Javascript canvas


First of all, I have difficulty in explaining because my English is not very good. But I will try to explain as best I can.

I add random photos to the canvasta cells fields with Javascript.

Each plot is equivalent to 20pixels. What I want to do is: if i and y in the map data are equal to 4; I want to add the photo I want to a 4x4 area.

In other words, while adding a photo to 20 pixels; I want to add a photo to a 320pixel area when it is 4x4.

As in the sample photo. Check Photo

enter image description here

var map = [
            [1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1],
            [1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0],
            [1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0],
            [1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0],
            [1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0],
            [1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1],
            [1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1],
            [1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1],
            [1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1],
            [1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1],
            [4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        ];
        
        window.onload = function() {
            const canvas = document.getElementById("main");
            const ctx = canvas.getContext("2d");
            ctx.strokeStyle = "transparent";
            ctx.lineWidth = 0;

            
            //draw grid
            for (let i = 0; i <= 300; i++) {
                const x = i*20;
                ctx.moveTo(x, 0);
                ctx.lineTo(x, canvas.height);
                ctx.stroke();
                
                const y = i*20;
                ctx.moveTo(0, y);
                ctx.lineTo(canvas.width, y);
                ctx.stroke();
            }
            
            
            //draw images
            const p = ctx.lineWidth / 1; //padding
            for (let xCell = 0; xCell < map.length; xCell++) {
                for (let yCell = 0; yCell < map[xCell].length; yCell++) {
                    const x = xCell * 20;
                    const y = yCell * 20;
                    const img = new Image();
                    if (map[xCell][yCell] === 1) {
                        img.onload = function() {
                            ctx.drawImage(img, y+p, x+p, 20-p*2, 20-p*2);
                        };
                        
                        //TODO: set img.src to your api url instead of the dummyimage url.
                        img.src = `https://picsum.photos/id/${Math.floor(Math.random() * 10)}${Math.floor(Math.random() * 10)}/200/300`;
                    }else if(map[xCell][yCell] == 4){
                        img.onload = function() {
                            ctx.drawImage(img, y+p, x+p, 20-p*2, 20-p*2);
                        };
                        
                        //TODO: set img.src to your api url instead of the dummyimage url.
                        img.src = `https://picsum.photos/id/${Math.floor(Math.random() * 12)}${Math.floor(Math.random() * 12)}/200/300`;
                    }
                
                }
            }
        };
<canvas id="main" width="600" height="630"></canvas>
        
    </canvas>


Solution

  • I solved the problem as below. There are positional errors but I will completely fix them.

    Thanks to Lajos Arpad for trying to help.

    var defaultMap = [
                    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1],
                    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1],
                    [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1],
                    [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
                    [0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
                    [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
                    [0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
                    [0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1],
                    [0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1],
                    [1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1],
                    [1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1],
                    [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1],
                    [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1],
                    [1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1],
                ];
                var images = [
                    {
                        name: "player 1",
                        position: [
                            {
                                x: [0,4],
                                y: [4,4]
                            }
                        ]
                    },
                    {
                        name: "player 2",
                        position: [
                            {
                                x: [5,7],
                                y: [5,2]
                            }
                        ]
                    }
                ];
                
                window.onload = function() {
                    const canvas = document.getElementById("main");
                    const ctx = canvas.getContext("2d");
                    ctx.strokeStyle = "transparent";
                    ctx.lineWidth = 0;
    
                    
                    //draw grid
                    for (let i = 0; i <= 300; i++) {
                        const x = i*40;
                        ctx.moveTo(x, 0);
                        ctx.lineTo(x, canvas.height);
                        ctx.stroke();
                        
                        const y = i*40;
                        ctx.moveTo(0, y);
                        ctx.lineTo(canvas.width, y);
                        ctx.stroke();
                    }
                    
                    const p = ctx.lineWidth / 1;
                    for (let xCell = 0; xCell < defaultMap.length; xCell++) {
                        for (let yCell = 0; yCell < defaultMap[xCell].length; yCell++) {
                            const x = xCell * 20;
                            const y = yCell * 20;
                            const img = new Image();
                            if (defaultMap[xCell][yCell] === 1) {
                                img.onload = function() {
                                    ctx.drawImage(img, y+p, x+p, 20-p*2, 20-p*2);
                                };
                                
                                img.src = `https://besthqwallpapers.com/Uploads/5-6-2020/134999/thumb-black-ground-texture-macro-grunge-textures-black-backgrounds-ground-textures.jpg`;
                            }
                        
                        }
                    }
    
                    for (let i = 0; i < images.length; i++) {
                        var width = 1, height = 1, x = images[i].position[0].x[0], y = images[i].position[0].y[0];
                        console.log(images[i].name + " x => " + images[i].position[0].x[0])
                        for(let w = images[i].position[0].x[0]; w < images[i].position[0].x[1]; w++){
                            width++
                        }
                        
                        for(let h = 1; h < images[i].position[0].y[1]; h++){
                            height++
                        }
                        const img = new Image();
                        img.src = `https://picsum.photos/id/${Math.floor(Math.random() * 12)}${Math.floor(Math.random() * 12)}/200/300`;
                        img.onload = function() {
                            console.log(20*images[i].position[0].x[0]+p)
                            /* ctx.rect(20*i, 20*i, 20*width-p*2, 20*height-p*2)
                            ctx.fillStyle = "blue";
                            ctx.fill(); */
                            ctx.drawImage(img, 20*images[i].position[0].y[0]+p, 20*images[i].position[0].x[0]+p, 20*width-p*2, 20*height-p*2);
                        };
                        
                    }
                };
    <canvas id="main" width="600" height="630"></canvas>
            
    </canvas>