Helpers
For some cases, Grapper offers helper functions to simplify the construction of the values accepted by the SVG attribute. These helpers are available in the $ object, accessible from the g-bind and g-content expressions:
g-bind
$.elementrefers to the wrapper of the element where theg-binddirective has been included. With this reference it is possible to query other attributes of the element such as$.element.x()or$.element.stroke().$.attributeis the name of the attribute in which theg-binddirective has been included.$.currentValue()returns the current value of the property that has theg-binddirective. It is useful when we need to know the value of the property before modifying it. If the attribute isclassreturns and array of strings. If the attribute isstylereturns an object withkeyandvalue.$.polar2cartesian(cx, cy, r, angleDegrees)this global helper returns anx,ypoint from an angular value, a center point, and a radius. Parameters:cxcenter x,cycenter y,rradius,angleDegreesangle in degrees. Returns:{x : *, y : *}.$.degrees2radians(degrees): this global helper converts an angle from degrees to radians. Parameters:degreesangle in degrees. Returns: angle in radians.
g-bind:transform
The transform attribute values are converted in helper functions. You can use these helpers to build the transformation. You can combine the helpers, as $.translate(10,10).rotate(45).
$.translate(<x>, [<y>)moves the object byxandy, ifyis not provided, it is assumed to be0.$.scale(<x>, [<y>])specifies a scale operation byxandy, ifyis not provided, it is assumed to be equal tox.$.rotate(<a>, [<x>, <y>])define a rotation by a degreesaabout a given point (xandy), ifxandyare not supplied, the rotation is about the origin coordinate system.$.skewX(<a>)and$.skewY(<a>)specify a skew transformation along thexandyaxis byadegrees.$.matrix(<a>, <b>, <c>, <d>, <e>, <f>)modify the form by a transformation matrix.
Example:
g-bind:d in path
Constructing the d attribute of a path in SVG may seem complicated, but it is a series of relatively simple instructions. Grapper offers a series of helpers to construct the value of d step by step, chaining the different functions.
M(<x>, <y>)m(<x>, <y>)move to absolute and relative point.Z()z()close path.L(<x>, <y>)l(<x>, <y>)line to absolute and relative point.H(<x>)h(<x>)horizontal line to absolute and relativexpoint.V(<y>)v(<y>)vertical line to absolute and relativeypoint.C(<x1>, <y1>, <x2>, <y2>, <x>, <y>)c(<x1>, <y1>, <x2>, <y2>, <x>, <y>)absolute and relative cubic Bézier curve.S(<x1>, <y1>, <x2>, <y2>)s(<x1>, <y1>, <x2>, <y2>)absolute and relative smooth cubic Bézier curve.Q(<x1>, <y1>, <x>, <y>)q(<x1>, <y1>, <x>, <y>)absolute and relative quadratic Bézier curve.T(<x>, <y>)t(<x>, <y>)absolute and relative smooth quadratic Bézier curve.A(<rx>, <ry>, <rot>, <arc-flag>, <sweep-flag>, <x>, <y>)a(<rx>, <ry>, <rot>, <arc-flag>, <sweep-flag>, <x>, <y>)absolute and relative arc curve.
Example:
:d="$.arc(cx, cy, r, grades, [start = 0] )"
Creates an arc (section of the circumference) based on a center (cx and cy), a radius (r), positive or negative number of grades (grades), and optionally a start angle (in degrees).
Parameters:
cx- center xcy- center yr- radiusgrades- grades in degreesstart- start position in degrees (optional, default value0)
:d="$.barArc(cx, cy, r, width, grades, [start = 0] )"
Creates a bar with arc form (section of the circumference) based on a center (cx and cy), a radius (r), and width (width), positive or negative number of grades (grades), and optionally a start angle (in degrees).
Parameters:
cx- center xcy- center yr- radiuswidth- bar widthgrades- grades in degreesstart- start position in degrees (optional, default value0)
:d="$.circleSlice(cx, cy, r, grades, [start = 0] )"
Creates a circle slice (section of the circle) based on a center (cx and cy), a radius (r), positive or negative number of grades (grades), and optionally a start angle (in degrees).
Parameters:
cx- center xcy- center yr- radiusgrades- grades in degreesstart- start position in degrees (optional, default value0)
g-content
The g-content directive has two helpers:
$.elementrefers to the wrapper of the element where theg-contendirective has been included. With this reference it is possible to query attributes of the element such as$.element.x()or$.element.stroke()for conditional content.$.currentContent()returns the content of the element. It can be useful to add elements instead of replacing them or to check the content before modifying it.$.fromURL()gets an external resource via a URL and inserts it as element content.