@Markus Edenhauser You’re close. I’d recommend:
- Don’t use the browser window dimensions for this case.
- You also needn’t worry about zoom here … unless you are trying to dynamically resize the object that you create to always “look” a certain size at the current zoom level.
- Viewport has X and Y and importantly here, it also has ‘width’ and ‘height’ → which are the exact width in Miro pts, (not screen pixels)… to get from miro pts to screen pixels you’d convert with ‘zoom’. But I don’t believe you don’t need that in this case.
-
Use instead: viewport.width
and viewport.height
-
In Miro just like SVG/other web graphics the Y is positive in the ‘down’ direction…
-
centerXY = w (viewport.x + (viewport.width/2)) , (viewport.y + (viewport.height/2)) ]
@Max Harper Thanks for your reply. This was my initial approach, and it didn’t work as expected because I had used it wrongly later on
const centeredX = viewport.x + (viewport.width/2);
const centeredY = viewport.y + (viewport.height/2);
Using this to get the center of the viewport works like a charm.
Not entirely sure what the cause is but I’m unable to produce the desired results.
In order to center a widget I have used this:
(async () => {
let widget: IWidget;
// somehow obtain the widget that you want to zoom into.
const widgetBounds = widget.bounds;
const viewport = await miro.board.viewport.get();
const targetWidth = widgetBounds.width;
const targetHeight = widgetBounds.height;
const targetViewport = {
x: widgetBounds.x - (targetWidth / 2) - ((viewport.width - targetWidth) / 2),
y: widgetBounds.y - (targetHeight / 2) - ((viewport.height - targetHeight) / 2),
width: viewport.width,
height: viewport.height,
}
console.log("target viewport", targetViewport);
console.log("widget", widgetBounds);
console.log("viewport", viewport);
miro.board.viewport.set(targetViewport, {
padding: {
top: 0,
left: 0,
right: 0,
bottom: 0,
},
animationTimeInMS: 250
});
})()
This seems to produce the desired results: the widget is centered in the middle but it does have some issues:
- The left panels might be open so it’s not the center of the visible part of the canvas;
- It doesn’t alter the zoom level, if the canvas is zoomed out a lot the object will be in the middle but not very apparent;
What I’m wondering
- Is there a way to increase/decrease the current zoom/scale level?
- Is there a way to know if the toolbar panels are open?
Thanks for helping out!
-Marijn