Search
Loading...
Skip to content

Flip

Flip images horizontally and vertically in CE.SDK for headless server-side processing. Mirror images for orientation correction, reflection effects, and batch operations.

8 mins
estimated time
Download
StackBlitz
GitHub

Flipping mirrors content along horizontal or vertical axes. Use flip operations for orientation correction, creating reflection effects, and adapting layouts.

This guide covers flipping images horizontally and vertically, understanding the difference between block flip and crop flip, querying flip state, and applying flips to multiple blocks.

Initialize Headless Engine#

Create a headless engine instance for programmatic manipulation:

// Initialize headless engine for programmatic creation
engine = await CreativeEngine.init({
// license: process.env.CESDK_LICENSE,
});

Create Scene#

Create a scene and page to hold the images:

// Create a new scene programmatically
const scene = engine.scene.create();
// Create and set up the page
const page = engine.block.create('page');
engine.block.setWidth(page, 800);
engine.block.setHeight(page, 500);
engine.block.appendChild(scene, page);

Flip Types#

CE.SDK provides two types of flip operations:

  • Block flip: Mirrors the entire block including its position context. Use setFlipHorizontal() and setFlipVertical().
  • Crop flip: Mirrors only the image content within its crop frame. Use flipCropHorizontal() and flipCropVertical().

Flip Horizontally#

Mirror an image along its vertical axis using engine.block.setFlipHorizontal(). This swaps the left and right sides:

// Flip the block horizontally (mirrors left to right)
engine.block.setFlipHorizontal(horizontalFlipImage, true);

Flip Vertically#

Mirror an image along its horizontal axis using engine.block.setFlipVertical(). This swaps the top and bottom:

// Flip the block vertically (mirrors top to bottom)
engine.block.setFlipVertical(verticalFlipImage, true);

Combine Flips#

Apply both horizontal and vertical flips to rotate an image 180 degrees:

// Combine horizontal and vertical flips
engine.block.setFlipHorizontal(bothFlipImage, true);
engine.block.setFlipVertical(bothFlipImage, true);

Flip Content Within Crop Frame#

Flip image content without affecting the block’s position using engine.block.flipCropHorizontal() and engine.block.flipCropVertical(). These methods toggle the flip state each time they are called:

// Flip the content within the crop frame (not the block itself)
engine.block.flipCropHorizontal(cropFlipImage);

Get Current Flip State#

Query the current flip state using engine.block.getFlipHorizontal() and engine.block.getFlipVertical():

// Get current flip state
const isFlippedH = engine.block.getFlipHorizontal(horizontalFlipImage);
const isFlippedV = engine.block.getFlipVertical(verticalFlipImage);
console.log(`Horizontal flip state: ${isFlippedH}`);
console.log(`Vertical flip state: ${isFlippedV}`);

Toggle Flip State#

Toggle a flip by reading the current state and setting the opposite value:

// Toggle flip by reading current state and setting opposite
const currentFlip = engine.block.getFlipHorizontal(originalImage);
engine.block.setFlipHorizontal(originalImage, !currentFlip);
// Toggle back for demo purposes
engine.block.setFlipHorizontal(originalImage, currentFlip);

Export Result#

Export the scene as a PNG file:

// Export the result as PNG
const blob = await engine.block.export(page, { mimeType: 'image/png' });
const buffer = Buffer.from(await blob.arrayBuffer());
writeFileSync('flip-images-output.png', buffer);
console.log('Successfully exported flip-images-output.png');

Cleanup#

Dispose the engine to free resources:

// Always dispose the engine to free resources
if (engine) {
engine.dispose();
}

Troubleshooting#

Flip Has No Visual Effect#

Ensure the block is appended to a page and the scene is loaded before applying flip. Verify the block exists using engine.block.isValid().

Content Appears in Wrong Position#

Check whether you need block flip or crop flip for your use case. Block flip mirrors the entire element, while crop flip mirrors only the content within the frame.

Engine Keeps Running#

Call engine.dispose() in a finally block to ensure resources are freed even if errors occur.

API Reference#

MethodDescription
CreativeEngine.init()Initialize the headless engine
engine.scene.create()Create a new scene
engine.block.create()Create a block of specified type
engine.block.addImage()Add an image block
engine.block.setFlipHorizontal()Set horizontal flip state
engine.block.setFlipVertical()Set vertical flip state
engine.block.getFlipHorizontal()Get horizontal flip state
engine.block.getFlipVertical()Get vertical flip state
engine.block.flipCropHorizontal()Flip content horizontally within crop frame
engine.block.flipCropVertical()Flip content vertically within crop frame
engine.block.export()Export block as image
engine.dispose()Dispose engine and free resources