gpt4 book ai didi

javascript - 如何调整网格中 "full bleed"的图像大小?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:21:41 25 4
gpt4 key购买 nike

以google images、flickr photostream等为例。每张图片都有不同的尺寸和 radio ,但它们都在左侧和右侧排列。

我最初的逻辑是在 javascript 中循环遍历它们,弄清楚我何时处于下一张图片太宽的位置。然后我看到我有多少“空间”并全面增加宽度 - 但我最终制作的薄图像太宽了。

这就是我正在做的事情:

maxwidth = 100;
width = 0;
imgs = [];
foreach image {
if (width + image.width > maxwidth) {
space = maxwidth - width / imgs.length;
foreach imgs {
img.width = img.width + space;
}

imgs = [];
width = 0;
}

imgs.push(image);
width += image.width;
}

最佳答案

这是 partition problem ;在 this blog post 中讨论了它在照片画廊中的应用。约翰内斯·特里茨 (Johannes Treitz) 着。建议的解决方案是:

  • 要找到所需的行数 k,请将照片缩放到窗口高度的一半,将它们的宽度相加,除以窗口宽度,然后取整。
  • 然后,照片的宽高比用作一组 S 权重。使用现有的线性分区算法找到 Sk 上的最优分布。

博文包含以下用于构建图库的 CoffeeScript:

viewport_width = $(window).width()
ideal_height = parseInt($(window).height() / 2)
summed_width = photos.reduce ((sum, p) -> sum += p.get('aspect_ratio') * ideal_height), 0
rows = Math.round(summed_width / viewport_width)

if rows < 1
# (2a) Fallback to just standard size
photos.each (photo) -> photo.view.resize parseInt(ideal_height * photo.get('aspect_ratio')), ideal_height
else
# (2b) Distribute photos over rows using the aspect ratio as weight
weights = photos.map (p) -> parseInt(p.get('aspect_ratio') * 100)
partition = linear_partition(weights, rows)

# (3) Iterate through partition
index = 0
row_buffer = new Backbone.Collection
_.each partition, (row) ->
row_buffer.reset()
_.each row, -> row_buffer.add(photos.at(index++))
summed_ratios = row_buffer.reduce ((sum, p) -> sum += p.get('aspect_ratio')), 0
row_buffer.each (photo) -> photo.view.resize parseInt(viewport_width / summed_ratios * photo.get('aspect_ratio')), parseInt(viewport_width / summed_ratios)

linear_partition 函数实现如下 ( see github ):

# Linear partition
# Partitions a sequence of non-negative integers into k ranges
# Based on Óscar López implementation in Python (http://stackoverflow.com/a/7942946)
# Also see http://www8.cs.umu.se/kurser/TDBAfl/VT06/algorithms/BOOK/BOOK2/NODE45.HTM
# Dependencies: UnderscoreJS (http://www.underscorejs.org)
# Example: linear_partition([9,2,6,3,8,5,8,1,7,3,4], 3) => [[9,2,6,3],[8,5,8],[1,7,3,4]]

linear_partition = (seq, k) =>
n = seq.length

return [] if k <= 0
return seq.map((x) -> [x]) if k > n

table = (0 for x in [0...k] for y in [0...n])
solution = (0 for x in [0...k-1] for y in [0...n-1])
table[i][0] = seq[i] + (if i then table[i-1][0] else 0) for i in [0...n]
table[0][j] = seq[0] for j in [0...k]
for i in [1...n]
for j in [1...k]
m = _.min(([_.max([table[x][j-1], table[i][0]-table[x][0]]), x] for x in [0...i]), (o) -> o[0])
table[i][j] = m[0]
solution[i-1][j-1] = m[1]

n = n-1
k = k-2
ans = []
while k >= 0
ans = [seq[i] for i in [(solution[n-1][k]+1)...n+1]].concat ans
n = solution[n-1][k]
k = k-1

[seq[i] for i in [0...n+1]].concat ans

关于javascript - 如何调整网格中 "full bleed"的图像大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19695950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com