Resizable workspace
Stay organized with collections
Save and categorize content based on your preferences.
A good web application will resize Blockly to fill the available space on
screen rather than being a fixed size. There are several ways to do this,
including using an iframe, CSS, and JavaScript positioning.
This page demonstrates an overlay approach which is robust and flexible.
This is a three-step process. The first step is to define the area.
The second step is to inject Blockly.
The third step is to position Blockly over this area.
1. Define the area
Using either an HTML table or a div
with CSS, create an empty area that
reflows as the page is resized.
Ensure that the area has an ID (on this page we will call it blocklyArea
).
Here is
a live demo
of a table cell that fills the bottom of the screen.
2. Inject the workspace
Injecting Blockly is the same as the process described in
injecting fixed-sized Blockly.
Add the code, the blocklyDiv
element, a toolbox, and an injection call.
Blockly should now be running on the page, just not located where it should be
(probably offscreen).
3. Position the workspace
The final step is to position the blocklyDiv
element over the blocklyArea
element. To do this, remove any height
and width
styles from blocklyDiv
and add absolute positioning:
<div id="blocklyDiv" style="position: absolute"></div>
Then replace the injection script with one that also positions 'blocklyDiv' over
'blocklyArea':
const blocklyArea = document.getElementById('blocklyArea');
const blocklyDiv = document.getElementById('blocklyDiv');
const workspace = Blockly.inject(blocklyDiv,
{toolbox: document.getElementById('toolbox')});
const onresize = function(e) {
// Compute the absolute coordinates and dimensions of blocklyArea.
const element = blocklyArea;
let x = 0;
let y = 0;
do {
x += element.offsetLeft;
y += element.offsetTop;
element = element.offsetParent;
} while (element);
// Position blocklyDiv over blocklyArea.
blocklyDiv.style.left = x + 'px';
blocklyDiv.style.top = y + 'px';
blocklyDiv.style.width = blocklyArea.offsetWidth + 'px';
blocklyDiv.style.height = blocklyArea.offsetHeight + 'px';
Blockly.svgResize(workspace);
};
window.addEventListener('resize', onresize, false);
onresize();
Here is
a live demo
of Blockly that fills the bottom of the screen.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-09-18 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-09-18 UTC."],[[["\u003cp\u003eThis guide demonstrates how to make a Blockly workspace resize dynamically to fill the available screen space using an overlay approach.\u003c/p\u003e\n"],["\u003cp\u003eThe process involves three steps: defining an area for Blockly, injecting the Blockly workspace, and positioning it over the defined area using JavaScript.\u003c/p\u003e\n"],["\u003cp\u003eThis approach allows for a robust and flexible Blockly integration that adapts to different screen sizes and layouts.\u003c/p\u003e\n"],["\u003cp\u003eSample code and live demos are provided to illustrate the implementation of a resizable Blockly workspace.\u003c/p\u003e\n"]]],["To make a web application's Blockly area resizable, a three-step overlay approach is used. First, define a resizable area (`blocklyArea`) using HTML and CSS. Second, inject Blockly into a `blocklyDiv` element. Third, position `blocklyDiv` over `blocklyArea` using absolute positioning. The script calculates `blocklyArea`'s coordinates, applies these to `blocklyDiv`, sets `blocklyDiv`'s dimensions, and resizes Blockly's SVG. This ensures Blockly fills the available space and adjusts with window resizing.\n"],null,["A good web application will resize Blockly to fill the available space on\nscreen rather than being a fixed size. There are several ways to do this,\nincluding using an iframe, CSS, and JavaScript positioning.\nThis page demonstrates an overlay approach which is robust and flexible.\n\nThis is a three-step process. The first step is to define the area.\nThe second step is to inject Blockly.\nThe third step is to position Blockly over this area.\n\n1. Define the area\n\nUsing either an HTML table or a `div` with CSS, create an empty area that\nreflows as the page is resized.\nEnsure that the area has an ID (on this page we will call it `blocklyArea`).\n\nHere is\n[a live demo](https://google.github.io/blockly-samples/examples/resizable-demo/index.html)\nof a table cell that fills the bottom of the screen.\n\n2. Inject the workspace\n\nInjecting Blockly is the same as the process described in\n[injecting fixed-sized Blockly](/blockly/guides/configure/web/fixed-size).\nAdd the code, the `blocklyDiv` element, a toolbox, and an injection call.\n\nBlockly should now be running on the page, just not located where it should be\n(probably offscreen).\n\n3. Position the workspace\n\nThe final step is to position the `blocklyDiv` element over the `blocklyArea`\nelement. To do this, remove any `height` and `width` styles from `blocklyDiv`\nand add absolute positioning: \n\n \u003cdiv id=\"blocklyDiv\" style=\"position: absolute\"\u003e\u003c/div\u003e\n\nThen replace the injection script with one that also positions 'blocklyDiv' over\n'blocklyArea': \n\n const blocklyArea = document.getElementById('blocklyArea');\n const blocklyDiv = document.getElementById('blocklyDiv');\n const workspace = Blockly.inject(blocklyDiv,\n {toolbox: document.getElementById('toolbox')});\n const onresize = function(e) {\n // Compute the absolute coordinates and dimensions of blocklyArea.\n const element = blocklyArea;\n let x = 0;\n let y = 0;\n do {\n x += element.offsetLeft;\n y += element.offsetTop;\n element = element.offsetParent;\n } while (element);\n // Position blocklyDiv over blocklyArea.\n blocklyDiv.style.left = x + 'px';\n blocklyDiv.style.top = y + 'px';\n blocklyDiv.style.width = blocklyArea.offsetWidth + 'px';\n blocklyDiv.style.height = blocklyArea.offsetHeight + 'px';\n Blockly.svgResize(workspace);\n };\n window.addEventListener('resize', onresize, false);\n onresize();\n\nHere is\n[a live demo](https://google.github.io/blockly-samples/examples/resizable-demo/overlay.html)\nof Blockly that fills the bottom of the screen."]]