@@ -878,7 +878,20 @@ export default class Sigma<
878
878
879
879
this . camera . minRatio = settings . minCameraRatio ;
880
880
this . camera . maxRatio = settings . maxCameraRatio ;
881
+ this . camera . enabledZooming = settings . enableCameraZooming ;
882
+ this . camera . enabledPanning = settings . enableCameraPanning ;
881
883
this . camera . enabledRotation = settings . enableCameraRotation ;
884
+ if ( settings . cameraPanBoundaries ) {
885
+ this . camera . clean = ( state ) =>
886
+ this . cleanCameraState (
887
+ state ,
888
+ settings . cameraPanBoundaries && typeof settings . cameraPanBoundaries === "object"
889
+ ? settings . cameraPanBoundaries
890
+ : { } ,
891
+ ) ;
892
+ } else {
893
+ this . camera . clean = null ;
894
+ }
882
895
this . camera . setState ( this . camera . validateState ( this . camera . getState ( ) ) ) ;
883
896
884
897
if ( oldSettings ) {
@@ -920,6 +933,73 @@ export default class Sigma<
920
933
return this ;
921
934
}
922
935
936
+ private cleanCameraState (
937
+ state : CameraState ,
938
+ { tolerance = 0 , boundaries } : { tolerance ?: number ; boundaries ?: Record < "x" | "y" , [ number , number ] > } = { } ,
939
+ ) : CameraState {
940
+ const newState = { ...state } ;
941
+
942
+ // Extract necessary properties
943
+ const {
944
+ x : [ xMinGraph , xMaxGraph ] ,
945
+ y : [ yMinGraph , yMaxGraph ] ,
946
+ } = boundaries || this . nodeExtent ;
947
+
948
+ // Transform the four corners of the graph rectangle using the provided camera state
949
+ const corners = [
950
+ this . graphToViewport ( { x : xMinGraph , y : yMinGraph } , { cameraState : state } ) ,
951
+ this . graphToViewport ( { x : xMaxGraph , y : yMinGraph } , { cameraState : state } ) ,
952
+ this . graphToViewport ( { x : xMinGraph , y : yMaxGraph } , { cameraState : state } ) ,
953
+ this . graphToViewport ( { x : xMaxGraph , y : yMaxGraph } , { cameraState : state } ) ,
954
+ ] ;
955
+
956
+ // Look for new extents, based on these four corners
957
+ let xMin = Infinity ,
958
+ xMax = - Infinity ,
959
+ yMin = Infinity ,
960
+ yMax = - Infinity ;
961
+ corners . forEach ( ( { x, y } ) => {
962
+ xMin = Math . min ( xMin , x ) ;
963
+ xMax = Math . max ( xMax , x ) ;
964
+ yMin = Math . min ( yMin , y ) ;
965
+ yMax = Math . max ( yMax , y ) ;
966
+ } ) ;
967
+
968
+ // For each dimension, constraint the smaller element (camera or graph) to fit in the larger one:
969
+ const graphWidth = xMax - xMin ;
970
+ const graphHeight = yMax - yMin ;
971
+ const { width, height } = this . getDimensions ( ) ;
972
+ let dx = 0 ;
973
+ let dy = 0 ;
974
+
975
+ if ( graphWidth >= width ) {
976
+ if ( xMax < width - tolerance ) dx = xMax - ( width - tolerance ) ;
977
+ else if ( xMin > tolerance ) dx = xMin - tolerance ;
978
+ } else {
979
+ if ( xMax > width + tolerance ) dx = xMax - ( width + tolerance ) ;
980
+ else if ( xMin < - tolerance ) dx = xMin + tolerance ;
981
+ }
982
+ if ( graphHeight >= height ) {
983
+ if ( yMax < height - tolerance ) dy = yMax - ( height - tolerance ) ;
984
+ else if ( yMin > tolerance ) dy = yMin - tolerance ;
985
+ } else {
986
+ if ( yMax > height + tolerance ) dy = yMax - ( height + tolerance ) ;
987
+ else if ( yMin < - tolerance ) dy = yMin + tolerance ;
988
+ }
989
+
990
+ if ( dx || dy ) {
991
+ // Transform [dx, dy] from viewport to graph (using two different point to transform that vector):
992
+ const origin = this . viewportToFramedGraph ( { x : 0 , y : 0 } , { cameraState : state } ) ;
993
+ const delta = this . viewportToFramedGraph ( { x : dx , y : dy } , { cameraState : state } ) ;
994
+ dx = delta . x - origin . x ;
995
+ dy = delta . y - origin . y ;
996
+ newState . x += dx ;
997
+ newState . y += dy ;
998
+ }
999
+
1000
+ return newState ;
1001
+ }
1002
+
923
1003
/**
924
1004
* Method used to render labels.
925
1005
*
@@ -1575,6 +1655,7 @@ export default class Sigma<
1575
1655
* Function used to create a canvas context and add the relevant DOM elements.
1576
1656
*
1577
1657
* @param {string } id - Context's id.
1658
+ * @param options
1578
1659
* @return {Sigma }
1579
1660
*/
1580
1661
createCanvasContext ( id : string , options : { style ?: Partial < CSSStyleDeclaration > } = { } ) : this {
@@ -1875,6 +1956,21 @@ export default class Sigma<
1875
1956
return this ;
1876
1957
}
1877
1958
1959
+ /**
1960
+ * Method setting multiple settings at once.
1961
+ *
1962
+ * @param {Partial<Settings> } settings - The settings to set.
1963
+ * @return {Sigma }
1964
+ */
1965
+ setSettings ( settings : Partial < Settings < N , E , G > > ) : this {
1966
+ const oldValues = { ...this . settings } ;
1967
+ this . settings = { ...this . settings , ...settings } ;
1968
+ validateSettings ( this . settings ) ;
1969
+ this . handleSettingsUpdate ( oldValues ) ;
1970
+ this . scheduleRefresh ( ) ;
1971
+ return this ;
1972
+ }
1973
+
1878
1974
/**
1879
1975
* Method used to resize the renderer.
1880
1976
*
0 commit comments