由這是怎麼回事.....
math/algorithm Fit image to screen retain aspect ratio
我得到這個.....
function update(group, activeAnchor) {
var topLeft = group.get(".topLeft")[0];
var topRight = group.get(".topRight")[0];
var bottomRight = group.get(".bottomRight")[0];
var bottomLeft = group.get(".bottomLeft")[0];
var image = group.get(".image")[0];
// update anchor positions
switch(activeAnchor.getName()) {
case "topLeft":
topRight.attrs.y = activeAnchor.attrs.y;
//topRight.attrs.x = activeAnchor.attrs.x + image.getWidth();
bottomLeft.attrs.x = activeAnchor.attrs.x;
// bottomRight.attrs.x = activeAnchor.attrs.x + image.getWidth();
break;
case "topRight":
topLeft.attrs.y = activeAnchor.attrs.y;
bottomRight.attrs.x = activeAnchor.attrs.x;
break;
case "bottomRight":
bottomLeft.attrs.y = activeAnchor.attrs.y;
topRight.attrs.x = activeAnchor.attrs.x;
break;
case "bottomLeft":
bottomRight.attrs.y = activeAnchor.attrs.y;
topLeft.attrs.x = activeAnchor.attrs.x;
break;
}
//https://stackoverflow.com/questions/6565703/math-algorithm-fit-image-to-screen-retain-aspect-ratio
var ws= topRight.attrs.x - topLeft.attrs.x;
var hs = bottomLeft.attrs.y - topLeft.attrs.y;
var wi = image.getWidth();
var hi = image.getHeight();
var rs = ws/hs;
var ri = wi/hi;
if (rs>ri){
var width =wi * hs/hi;
image.setSize(width, hs);
image.setPosition(topLeft.attrs.x+((ws-width)/2), bottomLeft.attrs.y-((hi)));
} else {
var height=hi * ws/wi;
image.setSize(ws, height);
image.setPosition(topRight.attrs.x-wi, topLeft.attrs.y+((hs-height)/2));
}
}
http://jsfiddle.net/zb3Km/2/
編輯:
http://jsfiddle.net/zb3Km/3/
在完成調整大小後使錨點回彈
編輯2: 現在將調整大小定位到對角。
http://jsfiddle.net/jdA3y/
更好的是,如果圖像呆中心....我_might_看,明天爲你做... _might_;) – PAEz
真的好基礎,再工作過...感謝派斯! – Nyxynyx
任何時候,高興地幫助....我prolly會考慮明天保持它的中心,真的應該得到它的權利;) – PAEz