Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: reduce number of zoom levels #146

Merged
merged 2 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 42 additions & 10 deletions scripts/components/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ export default class Main extends React.Component {

if (this.state.updateStaticSceneZoom) {
this.setState({
maxZoomedIn: this.state.zoomScale >= 4,
maxZoomedOut: this.state.zoomScale <= 1,
maxZoomedIn: this.state.zoomScale >= Main.MAX_ZOOM,
maxZoomedOut: this.state.zoomScale <= Main.MIN_ZOOM,
updateStaticSceneZoom: false
});
}
Expand Down Expand Up @@ -662,14 +662,24 @@ export default class Main extends React.Component {
});
}

onZoomIn(sceneType) {
if (sceneType === SceneTypes.STATIC_SCENE) {
/**
* Zoom in.
* @param {SceneTypes} sceneType Scene type.
* @param {string} eventType Event type.
*/
onZoomIn(sceneType, eventType) {
if (sceneType == SceneTypes.STATIC_SCENE) {
if (this.state.maxZoomedIn) {
return;
}

let zoomFactor = Main.ZOOM_FACTOR;
if (eventType == 'touch') {
zoomFactor = Main.ZOOM_FACTOR_TOUCH;
}

this.setState({
zoomScale: this.state.zoomScale + 0.1,
zoomScale: Math.min(this.state.zoomScale + zoomFactor, Main.MAX_ZOOM),
updateStaticSceneZoom: true
});
}
Expand All @@ -678,14 +688,24 @@ export default class Main extends React.Component {
}
}

onZoomOut(sceneType) {
if (sceneType === SceneTypes.STATIC_SCENE) {
/**
* Zoom out.
* @param {SceneTypes} sceneType Scene type.
* @param {string} eventType Event type.
*/
onZoomOut(sceneType, eventType) {
if (sceneType == SceneTypes.STATIC_SCENE) {
if (this.state.maxZoomedOut) {
return;
}

let zoomFactor = Main.ZOOM_FACTOR;
if (eventType == 'touch') {
zoomFactor = Main.ZOOM_FACTOR_TOUCH;
}

this.setState({
zoomScale: this.state.zoomScale - 0.1,
zoomScale: Math.max(this.state.zoomScale - zoomFactor, Main.MIN_ZOOM),
updateStaticSceneZoom: true
});
}
Expand Down Expand Up @@ -929,8 +949,8 @@ export default class Main extends React.Component {
} }
getReactRoots={ () => this.reactRoots ?? [null, null] }
zoomScale={ this.state.zoomScale }
zoomIn={ this.onZoomIn.bind(this, scene.sceneType) }
zoomOut={ this.onZoomOut.bind(this, scene.sceneType) }
zoomIn={ this.onZoomIn.bind(this) }
zoomOut={ this.onZoomOut.bind(this) }
maxZoomedIn={ this.state.maxZoomedIn }
maxZoomedOut={ this.state.maxZoomedOut }
/>
Expand Down Expand Up @@ -976,3 +996,15 @@ export default class Main extends React.Component {
}

Main.contextType = H5PContext;

/** @constant {number} ZOOM_FACTOR Zoom factor for static scene */
Main.ZOOM_FACTOR = 0.3;

/** @constant {number} ZOOM_FACTOR_TOUCH Zoom factor for static scene */
Main.ZOOM_FACTOR_TOUCH = 0.15;

/** @constant {number} MAX_ZOOM Maximum zoom level for statcic scene */
Main.MAX_ZOOM = 4;

/** @constant {number} MIN_ZOOM Minimum zoom level for static scene */
Main.MIN_ZOOM = 1;
12 changes: 6 additions & 6 deletions scripts/components/Scene/SceneTypes/StaticScene.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,10 +593,10 @@ export default class StaticScene extends React.Component {
const diff = this.endDistance - this.startDistance;

if (diff > 0) {
this.props.zoomIn();
this.props.zoomIn(this.props.sceneParams.sceneType, 'touch');
}
else if (diff < 0) {
this.props.zoomOut();
this.props.zoomOut(this.props.sceneParams.sceneType, 'touch');
}
}

Expand Down Expand Up @@ -675,10 +675,10 @@ export default class StaticScene extends React.Component {
event.stopPropagation();

if (event.deltaY < 0 && !this.props.maxZoomedIn) {
this.props.zoomIn();
this.props.zoomIn(this.props.sceneParams.sceneType);
}
else if (event.deltaY > 0 && !this.props.maxZoomedOut) {
this.props.zoomOut();
this.props.zoomOut(this.props.sceneParams.sceneType);
}
}

Expand All @@ -696,10 +696,10 @@ export default class StaticScene extends React.Component {
if (this.props.enableZoom) {
switch (event.key) {
case '+':
this.props.zoomIn();
this.props.zoomIn(this.props.sceneParams.sceneType);
break;
case '-':
this.props.zoomOut();
this.props.zoomOut(this.props.sceneParams.sceneType);
break;
}
}
Expand Down
Loading