Investigate how to manipulate the cards into a 3x3 grid programmatically #110

Closed
opened 2023-12-10 15:25:30 +00:00 by Vylpes · 1 comment
Owner
No description provided.
Vylpes added the
type
spike
label 2024-05-20 09:52:03 +01:00
Vylpes added this to the 0.7.0 milestone 2024-06-02 16:07:54 +01:00
Vylpes added this to the 0.7 Sprint 2 project 2024-06-16 15:43:27 +01:00
Vylpes self-assigned this 2024-06-18 18:46:47 +01:00
Vylpes started working 2024-06-21 17:15:08 +01:00
Vylpes stopped working 2024-06-21 17:45:31 +01:00
30 minutes 23 seconds
Author
Owner
const { createCanvas, loadImage } = require('canvas');
const { writeFileSync } = require('fs');

async function test(gridSize) {
    const totalImages = gridSize ** 2;

    const imageWidth = 526;
    const imageHeight = 712;

    const canvasWidth = imageWidth * gridSize;
    const canvasHeight = imageHeight * Math.floor(totalImages / gridSize);

    const canvas = createCanvas(canvasWidth, canvasHeight);
    const ctx = canvas.getContext('2d');

    const images = [];

    for (let i = 0; i < totalImages; i++) {
        images.push(await loadImage(`cards/${i}.png`));
    }

    for (let i = 0; i < images.length; i++) {
        const image = images[i];

        const x = i % gridSize;
        const y = Math.floor(i / gridSize);

        const imageX = imageWidth * x;
        const imageY = imageHeight * y;

        ctx.drawImage(image, imageX, imageY);

        writeFileSync("out.png", canvas.toBuffer());
    }
}

test(3);
```js const { createCanvas, loadImage } = require('canvas'); const { writeFileSync } = require('fs'); async function test(gridSize) { const totalImages = gridSize ** 2; const imageWidth = 526; const imageHeight = 712; const canvasWidth = imageWidth * gridSize; const canvasHeight = imageHeight * Math.floor(totalImages / gridSize); const canvas = createCanvas(canvasWidth, canvasHeight); const ctx = canvas.getContext('2d'); const images = []; for (let i = 0; i < totalImages; i++) { images.push(await loadImage(`cards/${i}.png`)); } for (let i = 0; i < images.length; i++) { const image = images[i]; const x = i % gridSize; const y = Math.floor(i / gridSize); const imageX = imageWidth * x; const imageY = imageHeight * y; ctx.drawImage(image, imageX, imageY); writeFileSync("out.png", canvas.toBuffer()); } } test(3); ```
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Total time spent: 30 minutes 23 seconds
Vylpes
30 minutes 23 seconds
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: External/card-drop#110
No description provided.