Skip to main content

Raffaello_CanvasLayer

Main elements to draw on a Raffaello_Canvas. They should only be called via Raffaello_Canvas.addLayer() methods. This will ensure each layers are added accordingly to the Raffaello_Canvas.layers array.

Raffaello_Canvas should typically be initiated with .addLayer() methods within the templateInstructions() function of the global class.

Usage exemple
new class {
constructor() {
this.canvas = new Raffaello_Canvas({ ... });
}

templateInstructions() {
// Capture the context of the template
const thisTemplate = this;

// LAYER 0
this.canvas.addLayer().draw(function() {
...
});

// LAYER 1
this.canvas.addLayer().draw(function() {
...
});

// ETC.
}
}

Initiated Properties

PropertyTypeDescription
this.canvas<canvas>DOM canvas element that will hold all the drawings.
this.contextctxthis.canvas 2D rendering context. Initiated with getContext().
this.widthnumberThe canvas width
this.heightnumberThe canvas height
this.isDrawnbooleanReturns true if the canvas has been drawn once
this.boundsobjectReference the most extreme boudaries of the canvas
this.bounds.minXnumberOuter-right boundary coordinate
this.bounds.maxXnumberOuter-left boundary coordinate
this.bounds.minYnumberOuter-top boundary coordinate
this.bounds.maxYnumberOuter-bottom boundary coordinate

Public Methods

draw()

Method to pass the drawing instructions to the Raffaello_Canvas.addLayer().

Usage
this.canvas.addLayer().draw(function() {
// Layer drawing instructions
});

Parameters:

.addLayer(function)
NameRequiredTypeDefaultDescription
unamedfunction{}The drawing instructions for the layer.

Returns:

void — This method performs a side effect (add the drawing instructions to the layer). It does not return any value.


drawText()

Method to draw text content upon a Raffaello_Canvas.addLayer().

Usage (Extended)
this.drawText({
text: thisTemplate.container.querySelector('.js-textInput').value,
fontFamily: 'RTSNeue-Medium',
fontSize: 30,
fontLineHeight: 34, // (Optional) Default 'fontSize' value
fontKerningOptical: true, // (Optional) Default false
fontLetterSpacing: 0, // (Optional) Default 0 // (in prct)
fontFillColor: 'white', // (Optional) Default 'white'
textAlign: { // (Optional)
horizontal: 'left', // Default 'left' // 'left' | 'center' | 'right'
vertical: 'bottom', // Default 'bottom' // 'top' | 'center' | 'bottom'
},
textBaseline: 'hanging', // (Optional) Default 'alphabetic'
anchorPoint: 'true', // (Optional) Default 'none' // 'none' | 'true' | 'L'
position: {
x: 40,
y: 1090,
maxWidth: 0, // (Optional) Default '0' // '0' means no limit
maxWidthRescale: false, // (Optional) Default false // false (means line break) | true (means rescale)
maxLines: 0, // (Optional) Default '0' // '0' means no limit
ignoreEmptyLines: true, // (Optional) Default true
ignoreDoubleSpaces: true, // (Optional) Default true
},
bounds: [ true, false ], // (Optional) Default [ true, true ] // [ include ascent?, include descent? ]
highlight: { // (Optional) Change style of text between '§'
fontFamily: 'RTSNeueACTU-ExtraLight',
fontSize: 30,
fontLetterSpacing: 0,
fontFillColor: 'white',
},
background: 'red', // (Optional)
background: { // (Optional)
color: 'red', // (Optional) Default 'red'
stroke: { // (Optional)
style: "middle", // Default 'middle' // 'middle' | 'inside' | 'outside'
color: 'red', // Default 'red'
width: 0,
},
cornerRadius: 0, // (Optional) Default 0
margins: { // (Optional)
top: 0,
bottom: 0,
right: 0,
left: 0,
},
},
});
this.applyFilter(
'drop-shadow(0px 0px 60px rgb(0 0 0 / 70%))'
);

Parameters:

.drawText(config)
config ParametersRequiredTypeDefaultDescription
textHTMLElement / stringnullThe HTML element or text content to render.
fontFamilystringnullFont family used to render the text.
fontSizenumbernullSize of the font in pixels.
fontLineHeightnumberfontSizeLine height, defaults to font size.
fontKerningOpticalbooleanfalseEnables optical kerning if supported.
fontLetterSpacingnumber0Letter spacing in percentage (e.g. 0 = default spacing).
fontFillColorCSS colorwhiteFill color of the text.
textAlign.horizontalstringleftHorizontal alignment: 'left' | 'center' | 'right'.
textAlign.verticalstringbottomVertical alignment: 'top' | 'center' | 'bottom'.
textBaselinestringalphabeticCanvas text baseline alignment (e.g. 'alphabetic').
anchorPointstringtrueAnchor reference: 'none' | 'true' | 'L'.
positionobject{}Object defining position and layout constraints.
position.xnumbernullX coordinate of the text position.
position.ynumbernullY coordinate of the text position.
position.maxWidthnumber0Max width in pixels. 0 means no limit.
position.maxWidthRescalebooleanfalseIf true, text will rescale to fit instead of breaking lines.
position.maxLinesnumber0Max number of lines. 0 means no limit.
position.ignoreEmptyLinesbooleantrueSkip empty lines when rendering text.
position.ignoreDoubleSpacesbooleantrueSkip multiple consecutive spaces.
boundsarray[true, true][ include ascent?, include descent? ].
highlightobjectundefinedOptional styling for text between § markers.
highlight.fontFamilystringInheritsFont family for highlighted segments.
highlight.fontSizenumberInheritsFont size for highlighted segments.
highlight.fontLetterSpacingnumberInheritsLetter spacing for highlights.
highlight.fontFillColorCSS colorInheritsFill color for highlights.
backgroundstring / objectundefinedBackground color or configuration object.
background.colorCSS color'red'Background fill color.
background.stroke.stylestring'middle'Stroke placement: 'middle' | 'inside' | 'outside'.
background.stroke.colorCSS color'red'Stroke color.
background.stroke.widthnumber0Stroke width.
background.cornerRadiusnumber0Corner radius.
background.margins.topnumber0Top margin.
background.margins.bottomnumber0Bottom margin.
background.margins.leftnumber0Left margin.
background.margins.rightnumber0Right margin.

Returns:

void — This method performs a side effect (draw text on the canvas). It does not return any value.


drawImage()

custom version of this.context.drawImage() — same parameters as CanvasRenderingContext2D: drawImage(), but with custom image type (Raffaello_ImageCropper or Raffaello_Image).

Usage
// Short (1 argument)
this.drawImage(thisTemplate.inputImage);

// Extended (3 arguments)
this.drawImage(thisTemplate.inputImage, 0, 0);

// Extended (5 arguments)
this.drawImage(thisTemplate.inputImage, 0, 0, 100, 100);

⚠️ This method behaves like CanvasRenderingContext2D.drawImage, but also supports Raffaello images as input.

Parameters:

drawImage(image, dx, dy)
drawImage(image, dx, dy, dWidth, dHeight)
drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight)
ParameterRequiredTypeDefaultDescription
imageRaffaello_ImageCropper / Raffaello_Image / HTMLImageElementnullThe image-like object to draw.
sxnumber0Source X: horizontal coordinate of the top-left corner of the sub-rectangle to draw from the source image.
synumber0Source Y: vertical coordinate of the top-left corner of the sub-rectangle to draw.
sWidthnumberimage widthWidth of the source sub-rectangle to draw. Defaults to full image width.
sHeightnumberimage heightHeight of the source sub-rectangle to draw. Defaults to full image height.
dxnumber0Destination X: horizontal position where to place the image on the canvas.
dynumber0Destination Y: vertical position on the canvas.
dWidthnumbersWidthWidth to draw the image on the canvas. Can scale it.
dHeightnumbersHeightHeight to draw the image on the canvas.

Returns:

void — This method performs a side effect (draw an image on the canvas). It does not return any value.


drawRect()

This method wraps the canvas rectangle drawing functionality with simplified parameters and extras like corner radius and stroke alignment.

Usage
this.drawRect({
x: 0, // Default 0
y: 0, // Default 0
width: 100, // Default 'this.canvas.width'
height: 100, // Default 'this.canvas.height'
color: "black", // (Optional) Default 'black'
cornerRadius: 0, // (Optional) Default 0 // 5 || [5] | [5, 5] | [5, 10, 5, 10]
stroke: {
style: "middle", // (Optional) Default 'middle' // 'inside' | 'outside' | 'middle'
width: 0, // (Optional) Default 0
color: "black", // (Optional) Default 'black'
},
})

If no stroke is needed, we can omit the stroke object entirely.

Parameters:

drawRect(config)
config ParameterRequiredTypeDefaultDescription
xnumber0X coordinate of the top-left corner of the rectangle.
ynumber0Y coordinate of the top-left corner of the rectangle.
widthnumbercanvas.widthWidth of the rectangle in pixels.
heightnumbercanvas.heightHeight of the rectangle in pixels.
colorCSS color'black'Fill color of the rectangle.
cornerRadiusnumber | array0Border radius in pixels. Can be a single value (5), or array: [5], [5, 5], [5, 10, 5, 10].
stroke.stylestring'middle'Stroke alignment: 'inside', 'outside', or 'middle'.
stroke.widthnumber0Stroke width in pixels.
stroke.colorCSS color'black'Stroke color of the rectangle.

Returns:

void — This method performs a side effect (draw on the canvas). It does not return any value.


drawStar()

This method draws a star centered on (x_center, y_center) with alternating inner and outer points.

Usage
this.drawStar({
x_center: 0,
y_center: 0,
spikes: 5,
color: "red",
outerRadius: 100,
innerRadius: 150,
})

The number of spikes determines how many points the star will have (e.g. 5 = typical star). You can customize the shape with outerRadius and innerRadius for sharper or rounder stars.

Parameters:

drawStar(config)
config ParametersRequiredTypeDefaultDescription
x_centernumbernullX coordinate of the star's center.
y_centernumbernullY coordinate of the star's center.
spikesnumbernullNumber of spikes (points) the star should have.
outerRadiusnumbernullRadius of the outer tips of the star.
innerRadiusnumbernullRadius of the inner dips between star points.
colorCSS color'red'Fill color of the star shape.

Returns:

void — This method performs a side effect (draw on the canvas). It does not return any value.


drawGradient()

This method simplifies createLinearGradient() by automatically calculating the start and end points based on the bounding rectangle and angle.

Usage
this.drawGradient({
x: 10, // (Optional) Default 0
y: 10, // (Optional) Default 0
width: 90, // (Optional) Default 100
height: 80, // (Optional) Default 100
angle: 0, // (Optional) Default 0 // (in deg, clockwise) 0 (upward) to 360
gradient: [ // (Optional) Default from '0% black' to '100% black'
[0.0, 'rgba(0,0,0, 0)'], // 0.0 - Start
[1.0, 'rgba(0,0,0, 0.8)'], // 1.0 - End
],
})

The gradient array defines color stops just like in native canvas, but easier to write.

Parameters:

drawGradient(config)
config ParametersRequiredTypeDefaultDescription
xnumber0X coordinate of the top-left corner of the gradient area.
ynumber0Y coordinate of the top-left corner of the gradient area.
widthnumber100Width of the gradient rectangle.
heightnumber100Height of the gradient rectangle.
anglenumber (degrees)0Angle of the gradient in degrees, clockwise. 0 means vertical (top to bottom), 90 is left to right, etc.
gradientarray[[0, 'black'], [1, 'black']]Array of gradient stops: each stop is a tuple [offset, color], where offset is between 0.0 and 1.0.

Returns:

void — This method performs a side effect (draw on the canvas). It does not return any value.


prepareFilter()

Coming soon.


applyFilter()

Coming soon.

this.applyFilter(`drop-shadow(0px 0px 30px rgba(0, 0, 0, 1))`);

applyLUT()

Coming soon.