How to scroll the workspace from code correctly

1,170 views
Skip to first unread message

phil cleaver

unread,
Mar 26, 2014, 6:02:38 AM3/26/14
to blo...@googlegroups.com
I am having problems finding the correct way to scroll the workspace from code.

What I do at the moment is

Blockly.mainWorkspace.scrollX = Blockly.mainWorkspace.scrollX - desiredScrollX;
Blockly.mainWorkspace.scrollY = Blockly.mainWorkspace.scrollY - desiredScrollY;

Blockly.mainWorkspace.render();

It works, but if the workspace has lots of blocks it's really slow, even when using requestAnimationFrame.

Any help appreciated.

Phil


Mark Friedman

unread,
Mar 26, 2014, 2:34:27 PM3/26/14
to blo...@googlegroups.com
I think that ScrollbarPair.set() in scrollbar.js might be what you're looking for.  You could call it with something like Blockly.mainWorkspace.scrollbar.set(x, y).

-Mark


--
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.
For more options, visit https://groups.google.com/d/optout.

phil cleaver

unread,
Mar 27, 2014, 10:43:34 AM3/27/14
to blo...@googlegroups.com
Thanks a million marks, it works brilliantly
 :)

phil cleaver

unread,
Mar 29, 2014, 1:51:14 AM3/29/14
to blo...@googlegroups.com
Mark, 

Thanks for your help.  This method does indeed set the scrollbars and scroll the workspace, but I'm having difficulty finding the correct value to set them to.

If I set them to 0,0, I see the top left of the workspace, similarly if I set them to (contentwidth, contentheight), i see bottom right.  This is great for setting them to an absolute position, but I'm trying to get them to "scroll" in a relative way.

I'm having problems finding the currentPosition of scrollbars so I can add / subtract values for scrolling?

This also needs to work under different window zooming levels.

Thanks 

On Thursday, March 27, 2014 1:34:27 AM UTC+7, Mark Friedman wrote:

phil cleaver

unread,
Mar 29, 2014, 4:07:42 AM3/29/14
to blo...@googlegroups.com
to answer my own question, in case anyone else was wondering, this seems to work:

function scrollRelative(xAmount, yAmount) {
var metrics = Blockly.mainWorkspace.getMetrics();
var currentScrollX = (metrics.viewLeft - metrics.contentLeft );
var currentScrollY = (metrics.viewTop - metrics.contentTop );

Blockly.mainWorkspace.scrollbar.set(currentScrollX + xAmount, currentScrollY + yAmount);

Austin Bart

unread,
Feb 18, 2015, 2:14:05 PM2/18/15
to blo...@googlegroups.com
Anyone know how to scroll the view to the center? I was hoping that 

var newScrollX = (metrics.contentWidth - metrics.viewWidth ) / 2;
var newScrollY = (metrics.contentHeight - metrics.viewHeight ) / 2;
Blockly.mainWorkspace.scrollbar.set(newScrollX, newScrollY);

Would work, but I'm clearly missing something else. Any ideas?

Eric Miller

unread,
Feb 22, 2015, 11:51:04 PM2/22/15
to blo...@googlegroups.com
Try 
Blockly.mainWorkspace.scrollX = ...;
Blockly.mainWorkspace.scrollY = ...;
Blockly.mainWorkspace.render();

You want to update the workspace itself, not just the graphical scrollbars.
Reply all
Reply to author
Forward
0 new messages