Skip to main content
Platform:
Language:

How to Use Fills

Some design blocks in CE.SDK allow you to modify or replace their fill. The fill is an object that defines the contents within the shape of a block. CreativeEditor SDK supports many different types of fills, such as images, solid colors, gradients and videos.

Similarly to blocks, each fill has a numeric id which can be used to query and modify its properties.

Explore a full code sample of the integration on CodeSandbox or view the code on GitHub

Setup#

This example uses the headless CreativeEngine. See the Setup article for a detailed guide. To get started right away, you can also access the block API within a running CE.SDK instance via cesdk.engine.block. Check out the APIs Overview to see that illustrated in more detail.

Accessing Fills#

Not all types of design blocks support fills, so you should always first call the hasFill(id: number): boolean API before accessing any of the following APIs.

In order to receive the fill id of a design block, call the getFill(id: number): number API. You can now pass this id into other APIs in order to query more information about the fill, e.g. its type via the getType(id: number): string API.

For a rect shape design block, the fill defaults to a solid color.

Fill Properties#

Just like design blocks, fills with different types have different properties that you can query and modify via the API. Use findAllProperties(id: number): string[] in order to get a list of all properties of a given fill.

For the solid color fill in this example, the call would return ["fill/color/value", "type"].

Please refer to the API docs for a complete list of all available properties for each type of fill.

Once we know the property keys of a fill, we can use the same APIs as for design blocks in order to modify those properties. For example, we can use setColorRGBA(id: number, key: string, r: number, g: number, b: number, a: number): void in order to change the color of the fill to red.

Once we do this, our rect shape will be filled with solid red.

Disabling Fills#

You can disable and enable a fill using the setFillEnabled(id: number, enabled: boolean): void API, for example in cases where the design block should only have a stroke but no fill. Notice that you have to pass the id of the design block and not of the fill to the API.

Changing Fill Types#

All design blocks that support fills allow you to also exchange their current fill for any other type of fill. In order to do this, you need to first create a new fill object using createFill(type: string): number.

We currently support the following fill types:

  • '//ly.img.ubq/fill/color'
  • '//ly.img.ubq/fill/gradient/linear'
  • '//ly.img.ubq/fill/gradient/radial'
  • '//ly.img.ubq/fill/gradient/conical'
  • '//ly.img.ubq/fill/image'
  • '//ly.img.ubq/fill/video'

Note: short types are also accepted, e.g. 'image' instead of '//ly.img.ubq/fill/image'.

In order to assign a fill to a design block, simply call setFill(id: number, fill: number): void. Make sure to delete the previous fill of the design block first if you don't need it any more, otherwise we will have leaked it into the scene and won't be able to access it any more, because we don't know its id.

Notice that we don't use the appendChild API here, which only works with design blocks and not fills.

When a fill is attached to one design block, it will be automatically destroyed when the block itself gets destroyed.

Duplicating Fills#

If we duplicate a design block with a fill that is only attached to this block, the fill will automatically be duplicated as well. In order to modify the properties of the duplicate fill, we have to query its id from the duplicate block.

Sharing Fills#

It is also possible to share a single fill instance between multiple design blocks. In that case, changing the properties of the fill will apply to all of the blocks that it's attached to at once.

Destroying a block with a shared fill will not destroy the fill until there are no other design blocks left that still use that fill.

File:
import CreativeEngine from 'https://cdn.img.ly/packages/imgly/cesdk-engine/1.10.1/index.js';
const config = {
baseURL: 'https://cdn.img.ly/packages/imgly/cesdk-engine/1.10.1/assets'
};
CreativeEngine.init(config).then(async (engine) => {
document.getElementById('root').append(engine.element);
const scene = engine.scene.create();
const page = engine.block.create('page');
engine.block.setWidth(page, 800);
engine.block.setHeight(page, 600);
engine.block.appendChild(scene, page);
engine.scene.zoomToBlock(page, 40, 40, 40, 40);
const rect = engine.block.create('shapes/rect');
engine.block.setWidth(rect, 100);
engine.block.setHeight(rect, 100);
engine.block.appendChild(page, rect);
const circle = engine.block.create('shapes/ellipse');
engine.block.setPositionX(circle, 100);
engine.block.setPositionY(circle, 50);
engine.block.setWidth(circle, 300);
engine.block.setHeight(circle, 300);
engine.block.appendChild(page, circle);
engine.block.hasFill(scene); // Returns false
engine.block.hasFill(rect); // Returns true
const rectFill = engine.block.getFill(rect);
const defaultRectFillType = engine.block.getType(rectFill);
const allFillProperties = engine.block.findAllProperties(rectFill);
engine.block.setColorRGBA(rectFill, 'fill/color/value', 1.0, 0.0, 0.0, 1.0);
engine.block.setFillEnabled(rect, false);
engine.block.setFillEnabled(rect, !engine.block.isFillEnabled(rect));
const imageFill = engine.block.createFill('image');
engine.block.setString(imageFill, 'fill/image/imageFileURI', 'https://img.ly/static/ubq_samples/sample_1.jpg');
engine.block.destroy(engine.block.getFill(circle));
engine.block.setFill(circle, imageFill);
/* The following line would also destroy imageFill */
// engine.block.destroy(circle);
const duplicateCircle = engine.block.duplicate(circle);
engine.block.setPositionX(duplicateCircle, 450);
const autoDuplicateFill = engine.block.getFill(duplicateCircle);
engine.block.setString(autoDuplicateFill, 'fill/image/imageFileURI', 'https://img.ly/static/ubq_samples/sample_2.jpg');
// const manualDuplicateFill = engine.block.duplicate(autoDuplicateFill);
// /* We could now assign this fill to another block. */
// engine.block.destroy(manualDuplicateFill);
const star = engine.block.create('shapes/star');
engine.block.setPositionX(star, 350);
engine.block.setPositionY(star, 400);
engine.block.setWidth(star, 100);
engine.block.setHeight(star, 100);
engine.block.appendChild(page, star);
engine.block.setFill(star, engine.block.getFill(circle));
});