Round rectangle
Introduction¶
Round rectangle on canvas.
- Author: Rex
- Game object
Live demos¶
Usage¶
Install plugin¶
Load minify file¶
- Load plugin (minify file) in preload stage
scene.load.plugin('rexroundrectanglecanvasplugin', 'https://raw.githubusercontent.com/rexrainbow/ phaser3-rex-notes/master/dist/rexroundrectanglecanvasplugin.min.js', true); - Add shape object
var rect = scene.add.rexRoundRectangleCanvas(x, y, width, height, radiusConfig, fillStyle, strokeStyle, lineWidth, fillColor2, isHorizontalGradient);
Import plugin¶
- Install rex plugins from npm
npm i phaser3-rex-plugins - Install plugin in configuration of game
import RoundRectanglePlugin from 'phaser3-rex-plugins/plugins/roundrectanglecanvas-plugin.js'; var config = { // ... plugins: { global: [{ key: 'rexRoundRectangleCanvasPlugin', plugin: RoundRectangleCanvasPlugin, start: true }, // ... ] } // ... }; var game = new Phaser.Game(config); - Add shape object
var rect = scene.add.rexRoundRectangleCanvas(x, y, width, height, radiusConfig, fillStyle, strokeStyle, lineWidth, fillColor2, isHorizontalGradient);
Import class¶
- Install rex plugins from npm
npm i phaser3-rex-plugins - Import class
import RoundRectangleCanvas from 'phaser3-rex-plugins/plugins/roundrectanglecanvas.js'; - Add shape object
var rect = new RoundRectangleCanvas(scene, x, y, width, height, radiusConfig, fillStyle, strokeStyle, lineWidth, fillColor2, isHorizontalGradient); scene.add.existing(rect);
Create shape object¶
var rect = scene.add.rexRoundRectangleCanvas(x, y, width, height, radiusConfig, fillStyle, strokeStyle, lineWidth, fillColor2, isHorizontalGradient);
width,height: Size of rectangle.undefined: Set otundefinedto draw a circle.
radius: Radius of four corners.0, orundefined: Disable round corner.- Number: 4 corners with the same radius.
- JSON
- 4 corners with the same radius X/Y
{ x: radiusX, y: radiusY } - Eeach radius of corner
or
{ tl: radius, tr: radius, bl: radius, br: radius }{ tl: {x : radiusX, y: radiusY}, tr: {x : radiusX, y: radiusY}, bl: {x : radiusX, y: radiusY}, br: {x : radiusX, y: radiusY}, } - Radius and iteration
or
{ radius: radius, iteration: 0 }or{ radius: {x: radiusX, y: radiusY}, iteration: 0 }{ radius: { tl: {x : radiusX, y: radiusY}, tr: {x : radiusX, y: radiusY}, bl: {x : radiusX, y: radiusY}, br: {x : radiusX, y: radiusY}, }, iteration: 0 }radius:0: No round corner> 0: Convex round corner< 0: Concave round corner
iteration: Number of interpolation points in each round corner. Default value is4.0: Draw a straight line instead of arc.
- 4 corners with the same radius X/Y
fillStyle: Fill color in number or css string value, or a callback- Number, or css string value : Color value
- Callback :
function(canvas, context) { // Radial gradient var grd = context.createRadialGradient(x0, y0, r0, x1, y1, r1); grd.addColorStop(0, color0); grd.addColorStop(1, color1); return grd; }
strokeStyle: Stroke color in number or css string value.lineWidth: Stroke line width.fillColor2: Gradient color in number or css string value.isHorizontalGradient:true: Horizontal gradient.false: Vertical gradient.
Note
If radius >= 0, draw convex corner, else draw concave corner
Deform¶
- Rectangle, set radius of 4 corners to
0.var rect = scene.add.rexRoundRectangleCanvas(x, y, width, height, 0, fillStyle, strokeStyle, lineWidth, fillColor2, isHorizontalGradient); - Circle, set width and height to
2*radius.var rect = scene.add.rexRoundRectangleCanvas(x, y, (2*radius), (2*radius), radius, fillStyle, strokeStyle, lineWidth, fillColor2, isHorizontalGradient); - Ellipse, set width and height to
2*radiusX,2*radiusY.var rect = scene.add.rexRoundRectangleCanvas(x, y, (2*radiusX), (2*radiusX), {x: radiusX, y: radiusY}, fillColor, fillAlpha); - Rhombus, set width and height to
2*radius, and assign iteration to0var rect = scene.add.rexRoundRectangleCanvas(x, y, (2*radius), (2*radius), { radius: radius, iteration: 0 }, fillStyle, strokeStyle, lineWidth, fillColor2, isHorizontalGradient); - Octagon, assign iteration to
0var rect = scene.add.rexRoundRectangleCanvas(x, y, width, height, { radius: radius, iteration: 0 }, fillStyle, strokeStyle, lineWidth, fillColor2, isHorizontalGradient);
Custom class¶
- Define class
class MyRoundRectangleCanvas extends RexPlugins.GameObjects.RoundRectangleCanvas { constructor(scene, x, y, width, height, radiusConfig, fillStyle, strokeStyle, lineWidth, fillColor2, isHorizontalGradient) { super(scene, x, y, width, height, radiusConfig, fillStyle, strokeStyle, lineWidth, fillColor2, isHorizontalGradient); // ... scene.add.existing(this); } // ... // preUpdate(time, delta) {} }scene.add.existing(gameObject): Adds an existing Game Object to this Scene.- If the Game Object renders, it will be added to the Display List.
- If it has a
preUpdatemethod, it will be added to the Update List.
- Create instance
var rect = new MyRoundRectangleCanvas(scene, x, y, width, height, radiusConfig, fillStyle, strokeStyle, lineWidth, fillColor2, isHorizontalGradient);
Color¶
- Fill color
- Get
var fillStyle = rect.fillStyle; var fillColor2 = rect.fillColor2; var isHorizontalGradient = rect.isHorizontalGradient; - Set
rect.setFillStyle(fillStyle); // rect.setFillStyle(fillStyle, fillColor2, isHorizontalGradient);fillStyle: Fill color in number or css string value, or a callback- Number, or css string value : Color value
- Callback :
function(canvas, context) { // Radial gradient var grd = context.createRadialGradient(x0, y0, r0, x1, y1, r1); grd.addColorStop(0, color0); grd.addColorStop(1, color1); return grd; }
- Clear
rect.setFillStyle();
- Get
- Stroke color
- Get
var strokeStyle = rect.strokeStyle; var lineWidth = rect.lineWidth; - Set
rect.setStrokeStyle(strokeStyle, lineWidth); - Clear
rect.setStrokeStyle();
- Get
Size¶
- Get
var width = rect.width; var height = rect.height; - Set
or
rect.setSize(width, height);rect.width = width; rect.height = height;
Radius¶
- Set
or
rect.setRadius(value);rect.radius = radius;radius:- Number : 4 corners with the same radius.
0: No round corner> 0: Convex round corner< 0: Concave round corner
- JSON
- 4 corners with the same radius X/Y
{ x: radiusX, y: radiusY } - Eeach radius of corner
or
{ tl: radius, tr: radius, bl: radius, br: radius }{ tl: {x : radiusX, y: radiusY}, tr: {x : radiusX, y: radiusY}, bl: {x : radiusX, y: radiusY}, br: {x : radiusX, y: radiusY}, }
- 4 corners with the same radius X/Y
- Number : 4 corners with the same radius.
Iteration¶
- Get
var iteration = rect.iteration; - Set
or
rect.setIteration(value);rect.iteration = value;
Number of interpolation points in each round corner. Default value is 4.
0: Draw a straight line instead of arc.
Other properties¶
See game object
Create mask¶
var mask = rect.createBitmapMask();
See mask
Shader effects¶
Support preFX and postFX effects
Compare with Shape-Roundrectangle¶
- Gradient
- Canvas-RoundRectangle can fill with gradient color.
- Shape-Roundrectangle can't fill gradient color.
- Drawing method
- Canvas-RoundRectangle draw shape on canvas, then map this canvas to frame buffer.
- Shape-Roundrectangle draw shape on frame buffer directly without drawing to canvas first.