2016-06-07 93 views
0

假設我有一個寬度和高度各爲400的畫布對象,並且在它的2D上下文中繪製畫布對象的邊界之外。有沒有辦法計算繪製在畫布邊界之外的任何東西的大小?計算在畫布邊界之外繪製的區域

實施例說明:

enter image description here

回答

0

所有包圍盒的rects面積

計算所有矩形的極值:

var currentLeftmostX=10000000; 
var currentTopmostY=10000000; 
var currentRightmostX=-10000000; 
var currentBottommostY=-10000000; 

// do this for each rect 
if(rect.x < currentLeftmostX) {currentLeftmostX=rect.x;} 
if(rect.y < currentTopmostY) {currentTopmostY=rect.y;} 
if(rect.x+rect.width > currentRightmostX) {currentRightmostX=rect.x;} 
if(rect.y+rect.height > currentBottommostY){currentBottommostY=rect.y;} 

var boundingArea= 
    (currentRightmostX-currentLeftmostX)*(currentBottommostY - currentBottommostY);