Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Unable to set Zoom While on Diagram rendered on this.diagramView.zoomToFit(); (Read 62 times)
Avinash
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 22
Joined: Nov 12th, 2024
Unable to set Zoom While on Diagram rendered on this.diagramView.zoomToFit();
Jan 24th, 2025 at 5:57am
Print Post  
I have set Zoomin and zoomout functionality which is working perfectly individually, but When i have rendered diagram and set it to zoomToFit(); and it is fitting to screen which is perfect, but my zoom buttons are not responding accordingly, how to set my zoom levels as per zoomToFit(); is rendered?
Here is my code:
HTML:
<canvas id="diagram" width="2100" height="2100" style="border: 1px solid black;">
          This page requires a browser that supports HTML 5 Canvas element.
        </canvas>

JS:
initializeZoomControls() {
    const zoomOutButton = document.getElementById('zoomOutButton') as HTMLButtonElement;
    const zoomInButton = document.getElementById('zoomInButton') as HTMLButtonElement;
    const zoomLabel = document.getElementById('zoomLabel') as HTMLLabelElement;

    if (!zoomOutButton || !zoomInButton || !zoomLabel) {
      console.error("Zoom controls not found.");
      return;
    }

    // Set initial zoom level
    let currentZoom = this.diagramView.zoomFactor * 100; // Convert to percentage

    // Define zoom change step
    const zoomStep = 500; // 5% zoom increments
    const minZoom = 1000; // Minimum zoom percentage
    const maxZoom = 10000; // Maximum zoom percentage

    // Zoom out functionality
    zoomOutButton.addEventListener('click', () => {
      if (currentZoom > minZoom) {
        currentZoom -= zoomStep;
        this.diagramView.zoomFactor = currentZoom / 100;
      }
    });

    // Zoom in functionality
    zoomInButton.addEventListener('click', () => {
      if (currentZoom < maxZoom) {
        currentZoom += zoomStep;
        this.diagramView.zoomFactor = currentZoom / 100;
      }
    });
  }

Help me to set zoom levels according to zoomToFit() diagram is rendered.

  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Online


tech.support

Posts: 3343
Joined: Oct 19th, 2005
Re: Unable to set Zoom While on Diagram rendered on this.diagramView.zoomToFit();
Reply #1 - Jan 24th, 2025 at 9:12am
Print Post  
DiagramView.zoomFactor is already specifying percentage, so your additional divisions and multiplications by 100 are kind of confusing. I think your max value specifies 100%? so if zoomToFit() zooms more than that, it would disable zooming in. Also make sure you synchronize the currentZoom variable value with view's zoomFactor after calling zoomToFit(). This seems to work well enough for me in attached project:

Code
Select All
var currentZoom = 100;
const zoomStep = 5; // 5% zoom increments
const minZoom = 10; // Minimum zoom percentage
const maxZoom = 500; // Maximum zoom percentage

function onZoomOut()
{
    if (currentZoom > minZoom)
    {
        currentZoom -= zoomStep;
        diagramView.zoomFactor = currentZoom;
    }
}

function onZoomIn()
{
    if (currentZoom < maxZoom)
    {
        currentZoom += zoomStep;
        diagramView.zoomFactor = currentZoom;
    }
}

function onZoomFit()
{
    diagramView.zoomToFit();
    currentZoom = diagramView.zoomFactor;
} 



Regards,
Slavcho
Mindfusion
  

ZoomTest.zip ( 971 KB | 15 Downloads )
Back to top
 
IP Logged
 
Avinash
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 22
Joined: Nov 12th, 2024
Re: Unable to set Zoom While on Diagram rendered on this.diagramView.zoomToFit();
Reply #2 - yesterday at 9:33am
Print Post  
Thanks Slavcho, It's working.
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint