简体   繁体   English

网格化图像,如画布上的 Pinterest 布局

[英]Gridify Images like Pinterest Layout on Canvas

I am using pixi.js to create an interactive app.我正在使用 pixi.js 创建一个交互式应用程序。 I need to place images in different containers on the screen in the form of pinterest like layout or gridify them so that they look organize and nice.我需要将图像以类似布局的 pinterest 形式放置在屏幕上的不同容器中,或者将它们网格化,以使它们看起来井井有条。 As Im working on canvas so plugins like gridify.js do not suport canvas and Im unable to find any plugin or code to get this working on canvas.因为我在画布上工作,所以像 gridify.js 这样的插件不支持画布,我无法找到任何插件或代码来让它在画布上工作。 Also, the images need to be clickable so I cannot use plugins like html to canvas conversion as they only generate the image.此外,图像需要可点击,所以我不能使用像 html 到画布转换这样的插件,因为它们只生成图像。

I have found a solution for this by examining the gridify.js logic.我通过检查 gridify.js 逻辑找到了解决方案。 Here is my code:这是我的代码:

    var items = options.items;

    var width = options.containerWidth,
    item_margin = parseInt(options.margin || 0),
    item_width = parseInt(options.max_width || options.width || 220),
    column_count = Math.max(Math.floor(width / (item_width + item_margin)), 1),
    left = column_count == 1 ? item_margin / 2 : (width % (item_width + item_margin)) / 2,
    lastHeight = 0;
    if (options.max_width) {
        column_count = Math.ceil(width / (item_width + item_margin));
        item_width = (width - column_count * item_margin - item_margin) / column_count;
        left = item_margin / 2;
    }

    for (var i = 0, length = items.length; i < length; i++) {

       var ratio = item_width / items[i].width;
        items[i].width = item_width;
        items[i].height = items[i].height * ratio;
        items[i].position.y = lastHeight + item_margin / 2;
        items[i].position.x = 0;


        lastHeight += items[i].height + (item_margin * 2);
    }

Iterate all the containers and pass the parameters to above function.迭代所有容器并将参数传递给上述函数。 It will gridify the images/contents in a container.它将网格化容器中的图像/内容。 I have used single column per container but you can use multiple columns by using the original code from the gridify.js我为每个容器使用了单列,但您可以使用 gridify.js 中的原始代码使用多列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM