Repository navigation
Expand file tree
/
Copy pathlayout.js
More file actions
91 lines (91 loc) · 5.58 KB
/
Copy pathlayout.js
File metadata and controls
91 lines (91 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
(function(root){
function arrange(count, cfg){
const {width,height,margin,gap,min}=cfg;
if (![width,height,margin,gap,min].every(Number.isFinite) || width<=0 || height<=0 || min<=0 || margin<0 || gap<0) return {error:'أدخل مقاسات صحيحة أكبر من صفر، وهوامش وفراغات غير سالبة.'};
const w=width-2*margin,h=height-2*margin;
if(w<=0||h<=0) return {error:'الهوامش أكبر من مساحة اللوحة.'};
const capacity=Math.max(0,Math.floor((w+gap+1e-8)/(min+gap)))*Math.max(0,Math.floor((h+gap+1e-8)/(min+gap)));
if(!count) return {size:0,positions:[],capacity,cols:0,rows:0};
let best=null;
for(let cols=1;cols<=count;cols++){
const rows=Math.ceil(count/cols);
const size=Math.min((w-gap*(cols-1))/cols,(h-gap*(rows-1))/rows);
const unused=cols*rows-count;
if(size>0 && (!best || size>best.size+1e-8 || (Math.abs(size-best.size)<1e-8 && unused<best.unused))) best={size,cols,rows,unused};
}
if(!best) return {error:'لا توجد مساحة كافية. قلّل الفراغات أو كبّر اللوحة.',capacity};
const positions=[];
const y0=(height-(best.rows*best.size+(best.rows-1)*gap))/2;
for(let r=0,index=0;r<best.rows;r++){
const n=Math.min(best.cols,count-index),x0=(width-(n*best.size+(n-1)*gap))/2;
for(let c=0;c<n;c++,index++)positions.push({x:x0+c*(best.size+gap),y:y0+r*(best.size+gap)});
}
return {...best,positions,capacity,tooSmall:best.size+1e-8<min};
}
function cropRect(width,height,crop,aspect=1){
const w=Math.min(width,height*aspect)/crop.zoom,h=w/aspect;
return {x:(width-w)*crop.x,y:(height-h)*crop.y,width:w,height:h,side:w};
}
function preview(count,cfg){
const fit=arrange(count,cfg);
if(fit.error||!count||(!fit.tooSmall&&cfg.sizeMode!=='exact'))return fit;
const cols=Math.max(0,Math.floor((cfg.width-2*cfg.margin+cfg.gap+1e-8)/(cfg.min+cfg.gap)));
const maxRows=Math.max(0,Math.floor((cfg.height-2*cfg.margin+cfg.gap+1e-8)/(cfg.min+cfg.gap)));
const visible=Math.min(count,cols*maxRows),rows=cols?Math.ceil(visible/cols):0,positions=[];
const y0=(cfg.height-(rows*cfg.min+Math.max(0,rows-1)*cfg.gap))/2;
for(let r=0,index=0;r<rows;r++){
const n=Math.min(cols,visible-index),x0=(cfg.width-(n*cfg.min+(n-1)*cfg.gap))/2;
for(let c=0;c<n;c++,index++)positions.push({x:x0+c*(cfg.min+cfg.gap),y:y0+r*(cfg.min+cfg.gap)});
}
return {...fit,size:cfg.min,cols:Math.min(cols,visible),rows,positions,overflow:count-visible};
}
function movePhoto(items,from,to){
const result=items.slice();
if(!Number.isInteger(from)||!Number.isInteger(to)||from<0||to<0||from>=items.length||to>=items.length)return result;
const [photo]=result.splice(from,1);result.splice(to,0,photo);return result;
}
function layoutFor(photos,cfg){
const base=preview(photos.length,cfg);
const mode=cfg.layoutMode||'grid';
if(base.error||!photos.length||mode==='grid')return {...base,positions:base.positions?.map(p=>({...p,width:base.size,height:base.size}))};
const w=cfg.width-2*cfg.margin,h=cfg.height-2*cfg.margin,g=cfg.gap,n=photos.length;
let positions=[],cols=0;
if(mode==='masonry'){
// Clamp extreme panoramas so every cell remains practical for cropping.
const ratios=photos.map(p=>Math.max(.5,Math.min(2,p.image.width/p.image.height)));
let best=null;
const maxColumns=Math.min(n,Math.ceil(Math.sqrt(2*n*w/h))+4);
for(let c=1;c<=maxColumns;c++){
const maxWidth=(w-(c-1)*g)/c;if(maxWidth<=0)break;
function pack(width){
const bottoms=Array(c).fill(0),cells=[];
ratios.forEach(r=>{let col=0;for(let j=1;j<c;j++)if(bottoms[j]<bottoms[col])col=j;
const height=width/r;cells.push({x:col*(width+g),y:bottoms[col],width,height});bottoms[col]+=height+g;});
return {cells,height:Math.max(...bottoms)-g};
}
let lo=0,hi=maxWidth;
for(let j=0;j<35;j++){const mid=(lo+hi)/2;if(pack(mid).height<=h)lo=mid;else hi=mid;}
if(lo<=1e-8)continue;
const packed=pack(lo),smallest=Math.min(...packed.cells.map(p=>Math.min(p.width,p.height)));
if(!best||smallest>best.smallest){best={...packed,smallest,cols:c,width:c*lo+(c-1)*g};}
}
if(!best)return {error:'الفراغات أكبر من مساحة اللوحة. قلّل الفراغات أو كبّر اللوحة.',positions:[]};
cols=best.cols;positions=best.cells.map(p=>({...p,x:p.x+(cfg.width-best.width)/2,y:p.y+(cfg.height-best.height)/2}));
}else{
function split(x,y,width,height,count,depth=0){
if(count===1){positions.push({x,y,width,height});return;}
const first=Math.max(1,Math.min(count-1,Math.round(count*(depth%2?.5:.4))));
const horizontal=width>=height;
const length=((horizontal?width:height)-g)*first/count;
if(horizontal){split(x,y,length,height,first,depth+1);split(x+length+g,y,width-length-g,height,count-first,depth+1);}
else{split(x,y,width,length,first,depth+1);split(x,y+length+g,width,height-length-g,count-first,depth+1);}
}
split(cfg.margin,cfg.margin,w,h,n);
if(positions.some(p=>p.width<=0||p.height<=0))return {error:'الفراغات كبيرة لهذا العدد من الصور. قلّلها أو كبّر اللوحة.',positions:[]};
}
const size=Math.min(...positions.map(p=>Math.min(p.width,p.height)));
return {positions,size,cols,rows:0,tooSmall:size+1e-8<cfg.min,variable:true};
}
const api={arrange,cropRect,preview,movePhoto,layoutFor};
if(typeof module!=='undefined')module.exports=api;else root.CollageLayout=api;
})(globalThis);