You can display a square image as a circle by creating a custom circular mask in Cocos Creator.
- Create a Canvas and add a Sprite containing the image.
- Add a node under the Canvas and name it
Mask. - Set the Mask node’s content size to the same dimensions as the image.
- Add a
Graphicscomponent to the Mask node. - Attach the following TypeScript component to the Mask node:
1 | import { _decorator, Component, color, Graphics, UITransform } from 'cc'; |
- In the Inspector, connect the
maskGraphicsproperty ofMaskCircleto the node’sGraphicscomponent. - Position the Mask node over the image and make the image node a child of the Mask node so that the mask affects it.
- Run the scene. The image should now appear circular.
Adjust the Mask node’s dimensions to match the source image, and customize the shape as needed.