如果你設置了一個數據結構,如:
var a = { 'scales' : [50, 100, 200, 400, 1000],
'cachedRects': [{'location': 'rect-large-1234-5678.png', x: 1234, y: 5678, scale: 3}
{'location': 'rect-small-1240-5685.png', x: 1240, y: 5685, scale: 1} ]
}
可以使用模函數來做到這一點:
var currentx = GetCurrentX();
var currenty = GetCurrentY();
var currentScale = GetCurrentScale();
var rectFound = false;
foreach(rect in a.cachedRects) {
if (rect.scale === currentScale
&& currentx % a.scales[currentScale] === rect.x
&& currenty % a.scales[currentScale] === rect.y) {
rectFound = true;
useOverlay(rect);
break;
}
}
if(!rectFound) {
//could loop again for a larger rectangle of a lower scale.
}
上面可能會或可能不會變成是有效的JS - 我沒有試圖運行它。無論如何,我希望你能得到主意。