Demos
One-click page rotation
Swap canvas width/height and rotate all elements by 90° with one button
This example adds a Rotate button that swaps each page’s width and height, then rotates its static elements by 90°. It handles nested groups and pages with different dimensions. Animation keyframes need separate updates.
import { Button } from 'polotno/primitives';
import { forEveryChild } from 'polotno/model/group-model';
function rotatePages(store) {
const pages = store.pages.map((page) => ({
page,
width: page.width,
height: page.height,
computedHeight: page.computedHeight,
}));
store.setSize(store.height, store.width);
pages.forEach(({ page, width, height, computedHeight }) => {
// Preserve page overrides and dimensions inherited from the store.
page.set({ width: height, height: width });
forEveryChild(page, (element) => {
if (element.type === 'group') return;
element.set({
x: computedHeight - element.y,
y: element.x,
rotation: (element.rotation || 0) + 90,
});
});
});
}
const PageRotate = ({ store }) => (
<Button onClick={() => rotatePages(store)}>Rotate</Button>
);How it works
- Swap the store size and each page’s size overrides. Dimensions set to
autocontinue to follow the store. - Visit every leaf element, including those inside groups. A clockwise quarter turn maps
(x, y)to(pageHeight - y, x). - Add 90° to each element’s rotation. The element turns with the page; text also turns sideways.
When is this handy?
- Quickly test both orientations of a social graphic.
- Offer users an instant “make it landscape” button in a print template.
Use the toolbar’s ActionControls slot so the button is available without selecting a page or element:
<Toolbar store={store} components={{ ActionControls: PageRotate }} />