Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Responsive HTML5 Canvas (Read 7986 times)
aiokos
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 5
Joined: Nov 9th, 2016
Responsive HTML5 Canvas
Nov 9th, 2016 at 3:30pm
Print Post  
Hi, I'm trying to make the HTML5 canvas responsive to changes in its container element's size.

Setting the canvas width to 100% via CSS achieves the desired effect, however, I cannot figure out how to reset the zoom scale of the MindFusion diagram so the ShapeNodes on the canvas look crisp. The canvas will resize, but all of the MF elements look fuzzy and the zooming behavior gets messed up. I've tried using `canvas.setBounds()` and various other methods in the `diagram` class, but haven't been successful.

Here's some code that I am using to achieve the resizing effect. Using MVC 5, Angular 1.5.8, and latest MindFusion builds.

Code (CSS)
Select All
#my-canvas {
  width: 100%;
}
 



Code (Javascript)
Select All
function scaleCanvas (scale) {
  scale = scale || 1;
  var viewport = angular.element('#viewport');
  var point = canvas.clientToDoc({ x: viewport.width() * scale, y: viewport.height() * scale });
  var dim = {
    width: point.x,
    height: point.y
  };
  var rect = new MindFusion.Drawing.Rect(0, 0, Math.floor(dim.width), Math.floor(dim.height));
  canvas.setBounds(rect);  // Instance of MindFusion Diagram fed in via Angular service, code omitted
  canvas.setZoomToRect(rect);
}
 



Thank you!
« Last Edit: Nov 10th, 2016 at 4:18pm by aiokos »  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3435
Joined: Oct 19th, 2005
Re: Responsive HTML5 Canvas
Reply #1 - Nov 10th, 2016 at 7:37am
Print Post  
Hi,

setZoomFactor parameter should be a number and not a rectangle. I guess you meant to call zoomToRect? Calling something like diagram.zoomToRect(diagram.getBounds()) from resize event handler should make sure the diagram's content always fits inside the current view. In that case you should not change the diagram's Bounds dynamically but initialize it just once to match boundaries of diagram's elements.

Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
aiokos
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 5
Joined: Nov 9th, 2016
Re: Responsive HTML5 Canvas
Reply #2 - Nov 10th, 2016 at 4:10pm
Print Post  
Sorry, the `zoomFactor` was a typo in my post, but not in the actual code.

The `sizeChanged` event seems to only be called with the size is changed programmatically, which makes it difficult to work with when wanting a responsive canvas. When the canvas is resized via browser restrictions or other means with the `width: 100%` property, the `sizeChanged` event isn't called. Is this intended, or is there another event that I can use that will watch for changes of the actual canvas element itself? I guess I am looking for an API method that will resize the canvas element and the MindFusion diagram's bounds automatically, based on the size of the containing element.

It seems in my testing that the actual MindFusion diagram doesn't know where the bounds of the canvas element are, as it seems that even when the actual canvas element size changes the MindFusion `diagram.getBounds()` method returns the same value.
  
Back to top
 
IP Logged
 
aiokos
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 5
Joined: Nov 9th, 2016
Re: Responsive HTML5 Canvas
Reply #3 - Nov 10th, 2016 at 4:20pm
Print Post  
Additionally, is there a way to set the viewport of the MindFusion diagram programmatically? I've searched through the API but it seems that there is only a method to get it. The method is diagram.getViewport().
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3435
Joined: Oct 19th, 2005
Re: Responsive HTML5 Canvas
Reply #4 - Nov 10th, 2016 at 6:42pm
Print Post  
Diagram.Bounds defines the origin and size of diagram's local coordinate system. Bounds size * MeasureUnit size determines the canvas element size, but not its offset in the page (which you can control or access using regular DOM APIs).

For example, if you set Bounds to (-500,-500,1000,1000), you can place elements at coordinates in that range, and scroll to them if parent div is smaller. Calling zoomToRect(getBounds()) will fit that 1000x1000 logical units rectangle inside the parent div's size in pixels.

You can handle the window.resize event to detect size changed by user.
  
Back to top
 
IP Logged
 
aiokos
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 5
Joined: Nov 9th, 2016
Re: Responsive HTML5 Canvas
Reply #5 - Nov 10th, 2016 at 7:49pm
Print Post  
So how would I go about expanding the height/width of the MindFusion canvas, without affecting the nodes on the canvas?

I want to avoid any stretching behavior, and it seems that even when utilizing the measureUnit property, I can't properly tell MindFusion to expand the width of its canvas without changing the resolution or position of the nodes already on the canvas. Resizing using the method you described results in a mis-configured viewport where clicks are no longer registering in the right locations with regards to the canvas. I thought that setBounds would solve this, but it seems to do more than simply altering the height and width of the canvas.
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3435
Joined: Oct 19th, 2005
Re: Responsive HTML5 Canvas
Reply #6 - Nov 11th, 2016 at 11:54am
Print Post  
You shouldn't change width and height of the canvas, but the size of its parent div instead. The div also implements scrolling, so if it gets smaller than the canvas size determined by Bounds and MeasureUnit, it will show scrollbars to let you scroll.
« Last Edit: Nov 11th, 2016 at 6:33pm by Slavcho »  
Back to top
 
IP Logged
 
aiokos
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 5
Joined: Nov 9th, 2016
Re: Responsive HTML5 Canvas
Reply #7 - Nov 11th, 2016 at 4:44pm
Print Post  
Slavcho, thank you for the help with this issue, you've cleared up a lot of questions I had regarding the canvas-MindFusion interactions.
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint