MISCELLANEOUS TECHNICAL ARTICLES BY Dr A R COLLINS

Cango Arrows User Guide

CangoArrows module

The CangoArrows module adds Extension Objects and methods for use with the Cango library to simplify drawing arrows either straight or arced. The module requires the Cango canvas graphics library Version 26 or later. Download the source code from CangoArrows-1v00.js and the minified version at CangoArrows-1v00-min.js

The CangoArrows module supplies the following objects.

Arrow
ArrowArc

In addition the following methods are added to the Cango library to simplify drawing single instances of the objects:

drawArrow
drawArrowArc

Getting Started

If the module is loaded as a JavaScript file it should be loaded after the Cango canvas graphics library (Version 26 or later). Save th "CangoArrows-1v00.js file to your server, then add the following lines to the web page header:

<script src="[directory path/]Cango-26v14-min.js"></script>
<script src="[directory path/]CangoArrows-1v00-min.js"></script>

Global Extension Objects added

Arrow

Syntax:

const obj = new Arrow(baseX, baseY, tipX, tipY [, options]);

Description:

Creates a straight arrow shape. The arrow starting at world coordinates (baseX,baseY) and the tip of the arrow head will end at (tipX, tipY). The arrow shaft width may be set in either pixels or world coordinates and head size can be adjusted by specifying a head size scale factor relative to the shaft width.

The arrow can be drawn with different colored border, a drop shadow or any of the other properties supported by Cango 'Shape' objects.

NOTE: The arrow shape is always drawn with the 'iso' property set true to preserve the arrow shape when independent X and Y world coordinate scaling is used.

Parameters:

baseX, baseY: Numbers - X and Y coordinates of the base of the arrow shaft. Measured in the world coordinates.

tipX, tipY: Numbers - X and Y coordinates of the tip of the arrow head. Measured in the world coordinates.

options: Object - The values for the various optional properties may be passed as key-value pairs in this object (see Options Properties below).

Arrow Optional Properties:

Property NameTypeDescription
shaftWidthNumber
(pixels)
Width of the arrow shaft.
shaftWidthWCNumber
(world coordinates)
Width of the arrow shaft.
'shaftWidthWC' takes precedence over 'shaftWidth' property.
headSizeNumberSize of the head relative to the shaft width (default = 4).
Length of head is approximately headSize*shaftWidth.

Inherited optional style properties

(see Cango User Guide Object Optional Style Properties for details).

  • border
  • fillColor
  • strokeColor
  • lineWidthWC
  • lineWidth
  • dashed
  • dashOffset
  • shadowOffsetX
  • shadowOffsetY
  • shadowBlur
  • shadowColor

Inherited Extension Object optional transform properties

(see Cango User Guide Object Optional Transform Properties for details).

  • rot
  • scl, sclX, sclY
  • skewX, skewY

Example:

function drawArrows(cvsID)
{
  var gc = new Cango(cvsID),
      xmin = -15, xmax = 25,
      ymin = 0, ymax = 4;

  gc.gridboxPadding(10, 8, 2, 8);
  gc.setWorldCoordsRHC(-15, 0, 40, 4);
  gc.drawAxes(xmin, xmax, ymin, ymax, {
    xOrigin: 0, 
    yOrigin: 0, 
    strokeColor:'gray'});

  // test straight arrow
  gc.drawArrow(5, 1, 20, 3, {
    shaftWidthWC:2,
    headSize:4,
    fillColor:'red',
    border:true,
    strokeColor:'blue',
    lineWidth:2});

  gc.drawArrow(0, 1, -10, 4, {
    shaftWidth: 8,
    fillColor:'green'});
}

Figure 5. Simple example of 'drawArrow' method.

ArrowArc

Syntax:

const obj = new ArrowArc(radius, startAngle, stopAngle [, options]);

Description:

Draws a curved arrow whose shape is a circular arc of radius 'radius' starting at the angle 'startAngle' from the X axis and finishing at 'stopAngle'. An arrow head is drawn at the end with its tip at radius 'radius' and angle 'stopAngle'. The drawing origin will be at the center of the circular arc. If options properties 'x' and/or 'y' are specified then the center of the arc will be drawn at world coordinates x,y otherwise the center will be drawn at world coordinates 0,0.

NOTE: The arrow shape is always drawn with the 'iso' property set true to preserve the arrow shape when independent X and Y world coordinate scaling is used.

Parameters:

radius: Number - The radius of the circular arc formed by the arrow shaft. Measured in the world coordinates.

startAngle: Number - The angle at which the arrow arc starts . Measured in degrees from X axis.

stopAngle: Number - The angle at which the arrow arc stops. Measured in degrees from X axis.

options: Object - The values for the various optional properties may be passed as key-value pairs in this object (see Options Properties below).

Optional Properties:

Property NameTypeDescription
shaftWidthNumber
(pixels)
Width of the arrow shaft.
shaftWidthWCNumber
(world coordinates)
Width of the arrow shaft, this value takes precedence over shaft width set in pixels by 'shaftWidth' property.
headSizeNumberRelative size of the head (default = 4).
Length of head is approximately headSize*shaftWidth.
clockwiseBooleanIf true the arrow sweeps clockwise from startAngle to stopAngle, if false it sweeps counter-clockwise

Inherited optional style properties

(see Cango User Guide Object Optional Style Properties for details).

  • border
  • fillColor
  • strokeColor
  • lineWidthWC
  • lineWidth
  • dashed
  • dashOffset
  • shadowOffsetX
  • shadowOffsetY
  • shadowBlur
  • shadowColor

Inherited Extension Object optional transform properties

(see Cango User Guide Object Optional Transform Properties for details).

  • x, y
  • rot
  • scl, sclX, sclY
  • skewX, skewY

Example:

  function arrowArcDemo(cvsID)
  {
    const cgo = new Cango(cvsID);
          
    cgo.clearCanvas("beige");
    cgo.gridboxPadding(5);

    cgo.setWorldCoordsRHC(-20, -4, 40, 8);
    cgo.drawAxes(-20, 20, -4, 4, {
      xOrigin: 0, yOrigin: 0,
      xLabel:"X", 
      yLabel:"Y",
      fontSize: 10 });

    cgo.drawArrowArc(8, 140, 280, {
      x:-10,
      y:-2,
      clockwise:true,
      shaftWidth:4,
      fillColor:'black'});
    cgo.drawPath(PathSVG.cross(2, 0.5), {
      x:-10, y:-2});
    cgo.drawArrowArc(10, 0, 40, {
      clockwise:false,
      shaftWidth:8,
      fillColor:'green',
      shadowOffsetX:0.50,
      shadowOffsetY:-0.1,
      shadowBlur:0.5,
      shadowColor:'gray'});
    cgo.drawArrowArc(15, 20, 156, {
      clockwise: false,
      shaftWidthWC: 1.7,
      fillColor: 'steelblue',
      border:true,
      strokeColor:'red'});
  }
  

Figure 6. Simple example of 'drawArrow' method.

Arrow and ArrowArc common methods

Arrow and ArrowArc are Cango Extension Object as so they are sub-classes of the Group object and inherit the following set of Group handling methods which apply the corresponding object method to the Group children recursively.


Cango methods added

drawArrow

Syntax:

cgo.drawArrow(baseX, baseY, tipX, tipY [, options]);

Description:

A wrapper function to simplify drawing a single instance of an arrow. The drawArrow method creates an Arrow object passing the arguments unchanged to the constructor and then renders it to the canvas.

Parameters:

baseX, baseY: Numbers - X and Y coordinates of the base of the arrow shaft. Measured in the world coordinates.

tipX, tipY: Numbers - X and Y coordinates of the tip of the arrow head. Measured in the world coordinates.

options: Object - The values for the various optional properties may be passed as key-value pairs in this object (see Options Properties below).

drawArrowArc

Syntax:

cgo.drawArrowArc(radius, startAngle, stopAngle, options);

Description:

A wrapper function to simplify drawing a single instance of an ArrowArc. The drawArrowArc method creates an ArrowArc object passing the arguments unchanged to the constructor and then renders it to the canvas.

Parameters:

radius: Number - The radius of the circular arc formed by the arrow shaft. Measured in the world coordinates.

startAngle: Number - The angle at which the arrow arc starts . Measured in degrees from X axis.

stopAngle: Number - The angle at which the arrow arc stops. Measured in degrees from X axis.

options: Object - The values for the various optional properties may be passed as key-value pairs in this object (see ArrowArc properties).