Canvas Molecule

Add a 2D canvas.

Summary
Canvas Molecule
Preference Dialog
CommentsEach vector graphic shape on canvas is represented by a shape object.
Properties
typestring, ( r )
pref.namestring, ( r )
pref.enableWhenstring, ( r, w )
pref.disableWhenstring, ( r, w )
pref.shapesarray, ( r, w )
pref.flipbool, ( r, w )
pref.visiblebool, ( r, w )
pref.drawBorderbool, ( r, w )
pref.closedbool, ( r, w )
pref.filledbool, ( r, w )
pref.compositestring, ( r, w )
pref.lineWidthint, ( r, w )
pref.lineCapstring, ( r, w )
pref.lineJoinstring, ( r, w )
pref.miterLimitint, ( r, w )
pref.fillTypeint, ( r, w )
pref.gradientTypestring, ( r, w )
pref.brint [0, 255], ( r, w )
pref.bgint [0, 255], ( r, w )
pref.bbint [0, 255], ( r, w )
pref.baint [0, 100], ( r, w )
pref.frint [0, 255], ( r, w )
pref.fgint [0, 255], ( r, w )
pref.fbint [0, 255], ( r, w )
pref.faint [0, 100], ( r, w )
pref.colorStopsarray, ( r, w )
pref.pattern_srcstring, ( r, w )
pref.pattern_repeatstring, ( r, w )
pref.textstring, ( r, w )
pref.fontstring, ( r, w )
pref.textAlignstring, ( r, w )
pref.textBaseLinestring, ( r, w )
pref.strokeTextbool, ( r, w )
pref.toleranceint, ( r, w )
pref.arrowint, ( r, w )
contextobject, ( r )
tracksobject, ( r )
ishapeobject, ( r, w )
Functions
attach
detach
delete
rename
show
hide
refresh
clone
serialize
addShape
addGroup
addShapeToGroup
ungroupShape
selectShape
selectShapeAt
getShape
showShape
zoom
rotateShape
rotateShapeByDegree
scaleShape
scaleShapeCt
importShape
cloneShape
deleteShape
deleteAll
bring2Front
selectPoint
selectControlPoint
selectCenter
updateCenter
calculateCenter
addPoint
addBezierPoint
deletePoint
movePointTo
movePointBy
moveControlPointTo
moveControlPointBy
moveShapeTo
moveShapeBy
convertBezier2Polygon
fitText
fitCurve
preferences
easing
start
stop

Preference Dialog

Comments

Each vector graphic shape on canvas is represented by a shape object.  Direct manipulation of the shape object is possible, if you are familiar with the its structure.

{ id: name,
  type: 'typeName',
  pts:  [ {px: 5, py: 10,
           cp1: {px: 2, py: 15},
           cp2: {px: 2, py: 15}},
           ...],
  pref: {visible: true,
         closed: false,
         drawBorder: true,
         filled: false,
         text: 'hello',
         ...,
         shapes: [...] },
  center: {px: 10, py: 20},
  img: Image };

typeName can be null, arc, or group.  An arc shape includes a circle or arc segment.  A group shape holds all its child shapes in pref.shapes array.  Nested groups are possible such as group of groups.

cp1 and cp2 are the first and second control points which are only present for bezier shapes.

If pref.pattern_src is given, img holds the downloaded Image object, allowing you to query for its width and height.

Note: After making changes, you need to call refresh, or the parent droplet’s droplet.paint function, to update to screen.

Double clicking on the droplet enters you into canvas’s edit mode where you can modify each shape interactively.  While in edit mode, selecting a shape followed by clicking on the gear icon on the right opens up a configure dialog box that allows you to change individual shape’s drawing preference.

The event callback option in the config dialog, if set, will trigger your supplied function during animation playback.  In addition to regular tween variables, the input params contain the following identifiers,

type’update’ or ‘complete’, ‘update’ is sent on each time increment, ‘complete’ is sent on reaching each key frame.
shapethe shape ID.
trackthe track name.
keyframethe index of current key frame (0-based.)

Optionally, you can assign a category name to each shape and later change its drawing preferences by category (see preferences).

Properties

type

string, ( r )

Type of this molecule, canvas.

pref.name

string, ( r )

The scriptable name for this molecule.  This name can be set using rename function dynamically.

pref.enableWhen

string, ( r, w )

Specifies the event that activates this molecule.  Default is load.

pref.disableWhen

string, ( r, w )

Specifies the event that deactivates this molecule.  Default is none.

pref.shapes

array, ( r, w )

An array of shapes.  Each shape object has its own drawing pref.  For example, this.pref.shapes[1].pref.closed = true; this.pref.shapes[0].pref.lineWidth=5.0;

pref.flip

bool, ( r, w )

Specifies if the shape is flipped horizontally or vertically.  0: none, 1: horizontal, 2: vertical, 3: horizontal and vertical.

pref.visible

bool, ( r, w )

Specifies the visibility of a shape.

pref.drawBorder

bool, ( r, w )

Specifies the visibility of a shape’s border.

pref.closed

bool, ( r, w )

Specifies whether the shape’s border is open or closed.

pref.filled

bool, ( r, w )

Specifies whether the shape is hollow or filled.

pref.composite

string, ( r, w )

Specifies the style of composition between overlapping shapes.

source-overDefault.  Display the source image wherever the source image is opaque.  Display the destination image elsewhere.
destination-overNew shapes are drawn behind the existing canvas content.
source-inThe new shape is drawn only where both the new shape and the destination canvas overlap.  Everything else is made transparent.
destination-inThe existing canvas content is kept where both the new shape and existing canvas content overlap.  Everything else is made transparent.
source-outThe new shape is drawn where it doesn’t overlap the existing canvas content.
destination-outThe existing content is kept where it doesn’t overlap the new shape.
source-atopThe new shape is only drawn where it overlaps the existing canvas content.
destination-atopThe existing canvas is only kept where it overlaps the new shape.  The new shape is drawn behind the canvas content.
lighterDisplay the sum of the source image and destination image, with color values approaching 1 as a limit.
darkerWhere both shapes overlap the color is determined by subtracting color values.
xorExclusive OR of the source and destination images.  Works only with black and white images and is not recommended for color images.
copyOnly draws the new shape and removes everything else.

pref.lineWidth

int, ( r, w )

Specifies the border width of a shape.

pref.lineCap

string, ( r, w )

Specifies the style of line cap.

buttDefault.  A flat edge that is perpendicular to the line itself.
roundRound endpoints.
squareSquare endpoints.

pref.lineJoin

string, ( r, w )

Specifies the style of line join.

miterDefault.  Miter joins.
roundRound joins.
bevelSquare joins.

pref.miterLimit

int, ( r, w )

Specifies the vaule of miter limit.

pref.fillType

int, ( r, w )

Specifies the style of fill.

0Default. fill with solid color.  (see pref.fr, pref.fg, pref.fb.)
1fill with color gradient.  (see pref.colorStops.)
2fill with pattern.  (see pref.pattern_src.)
3fill with text.  (see pref.text.)

pref.gradientType

string, ( r, w )

Specifies the style of gradient.  linear or radial.

pref.br

int [0, 255], ( r, w )

Specifies the RED component of the border color.

pref.bg

int [0, 255], ( r, w )

Specifies the GREEN component of the border color.

pref.bb

int [0, 255], ( r, w )

Specifies the BLUE component of the border color.

pref.ba

int [0, 100], ( r, w )

Specifies the ALPHA component of the border color.

pref.fr

int [0, 255], ( r, w )

Specifies the RED component of the fill color.

pref.fg

int [0, 255], ( r, w )

Specifies the GREEN component of the fill color.

pref.fb

int [0, 255], ( r, w )

Specifies the BLUE component of the fill color.

pref.fa

int [0, 100], ( r, w )

Specifies the ALPHA component of the fill color.

pref.colorStops

array, ( r, w )

An array of color stops.  Each color stop is a string of the following format, rgba(rv, gv, bv, av) offset.

rv(int [0, 255]) red component.
gv(int [0, 255]) green component.
bv(int [0, 255]) blue component.
av(int [0, 255]) alpha component.
offset(float [0, 1]) color stop offset.

pref.pattern_src

string, ( r, w )

Specifies the URL of a fill pattern image.  This can be a valid http URL or a private vault URL starting with //.

pref.pattern_repeat

string, ( r, w )

Specifies the repeat of the fill pattern.

repeatDefault.  The image will be repeated both vertically and horizontally.
repeat-xThe image will be repeated only horizontally
repeat-yThe image will be repeated only vertically
no-repeatThe image will not be repeated.

pref.text

string, ( r, w )

Specifies the fill text.

pref.font

string, ( r, w )

Specifies the font of fill text.  For example: 12px sans-serif.

pref.textAlign

string, ( r, w )

Specifies the alignment of fill text.

leftThe text is left-aligned.
rightThe text is right-aligned.
centerThe text is centered.
startDefault.  The text is aligned at the normal start of the line (left-aligned for left-to-right locales, right-aligned for right-to-left locales).
endThe text is aligned at the normal end of the line (right-aligned for left-to-right locales, left-aligned for right-to-left locales).

pref.textBaseLine

string, ( r, w )

Specifies the current baseline for drawing text.

topThe text baseline is the top of the em square.
hangingThe text baseline is the hanging baseline.  Currently unsupported; this will act like alphabetic.
middleThe text baseline is the middle of the em square.
alphabeticDefault.  The text baseline is the normal alphabetic baseline.
ideographicCurrently unsupported; this will act like alphabetic.
bottomThe text baseline is the bottom of the bounding box.  This differs from the ideographic baseline in that the ideographic baseline doesn’t consider descenders.

pref.strokeText

bool, ( r, w )

Specifies whether the text is filled or stoked with hollow center.

pref.tolerance

int, ( r, w )

Specifies the tolerance for the automatic curve fitting algorithm.  The lower the tolerance, the closer a generated curve is to a free hand drawing.

pref.arrow

int, ( r, w )

Specifies the type of arrow head added to a curve or polyline.

0Default.  None.
1to.
2from.
3to and from.

context

object, ( r )

Returns the internal 2d canvas context.

tracks

object, ( r )

Returns the array of animation tracks.  Each track is indexed by its shape ID and track name and support the following methods,

playplay(bool repeat, bool mirror, int repeatCount) or play(bool repeat, int repeatCount), start the animation track.
stopstop animation track.
resetrestore track to initial state.
delaydelay(int msec), add an initial delay in msec.
isPlayingcheck if track is currently playing.

and the following attributes,

keysaccessor to the array of keyframes.
easingspecify the easing function.  (e.g.  EASING.Exponential.InOut.)
easingOption0 for on every keyframe or 1 for only the first and last key frames.

ishape

object, ( r, w )

The accessor to the currently selected shape.  (see selectShape.)

Functions

attach

attach(droplet);

Attach this molecule to the given droplet.

Parameters

droplet(object) a droplet.

Returns

N/A

detach

detach();

Remove this molecule from its droplet.

Parameters

N/A

Returns

N/A

delete

delete();

Delete this molecule.

Parameters

N/A

Returns

N/A

rename

rename(name);

Rename this molecule.

Parameters

name(string) a new name.

Returns

N/A

show

show();

Shows the effects of this molecule.

Parameters

N/A

Returns

N/A

hide

hide();

Hides the effects of this molecule.

Parameters

N/A

Returns

N/A

refresh

refresh();

Forces molecule to redraw with the current set of preferences.

Parameters

N/A

Returns

N/A

clone

clone();

Create a clone of this molecule.

Parameters

N/A

Returns

(object) a molecule.

serialize

serialize();

Serialize this molecule into its JSON representation.

Parameters

N/A

Returns

(string) JSON representation of a molecule.

addShape

addShape(option);

If option is a string, Create and add an empty shape with given name to canvas.  If option is a shape object, add that shape to canvas.

Parameters

option(mixed) a name or an existing shape.

Returns

(object) a shape.

addGroup

addGroup(name);

Creates a group with the given name.  A group is a special kind of shape object with its type sets to ‘group’.  A group is used to hold multiple shapes.  Every drawing/transformation operation applied to a group is applied to every shape within that group.

Parameters

name(string) a group name.

Returns

(object) a group.

addShapeToGroup

addShapeToGroup(shape, group);

Add shape into the currently selected group.  If group is not null, shape is added to group instead.

Parameters

shape(object) a shape object.
group(object) Optional. a group object.

Returns

N/A

ungroupShape

ungroupShape();

Separate currently selected group into individual shapes.

Parameters

N/A

Returns

N/A

selectShape

selectShape(name);

Select a shape by name.  The selected shape is stored in ishape.

Parameters

name(string) a shape name.

Returns

(object) a shape.

selectShapeAt

selectShapeAt( x, y, array )

Select a shape by location in relation to the upper left corner of the droplet.  If array is given, it searches within the given array.

Parameters

x(int) x-coordinate.
y(int) y-coordinate.
array(array) Optional. an array of shapes.

Returns

(object) a shape.

getShape

getShape( name, array )

Returns a shape with the matching name without selecting it.  If array is given, this function searches within that array.

Parameters

name(string) a shape name.
array(array) Optional. an array of shapes.

Returns

(object) a shape.

showShape

showShape( opt )

Show/Hide a selected shape.  A shape must be selected first.

Parameters

opt(bool)

Returns

N/A

zoom

zoom( scale, x, y, array )

Zoom in/out on the given array of shapes at location (x, y).  If array is null, apply to all shapes in canvas.

Parameters

scale(float) zoom factor.
x(int) x-coordinate.
y(int) y-coordinate.
array(array) Optional. an array of shapes.

Returns

N/A

rotateShape

rotateShape( rad, shape, x, y )

Rotate the currently selected shape by rad radian.  If shape is given, it rotates the given shape.  If x and y are given, it rotate using (x, y) as the pivot point.

Parameters

rad(float) angle in radian.
shape(object) Optional. a shape object.
x(int) Optional. x-coordinate.
y(int) Optional. y-coordinate.

Returns

N/A

rotateShapeByDegree

rotateShape( deg, shape, x, y )

Rotate the currently selected shape by deg degree.  If shape is given, it rotates the given shape.  If x and y are given, (x, y) is used as the pivot point.

Parameters

deg(float) angle in degree.
shape(object) Optional. a shape object.
x(int) Optional. x-coordinate.
y(int) Optional. y-coordinate.

Returns

N/A

scaleShape

scaleShape( rw, rh, point, shape )

Scales the currently selected shape using the given ratio (rw, rh) for width and height and point as the scaling center.  If shape is given, scaling is applied to the input shape.

Parameters

rw(float) scaling factor in width.
rh(float) scaling factor in height.
point(object) the scaling center in {‘px’: x, ‘py’: y} format.
shape(object) Optional. a shape object.

Returns

N/A

scaleShapeCt

scaleShapeCt( rw, rh, shape )

Scales the currently selected shape using the given ratio (rw, rh) for width and height and the shape’s center point as the scaling center.  If shape is given, scaling is applied to the input shape.

Parameters

rw(float) scaling factor in width.
rh(float) scaling factor in height.
shape(object) Optional. a shape object.

Returns

N/A

importShape

importShape( shape )

Import the given shape.  Shapes can be transported across canvases this way.

Parameters

shape(object) Optional. a shape object.

Returns

(object) the imported shape object.

cloneShape

cloneShape( shape, copyName )

Clone the given shape.  If copyName is true, the new shape takes on the same Name as shape.  Otherwise, a name is automatically assigned to the new shape.  This call can be used to clone groups as well.

Parameters

shape(object) a shape object.
copyName(bool) Optional.

Returns

(object) a shape object.

deleteShape

deleteShape( option )

If option is string, delete shape with name option.  If option is a shape object, delete the input shape option.

Parameters

option(mixed) a name or a shape object.

Returns

N/A

deleteAll

deleteAll()

Delete all shapes in canvas.

Parameters

N/A

Returns

N/A

bring2Front

bring2Front(name)

Brings shape with the given name to front.

Parameters

name(string) a shape name.

Returns

N/A

selectPoint

selectPoint( x, y )

Select a point at the given location.  The shape that the point belongs to is also selected.

Parameters

x(int) x-coordinate.
y(int) y-coordinate.

Returns

(int) the index of a selected point on the currently selected shape, otherwise -1.

selectControlPoint

selectControlPoint( x, y )

Select a Bezier control point at the given location.  The Bezier shape that the point belongs to is also selected.

Parameters

x(int) x-coordinate.
y(int) y-coordinate.

Returns

(int) the index of a selected control point on the currently selected bezier shape, otherwise -1.

selectCenter

selectCenter( x, y )

Test if the center point of the currently selected shape is at the given location.

Parameters

x(int) x-coordinate.
y(int) y-coordinate.

Returns

(bool) true if the center point of the currently selected shape is selected, otherwise false.

updateCenter

updateCenter()

Move the center point of the currently selected shape to its geometric center.

Parameters

N/A

Returns

N/A

calculateCenter

calculateCenter(shape)

Calculates and returns the geometrical center for the given shape shape.

Parameters

shape(object) a shape object.

Returns

(object)the center point, {x: 0.0, y: 0.0}.

addPoint

addPoint(x, y)

Add a point to the end of the currently selected shape’s point array.  (e.g. this.ishape.pts)

Parameters

x(int) x-coordinate.
y(int) y-coordinate.

Returns

N/A

addBezierPoint

addBezierPoint( cp1x, cp1y, cp2x, cp2y, x, y )

Add a bezier point to the end of the currently selected shape’s point array.  Each bezier point is made of two control points, (cp1x, cp1y) and (cp2x, cp2y), and a point on the curve, (x, y).

Parameters

cp1x(int) x-coordinate of first control point.
cp1y(int) y-coordinate of first control point.
cp2x(int) x-coordinate of second control point.
cp2y(int) y-coordinate of second control point.
x(int) x-coordinate.
y(int) y-coordinate.

Returns

N/A

deletePoint

deletePoint(index)

Delete the point at the given index from the currently selected shape.

Parameters

index(int) index into a shape’s internal point array.  (e.g. shape.pts.)

Returns

N/A

movePointTo

movePointTo( index, x, y )

Move a point at the given index of the currently selected shape to a new location at (x, y).

Parameters

index(int) index into a shape’s internal point array.  (e.g. shape.pts.)
x(int) x-coordinate.
y(int) y-coordinate.

Returns

N/A

movePointBy

movePointBy( index, x, y )

Move a point at the given index of the currently selected shape by (x, y).

Parameters

index(int) index into a shape’s internal point array.  (e.g. shape.pts.)
x(int) x-coordinate.
y(int) y-coordinate.

Returns

N/A

moveControlPointTo

moveControlPointTo( index, option, x, y )

Move either the first(option=1) or second(option=2) control point at the given index from the currently selected shape to (x, y).

Parameters

index(int) index into a shape’s internal point array.  (e.g. shape.pts.)
option(int) specifies which control point.
x(int) x-coordinate.
y(int) y-coordinate.

Returns

N/A

moveControlPointBy

moveControlPointBy( index, option, x, y )

Move either the first(option=1) or second(option=2) control point at the given index from the currently selected shape by (x, y).

Parameters

index(int) index into a shape’s internal point array.  (e.g. shape.pts.)
option(int) specifies which control point.
x(int) x-coordinate.
y(int) y-coordinate.

Returns

N/A

moveShapeTo

moveShapeTo( x, y )

Move the currently selected shape to (x, y).

Parameters

x(int) x-coordinate.
y(int) y-coordinate.

Returns

N/A

moveShapeBy

moveShapeBy( x, y )

Move the currently selected shape by (x, y).

Parameters

x(int) x-coordinate.
y(int) y-coordinate.

Returns

N/A

convertBezier2Polygon

convertBezier2Polygon( points )

Convert the given point array, points, to a pure polygonal point array.  All control points are removed.

Parameters

points(array) a point array.

Returns

(array) a new point array representing a polygon.

fitText

fitText( shape, padding )

Fits shape to its fill text pattern with an optional padding.  The shape parameter can be a shape name or shape object.  If shape is omitted, the currently selected shape is used.

Parameters

shape(mixed) Optional. a shape name or a shape object.
padding(int) padding in pixels.

Returns

N/A

fitCurve

fitCurve( points )

Applies curve fitting algorithm on the input point array and returns the result as a new shape.

Parameters

points(array) a point array, [{px:2,py:2},{px:5,py:3},...].

Returns

(object) a shape object.

preferences

preferences( shape, category, pref )

Specify drawing preferences by category name.  Shapes of given category are updated.

Parameters

shape(string) name of a shape or * for wildcard.
category(string) category name.
pref(object) subset of drawing preferences.

Returns

N/A

easing

easing( shape, track, type, opt )

Specify easing function for a subset of animation tracks.

Parameters

shape(string) name of a shape or * for wildcard.
track(string) track name.
type(object) type of easing function (e.g.  EASING.Bounce.InOut.)
opt(number) 0 for each keyframe, 1 for the first and last keys only.

Returns

N/A

start

start( shape, track, repeat, mirror, repeatCount )

Starts a subset of animation tracks.

Parameters

shape(string) name of a shape or * for wildcard.
track(string) track name.
repeat(bool) repeat the animation.
mirror(bool) alternate between forward and reversed animations.
repeatCount(number) repeat count.

Returns

N/A

stop

stop( shape, track )

Stops a subset of animation tracks.

Parameters

shape(string) name of a shape or * for wildcard.
track(string) track name.

Returns

N/A

string, ( r, w )
paint: function()
int [0, 255], ( r, w )
int [0, 255], ( r, w )
int [0, 255], ( r, w )
array, ( r, w )
string, ( r, w )
object, ( r, w )
Close