Creating a Plugin
jCanvas provides a plugin API so you can create methods which integrate with jCanvas. To do so, use the jCanvas.extend()
method.
The extend()
method accepts one object containing three properties:
name
: The name of the method you are creatingtype
: Optional; the type of drawing, which jCanvas will recognize as a valid value for thetype
property.props
: Optional; the custom properties your method uses (and their default values). These properties will be merged into the arguments object (mentioned below) for use in your method’s code.fn
: The function providing the plugin’s functionality. It accepts two arguments:- The context of the canvas
- The parameters object the method will receive when called
Example: drawHeart()
To demonstrate how this works, we’ll be creating a method that draws a heart on the canvas.
TypeScript
If you use TypeScript, the $.jCanvas.extend
method is generic and accepts a
single type argument representing the types of any new props you define.
Creating a definitions file (d.ts)
To extend the native jCanvas types to support your new methods/properties, create a d.ts
file with the same base name as your plugin file (e.g. if your plugin filename is jcanvas-crescent.ts
, then the definitions file should be jcanvas-crescent.d.ts
):
The definitions file should look something like this:
API Methods
The jCanvas object ($.jCanvas
) provides a few useful methods for integrating your methods with jCanvas. All of these methods accept the same three arguments: the canvas DOM element (this
), the canvas context (ctx
), and the parameters object (params
).
setGlobalProps()
: sets global canvas properties likefillStyle
,shadowColor
, etc.transformShape()
: Enables shape transformation using the standard transformation properties (rotate
,scale
,translate
). Note that theclosePath()
method must be called later on to restore the layer transformations.detectEvents()
: Enables and detects jCanvas events for your custom path. Note that this method should be called at the end of your path.closePath()
: Closes the current path, and fills/strokes it if the respective properties have been set. The method also enables masking for the path through the use of themask
property.setCanvasFont()
: Sets the font of the canvas context based on thefontStyle
,fontSize
, andfontFamily
properties.measureText()
: Augments the given parameter object with the calculatedwidth
andheight
of the text. Accepts an array of strings (representing lines of text) as a fourth argument.
Notes
When calling your method, jCanvas will automatically loop through selected canvas elements, so you don’t need to.