Skip to content

Instantly share code, notes, and snippets.

@peteroupc
Created November 15, 2012 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peteroupc/4075955 to your computer and use it in GitHub Desktop.
Save peteroupc/4075955 to your computer and use it in GitHub Desktop.
Calculates the bounding box of a KineticJS path
/* This file is in the public domain. Peter O., 2012. http://upokecenter.dreamhosters.com
Public domain dedication: http://creativecommons.org/publicdomain/zero/1.0/legalcode */
/* NOTE: Currently supports lines only */
function RectAccum(){
this.left=0;
this.top=0;
this.right=0;
this.bottom=0;
this.startX=true;
this.startY=true;
}
RectAccum.prototype={
width:function(){
return this.right-this.left
},
height:function(){
return this.bottom-this.top
},
addX:function(x){
this.left=(this.startX) ? x : Math.min(x,this.left);
this.right=(this.startX) ? x : Math.max(x,this.right);
this.startX=false;
},
addY:function(y){
this.top=(this.startY) ? y : Math.min(y,this.top);
this.bottom=(this.startY) ? y : Math.max(y,this.bottom);
this.startY=false;
},
addXY:function(x,y){
this.addX(x); this.addY(y);
}
}
function getPathDimensions(path){
var data=path.dataArray;
var ra=new RectAccum();
for(var i=0;i<data.length;i++){
switch(data[i].command){
case "L":
ra.addXY(data[i].start.x,data[i].start.y);
ra.addXY(data[i].points[0],data[i].points[1]);
break;
}
}
return {left:ra.left,top:ra.top,width:ra.width(),height:ra.height()}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment