Skip to content

Instantly share code, notes, and snippets.

@BaldarSilveraxe
Created January 29, 2016 21: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 BaldarSilveraxe/ed2228eb8ad237990e20 to your computer and use it in GitHub Desktop.
Save BaldarSilveraxe/ed2228eb8ad237990e20 to your computer and use it in GitHub Desktop.
var pathing_rotation = function(angle, point,width,height) {
var pointX = point[0], pointY = point[1], originX = (width/2), originY = (height/2);
angle = angle * Math.PI / 180.0;
return [
Math.cos(angle) * (pointX-originX) - Math.sin(angle) * (pointY-originY) + originX,
Math.sin(angle) * (pointX-originX) + Math.cos(angle) * (pointY-originY) + originY
];
},
rotated_flipPaths = function(shapePath) {
var i,newPath,angle,xOffset,yOffset,maxX,minX,maxY,minY,spinPath,objectWidth,objectHeight,objectTop,objectLeft;
newPath = [];
for(i = 0; i < shapePath.path.length; i = i + 1) {
newPath.push([
shapePath.fliph ? shapePath.width - shapePath.path[i][0] : shapePath.path[i][0],
shapePath.flipv ? shapePath.height - shapePath.path[i][1] : shapePath.path[i][1]
]);
}
angle = shapePath.rotation || 0;
xOffset = shapePath.left - (shapePath.width / 2) || 0;
yOffset = shapePath.top - (shapePath.height / 2) || 0;
maxX = 0;
minX = false;
maxY = 0;
minY = false;
spinPath = [];
for(i = 0; i < newPath.length; i = i + 1) {
spinPath.push([newPath[i][0], newPath[i][1]]);
spinPath[i] = pathing_rotation(angle, spinPath[i],shapePath.width,shapePath.height);
if(spinPath[i][0] > maxX) {maxX = spinPath[i][0]; }
if(minX === false || Number(spinPath[i][0]) < Number(minX)) {minX = spinPath[i][0]; }
if(spinPath[i][1] > maxY) {maxY = spinPath[i][1]; }
if(minY === false || spinPath[i][1] < minY) {minY = spinPath[i][1]; }
}
objectWidth = maxX - minX;
objectHeight = maxY - minY;
objectTop = minY + (objectHeight/2);
objectLeft = minX + (objectWidth/2);
for(i = 0; i < spinPath.length; i = i + 1) {
spinPath[i][0] = spinPath[i][0] - minX;
spinPath[i][1] = spinPath[i][1] - minY;
}
objectTop = objectTop + yOffset;
objectLeft = objectLeft + xOffset;
return {
left: objectLeft,
top: objectTop,
width: objectWidth,
height: objectHeight,
rotation: angle,
fliph: shapePath.fliph,
flipv: shapePath.flipv,
path: spinPath
};
},
remove_paths = function(area) {
_.each(findObjs({type: 'path', controlledby: area.key }), function(obj) {
obj.remove();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment