Skip to content
On this page

Shape

Shape is the source of display elements like Arc, Rect...

Any method or something that exists in Shape also exists in its children

Requrie Option

NameTypeDescription
xMayBeRef<number>x
yMayBeRef<number>y

Optional Option

NameTypeDefaultDescription
fillAfterStrokeEnabledMayBeRef<boolean>falseif enabled then fill() will be executed before stroke()
fillEnabledMayBeRef<boolean>trueif enabled then fill() will be executed
strokeEnabledMayBeRef<boolean>trueif enabled then stroke() will be executed
strokeMayBeRef<string | CanvasGradient | CanvasPattern>"#000"Resource to draw it accept hexa color code, rgb, hls, hue... More
fillMayBeRef<string | CanvasGradient | CanvasPattern>"#000"Resource to draw it accept hexa color code, rgb, hls, hue... More
strokeWidthMayBeRef<number>1border size
hitStrokeWidthMayBeRef<number>1
strokeHitEnabledMayBeRef<boolean>falseif enabled then strokeHit() will be executed
perfectDrawEnabledMayBeRef<boolean>trueuse redraw optimization strategy
shadowForStrokeEnabledMayBeRef<boolean>falseenable shading for the whole border
lineJoinMayBeRef<"bevel" | "round" | "miter">"miter"https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineJoin
lineCapMayBeRef<"butt" | "round" | "square">"butt"https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineCap
sceneFunc(ctx: Context2D) => voidundefinedfunction execute drawing when customizing Shape
fillPriorityMayBeRef<"color" | "linear-gradient" | "radial-gradient" | "pattern">autofill style when there are many options. By default Shape searches in order
fillPatternMayBeRef<FillPattern>undefinedsee below
fillLinearGradientMayBeRef<FillModelLinearGradient>undefinedsee below
fillLinearGradientMayBeRef<FillModelLinearGradient>undefinedsee below
shadowEnabledMayBeRef<boolean>trueenable shadow
shadowMayBeRef<Shadow>undefiedsee below
dashMayBeRef<number[]>undefinedSets the line dash pattern used when stroking lines. It uses an array of values that specify alternating lengths of lines and gaps which describe the pattern. Equivalent to: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setLineDash
dashEnabledMayBeRef<boolean>trueSet if the line dash pattern should be used
visibleMayBeRef<boolean>trueshow shape
filterMayBeRef<"none" | Filter>"none"filter
xMayBeRef<number>0x from option Transform
yMayBeRef<number>0y from option Transform
scaleMayBeRef<{ x: number; y: number }>{ x: 1, y: 1 }scale from option Transform
rotationMayBeRef<number>0rotation from option Transform
offsetMayBeRef<{ x: number; y: number }>{ x: 1, y: 1 }offset from option Transform
skewXMayBeRef<number>1skewX from option Transform
skewYMayBeRef<number>1skewY from option Transform

Complex options

fillPattern

This option allows to fill with complex things like images, canvas.

See more at: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/createPattern

ts
interface FillPattern extends TransformOptions {
  image: CanvasImageSource | string
  repeat?: "repeat" | "repeat-x" | "repeat-y" | "no-repeat"
}
NameTypeDescription
imageMayBeRef<CanvasImageSource | string>this option takes the same parameter as image mentioned in Image
repeatMayBeRef<"repeat" | "repeat-x" | "repeat-y" | "no-repeat">how to repeat the pattern's image

TIP

fillPattern accepts any parameters that Transform owns

fillLinearGradient

This option of the Canvas 2D API creates a gradient along the line connecting two given coordinates.

See more at: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/createLinearGradient

ts
interface FillModelLinearGradient {
  start: { x: number; y: number }
  end: { x: number; y: number }
  colorStops: [number, string][]
}
NameTypeDescription
startMayBeRef<Offset>The {x; y} coordinate of the start point.
endMayBeRef<Offset>The {x; y} coordinate of the end point.
colorStopsMayBeRef<[number, string][]>method adds a new color stop, defined by an offset and a color, to a given canvas gradient. [offset, color][]. See more: https://developer.mozilla.org/en-US/docs/Web/API/CanvasGradient/addColorStop

fillRadialGradient

This option of the Canvas 2D API creates a radial gradient using the size and coordinates of two circles.

See more at: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/createRadialGradient

ts
interface FillModelRadialGradient {
  start: { x: number; y: number }
  startRadius: number
  end: { x: number; y: number }
  endRadius: number
  colorStops: [number, string][]
}
NameTypeDescription
startMayBeRef<Offset>The {x; y} coordinate of the start point.
startRadiusMayBeRef<number>The radius of the start circle. Must be non-negative and finite.
endMayBeRef<Offset>The {x; y} coordinate of the end point.
endRadiusMayBeRef<number>The radius of the end circle. Must be non-negative and finite.
colorStopsMayBeRef<[number, string][]>method adds a new color stop, defined by an offset and a color, to a given canvas gradient. [offset, color][]. See more: https://developer.mozilla.org/en-US/docs/Web/API/CanvasGradient/addColorStop

shadow

This option allows shading an image

ts
interface Shadow {
  x?: number
  y?: number
  color: string
  blur: number
}
NameTypeDescription
xMayBeRef<number>specifies the distance that shadows will be offset horizontally. see more
yMayBeRef<number>specifies the distance that shadows will be offset vertically. see more
colorMayBeRef<string>specifies the color of shadows. see more
blurMayBeRef<string>specifies the blur. see more

Get

$ (as attrs)

A Proxy that contains all the settings allowing to get/set the value of props and react automatically

ts
shape.$.x = 100

console.log(shape.$.x) // 100

clientRect

Returns a Rect object describing the internal size and location

ts
shape.clientRect // { x: 0, y: 0, width: 100, height: 100 }

bounding (as getBoundingClientRect())

Returns a Rect object describing the internal size and generate

ts
shape.bounding // { x: 10, y: 10, width: 100, height: 100 }

Protected Methods

These functions are only available when you customize or inherit Shape

getFill(ctx)

This function will return a FillStyle that can be filled based on the option set to Shape

ts
getFill(ctx: Canvas2D): FillStyle | void

fillScene(ctx: Canvas2D, path?: Path2D)

Fill Shape optionally and if path exists it will fill as specified by path

See more: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/fill#specifying_a_path_and_a_fillrule

ts
fillScene(ctx: Canvas2D, path?: Path2D): void

getStroke(ctx: Canvas2D)

Returns the possible value of stroke based on the option of Shape

strokeScene(ctx: Canvas2D)

Draw border

ts
strokeScene(ctx: Canvas2D): void

fillStrokeScene(ctx: Canvas2D): void

Do fillScene, strokeScene and optionally shader. It depends on setting shadowForStrokeEnabled, fillAfterStrokeEnabled to decide the order in which to do these 3 things

ts
fillStrokeScene(ctx: Canvas2D): void

getSize()

Returns the size of the shape

Methtods

getBoundingClientRect()

This function returns the size and position of the expression of Stage it's the same as Element.getBoundingClientRect

ts
getBoundingClientRect(): {
  width: number
  height: number
  x: number
  y: number
}

draw()

Make Shape draw once

isPressedPoint(x, y)

Check if the point with coordinates {x,y} is in Shape or not

ts
isPressedPoint(x: number, y: number): boolean

addTo(layer)

Where to add Layer

ts
addTo(stage: Layer): void

it is equivalent to

ts
layer.add(layer)

destroy()

Destroy Shape

ts
destroy(): void

Released under the MIT License.