How to center the blocks?

562 views
Skip to first unread message

Antonio Dal Borgo

unread,
May 26, 2021, 5:50:42 AM5/26/21
to Blockly
Hi,
in my Blockly based app, when I load a script from file, the blocks are often loaded messily, I would like add a function that centers blocks automatically.
Thanks for any reply.
Antonio

Beka Westberg

unread,
May 26, 2021, 5:59:44 PM5/26/21
to blo...@googlegroups.com
Hello Antonio,

There are a couple of built-in functions provided by Blockly that may help you out.
1) The workspace.zoomCenter function centers the view of the blocks (without moving the blocks themselves).
2) The workspace.zoomToFit function puts all of the blocks within view (again without moving the blocks themselves).
3) The workspace.cleanup function, which moves the blocks so that they are all in a column.

If you want to create your own function that rearranges the blocks (similar to workspace.cleanup) I recommend checking out the source code for cleanup. This will show you which functions you might want to use to rearrange your blocks =)

I hope that helps! If you have any further questions please reply!
--Beka

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/3c13358f-4d5e-47c4-88eb-3d613b2be0d1n%40googlegroups.com.

Antonio Dal Borgo

unread,
May 27, 2021, 6:03:28 AM5/27/21
to Blockly
Thanks for the info, I solved the problem with cleanUp() and translate().

Meer Estiyak

unread,
Sep 12, 2023, 1:58:27 AM9/12/23
to Blockly
zoomToFit makes it center but also zooms, how can i achieve this without need of scaling, i want the center only

Tobias Mouritzen

unread,
Sep 12, 2023, 3:23:44 AM9/12/23
to Blockly
Hi,

I think you can use the workspace.scrollCenter()

Message has been deleted

Meer Estiyak

unread,
Sep 12, 2023, 3:57:00 AM9/12/23
to Blockly
worked like a charm, thanks a million!
Reply all
Reply to author
Forward
0 new messages