1.22.0 • Published 3 months ago

plotboilerplate v1.22.0

Weekly downloads
112
License
MIT
Repository
github
Last release
3 months ago

License Release NPM

An interactive Javascript Plotting Boilerplate

For plotting visual 2D data with Javascript on HTML canvas (in 2d-context) or SVG nodes.

logo

This is a simple collection of useful functions, methods, classes, algorithms and concepts which I often use for the visualization of 2D geometries. Basic features are

The compressed library has 135kb.

Install the package via npm

   # Installs the package
   $ npm i plotboilerplate

The HTML file

For a full example see main-dist.html :

<canvas id="my-canvas"> Your browser does not support the canvas tag. </canvas>

The element canvas will be used to draw on.

The javascript

var pb = new PlotBoilerplate({
  canvas: document.getElementById("my-canvas"),
  fullSize: true
});

Alternative with SVG elements

Use SVG elements instead of canvas:

<svg id="my-svg"></svg>

And pass the SVG element:

var pb = new PlotBoilerplate({
  canvas: document.getElementById("my-svg"),
  fullSize: true
});

Add elements to your canvas

// Create two points:
//   The origin is at the visual center by default.
var pointA = new Vertex(-100, -100);
var pointB = new Vertex(100, 100);
pb.add(new Line(pointA, pointB));

// When point A is moved by the user
//   then move point B in the opposite direction
pointA.listeners.addDragListener(function (e) {
  pointB.sub(e.params.dragAmount);
  pb.redraw();
});

// and when point B is moved
//   then move point A
pointB.listeners.addDragListener(function (e) {
  pointA.sub(e.params.dragAmount);
  pb.redraw();
});

Typescript

// Usage with Typescript could look like this
import { PlotBoilerplate, Vertex, Line } from "plotboilerplate";

globalThis.addEventListener("load", () => {
  const pointA: Vertex = new Vertex(100, -100);
  const pointB: Vertex = new Vertex(-100, 100);
  console.log(pointA, pointB);

  const line: Line = new Line(pointA, pointB);

  const pb: PlotBoilerplate = new PlotBoilerplate({
    canvas: document.getElementById("my-canvas"),
    fullSize: true
  });

  pb.add(line);
});

For a guide of how to Getting Started click here.

A full working demo repository about the Usage with Typescript is here.

Codepen Demos

Basic Setup in a Codepen.io demo

Screenshot

Simple Demo

And the simple demo is here

API

See API Documentation for details.

Screenshot

Current demo

See the demo

Examples and Demos

Examples and Demos

Initialization parameters

NameTypeDefaultDescription
canvasHTMLCanvasElement | SVGElement | stringnullThe canvas or its query selector string (required).
fullsizebooleantrueIf true, then the canvas will always claim tha max available screen size.
fitToParentbooleantrueIf true, then the canvas will alway claim the max available parent container size.
scaleXnumber1.0The initial horizontal zoom. Default is 1.0.
scaleYnumber1.0The initial vertical zoom. Default is 1.0.
offsetXnumber0.0The initial offset. Default is 0.0. Note that autoAdjustOffset=true overrides these values.
offsetYnumber0.0The initial offset. Default is 0.0. Note that autoAdjustOffset=true overrides these values.
drawGridbooleantrueSpecifies if the raster should be drawn.
rasterScaleXnumber1.0Define the default horizontal raster scale.
rasterScaleYnumber1.0Define the default vertical raster scale.
rasterGridbooleantrueIf set to true the background grid will be drawn rastered.
rasterAdjustFactornumber2.0The exponential limit for wrapping down the grid. (2.0 means: halve the grid each 2.0*n zoom step).
drawOriginbooleanfalseDraw a crosshair at (0,0).
autoAdjustOffsetbooleantrueWhen set to true then the origin of the XY plane will be re-adjusted automatically (see the params offsetAdjust{X,Y}Percent for more).
offsetAdjustXPercentnumber50The x- and y- fallback position for the origin after resizing the canvas.
offsetAdjustYPercentnumber50The x- and y- fallback position for the origin after resizing the canvas.
defaultCanvasWidthnumber1024The canvas size fallback if no automatic resizing is switched on.
defaultCanvasHeightnumber768The canvas size fallback if no automatic resizing is switched on.
canvasWidthFactornumber1.0Two scaling factors (width and height) upon the canvas size. In combination with cssScale{X,Y} this can be used to obtain sub pixel resolutions for retina displays.
canvasHeightFactornumber1.0Two scaling factors (width and height) upon the canvas size. In combination with cssScale{X,Y} this can be used to obtain sub pixel resolutions for retina displays.
cssScaleXnumber1.0Visually resize the canvas using CSS transforms (scale x).
cssScaleYnumber1.0Visually resize the canvas using CSS transforms (scale y).
cssUniformScaleboolean1.0If set to true only cssScaleX applies for both dimensions.
autoDetectRetinabooleantrueWhen set to true (default) the canvas will try to use the display's pixel ratio.
backgroundColorstring#ffffffA background color (CSS string) for the canvas.
redrawOnResizebooleantrueSwitch auto-redrawing on resize on/off (some applications might want to prevent automatic redrawing to avoid data loss from the drae buffer).
drawBezierHandleLinesbooleantrueIndicates if Bézier curve handle points should be drawn.
drawBezierHandlePointsbooleantrueIndicates if Bézier curve handle points should be drawn.
preClearfunctionnullA callback function that will be triggered just before the draw function clears the canvas (before anything else was drawn).
preDrawfunctionnullA callback function that will be triggered just before the draw function starts.
postDrawfunctionnullA callback function that will be triggered right after the drawing process finished.
enableMousebooleantrueIndicates if the application should handle touch events for you.
enableTouchbooleantrueIndicates if the application should handle touch events for you.
enableKeysbooleantrueIndicates if the application should handle key events for you.
enableMouseWheelbooleantrueIndicates if the application should handle mouse wheelevents for you.
enableSVGExportbooleantrueIndicates if the SVG export should be enabled (default is true).
enableGLbooleanfalseExperimental Indicates if the application should use the experimental WebGL features.

Example

var pb = new PlotBoilerplate({
  // HTMLCanvasElement | SVGElement | string
  //   Your canvas element in the DOM (required).
  canvas: document.getElementById("my-canvas"),

  // boolean
  //   If set to true the canvas will gain full window size.
  fullSize: true,

  // boolean
  //   If set to true the canvas will gain the size of its parent
  //   container.
  // @overrides fullSize
  fitToParent: true,

  // float
  //   The initial zoom. Default is 1.0.
  scaleX: 1.0,
  scaleY: 1.0,

  // float
  //   The initial offset. Default is 0.0. Note that autoAdjustOffset=true overrides these values.
  offsetX: 0.0,
  offsetY: 0.0,

  // Specifies if the raster should be drawn.
  drawGrid: true,

  // If set to true the background grid will be drawn rastered.
  rasterGrid: true,

  // float
  //    The exponential limit for wrapping down the grid.
  //    (2.0 means: halve the grid each 2.0*n zoom step).
  rasterAdjustFactor: 2.0,

  // Draw a crosshair at (0,0).
  drawOrigin: false,

  // boolean
  //   When set to true then the origin of the XY plane will
  //   be re-adjusted automatically (see the params
  //    offsetAdjust{X,Y}Percent for more).
  autoAdjustOffset: true,
  // float
  //   The x- and y- fallback position for the origin after
  //   resizing the canvas.
  offsetAdjustXPercent: 50,
  offsetAdjustYPercent: 50,

  // int
  //   The canvas size fallback if no automatic resizing
  //   is switched on.
  defaultCanvasWidth: 1024,
  defaultCanvasHeight: 768,

  // float
  //   Two scaling factors (width and height) upon the canvas size.
  //   In combination with cssScale{X,Y} this can be used to obtain
  //   sub pixel resolutions for retina displays.
  canvasWidthFactor: 1.0,
  canvasHeightFactor: 1.0,

  // float
  //   Visually resize the canvas using CSS transforms (scale).
  cssScaleX: 1.0,
  cssScaleY: 1.0,

  // boolean
  //   If set to true only cssScaleX applies for both dimensions.
  cssUniformScale: true,

  // boolean
  //   When set to true (default) the canvas will try to use the display's pixel ratio.
  autoDetectRetina: true,

  // string
  //   A background color (CSS string) for the canvas.
  backgroundColor: "#ffffff",

  // boolean
  //   Switch auto-redrawing on resize on/off (some applications
  //   might want to prevent automatic redrawing to avoid data
  //   loss from the drae buffer).
  redrawOnResize: true,

  // boolean
  //   Indicates if Bézier curve handles should be drawn (used for
  //   editors, no required in pure visualizations).
  drawBezierHandleLines: true,

  // boolean
  //   Indicates if Bézier curve handle points should be drawn.
  drawBezierHandlePoints: true,

  // function
  //   A callback function that will be triggered just before the
  //   draw function clears the canvas (before anything else was drawn).
  preClear: function () {
    console.log("before clearing the canvas on redraw.");
  },

  // function
  //   A callback function that will be triggered just before the
  //   draw function starts.
  preDraw: function (draw, fill) {
    console.log("after clearing and before drawing.");
  },

  // function
  //   A callback function that will be triggered right after the
  //   drawing process finished.
  postDraw: function (draw, fill) {
    console.log("after drawing.");
  },

  // boolean
  //   Indicates if the application should handle mouse events for you.

  enableMouse: true,
  // boolean
  //   Indicates if the application should handle touch events for you.
  enableTouch: true,

  // boolean
  //   Indicates if the application should handle key events for you.
  enableKeys: true,

  // boolean
  //   Indicates if the application should handle mouse wheelevents for you.
  enableMouseWheel: true,

  // boolean (default true)
  //    Use this to disable panning completely.
  enablePan: true,

  // boolean (default true)
  //    Use this to disable zooming completely.
  enableZoom: true,

  // Indicates if the SVG export should be enabled (default is true).
  enableSVGExport: true,

  // boolean
  //   Indicates if the application should use the experimental WebGL features.
  enableGL: false
});

Events

The Vertex class has basic drag event support:

var vert = new Vertex(100, 100);
vert.listeners.addDragListener(function (e) {
  // e is of type Event.
  // You are encouraged to use the values in the object e.params
  console.log("vertex was dragged by: ", "x=" + e.params.dragAmount.x, "y=" + e.params.dragAmount.y);
});

The e.params object

 {
  // The canvas that fired the event.
  element : [HTMLElement],

  // The event name.
  //   Default: 'drag'
  name : string,

  // The current drag position.
  pos : { x : number, y : number },

  // A mouse button indicator (if mouse event).
  //    0=left, 1=middle, 2=right
  button : number,

  // A flag indicating if event comes from left mouse button.
  leftButton : boolean,

  // A flag indicating if event comes from middle mouse button.
  middleButton : boolean,

  // A flag indicating if event comes from right mouse button.
  rightButton : boolean,

  // A mouse-down-position: position where the dragging
  //   started. This will not change during one drag process.
  mouseDownPos : { x : number, y : number },

  // The most recent drag position (position before
  //   current drag step).
  draggedFrom : { x : number, y : number },

  // True if this is a drag event (nothing else available the moment).
  wasDragged : boolean,

  // The x-y-amount of the current drag step.
  //   This is the difference between the recent drag step
  //   and the actual drag position.
  dragAmount : { x : number, y : number }
 }
NameTypeExample valueDescription
elementHTMLCanvasElement[HTMLCanvasElement]The canvas that fired the event.
namestringdragThe event name (default is 'drag').
posposition{ x : 20, y : 50 }The current drag position.
buttonnumber0A mouse button indicator (if mouse event). 0=left, 1=middle, 2=right
leftButtonbooleantrueA flag indicating if event comes from left mouse button.
middleButtonbooleanfalseA flag indicating if event comes from middle mouse button.
rightButtonbooleanfalseA flag indicating if event comes from right mouse button.
mouseDownPosposition{ x : 0, y : 20 }A mouse-down-position: position where the dragging started. This will not change during one drag process.
draggedFromposition{ x : 10, y : -5 }The most recent drag position (position before current drag step).
wasDraggedbooleantrueTrue if this is a drag event (nothing else available at the moment).
dragAmountposition{ x : 100, y : 34 }The x-y-amount of the current drag step. This is the difference between the recent drag step and the actual drag position.

Mouse, Keyboard and Touch interaction

Custom keyboard events

new KeyHandler({ trackAll: true })
  .down("enter", function () {
    console.log("ENTER was hit.");
  })
  .press("enter", function () {
    console.log("ENTER was pressed.");
  })
  .up("enter", function () {
    console.log("ENTER was released.");
  })
  .down("e", function () {
    console.log("e was hit. shift is pressed?", keyHandler.isDown("shift"));
  })
  .up("spacebar", function () {
    console.log("spacebar was released.");
  });

For a list of all supported key codes see Full list of supported key codes.

Custom mouse event

new MouseHandler(document.getElementById("mycanvas"))
  .drag(function (e) {
    console.log("Mouse dragged: " + JSON.stringify(e));
    if (e.params.leftMouse);
    else if (e.params.rightMouse);
  })
  .move(function (e) {
    console.log("Mouse moved: " + JSON.stringify(e.params));
  })
  .up(function (e) {
    console.log("Mouse up. Was dragged?", e.params.wasDragged);
  })
  .down(function (e) {
    console.log("Mouse down.");
  })
  .click(function (e) {
    console.log("Click.");
  })
  .wheel(function (e) {
    console.log("Wheel. delta=" + e.deltaY);
  });

Build the package

Compile and build howto

Browsers support

IE / EdgeFirefoxChromeiOS Safari
IE11 & Edgelatestlatestlatest

Credits

Todos

What needs to be done

Known bugs

  • SVG resizing does not work in Firefox (aspect ratio is always kept, even if clip box changes). Please use PNGs until this is fixed.
  • The BBTree.iterator() fails if the tree is empty! (Demos)
  • The minifid BBTree and BBTreeCollection files do not export anything. The un-minified does. Why that?
  • Arcs and ellipses break when non-uniform scaling (scalex!=scaley) is applied. Convert them to Bézier curves before drawing.
  • Currently no more known. Please report bugs.

Oh look, a cat ᓚᘏᗢ

1.22.0

3 months ago

1.21.0

7 months ago

1.20.1

1 year ago

1.20.2

1 year ago

1.19.0

1 year ago

1.20.0

1 year ago

1.18.0

2 years ago

1.17.1

2 years ago

1.17.0

2 years ago

1.14.0

2 years ago

1.16.0

2 years ago

1.15.0

2 years ago

1.12.10

2 years ago

1.13.0

2 years ago

1.12.7

3 years ago

1.12.9

3 years ago

1.12.8

3 years ago

1.12.6

3 years ago

1.12.5

3 years ago

1.12.4

3 years ago

1.12.3

3 years ago

1.12.2

3 years ago

1.12.1

3 years ago

1.12.0

3 years ago

1.11.1

3 years ago

1.11.0

3 years ago

1.10.1

3 years ago

1.10.0

3 years ago

1.9.0

3 years ago

1.8.9

3 years ago

1.8.8

3 years ago

1.8.7

3 years ago

1.8.6

4 years ago

1.8.5

4 years ago

1.8.4

4 years ago

1.8.3

4 years ago

1.8.2

4 years ago

1.8.1

4 years ago

1.8.0

4 years ago

1.7.14

4 years ago

1.7.13

4 years ago

1.7.12

4 years ago

1.7.10

4 years ago

1.7.11

4 years ago

1.7.9

4 years ago

1.7.8

4 years ago

1.7.7

4 years ago

1.7.6

4 years ago

1.7.5

4 years ago

1.7.4

4 years ago

1.7.3

4 years ago

1.7.2

4 years ago

1.7.1

4 years ago

1.7.0

4 years ago

1.6.7

4 years ago

1.6.6

4 years ago

1.6.5

4 years ago

1.6.4

4 years ago

1.6.3

4 years ago

1.6.2

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.17

4 years ago

1.4.16

4 years ago

1.4.15

4 years ago

1.4.14

4 years ago

1.4.12

5 years ago

1.4.11

5 years ago

1.4.10

5 years ago

1.4.9

5 years ago

1.4.8

5 years ago

1.4.7

5 years ago

1.4.6

5 years ago

1.4.5

5 years ago

1.4.4

5 years ago

1.4.3

5 years ago

1.4.2

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

0.0.1-security

5 years ago