1.6.0 • Published 2 years ago

@wework/floormap.gl v1.6.0

Weekly downloads
6
License
WeWork
Repository
-
Last release
2 years ago

floormap.gl

A javascript rendering engine that provides a simple interface to draw shapes, image, text and line on the screen.

Quick Start

Clone the project

git clone git@github.com:WeConnect/floormap.gl.git
cd floormap.gl

Install dependencies

yarn install

Start the demo

yarn start:demo

Outcome

alt text

Building the library

yarn build

A dist folder will be created, and outputs files are floormap.gl.min.js, index.js and index.es.js files

Gently reminder: In order to use this floormap.gl library in your own node application, you may yarn link first and it will register @wework/floormap.gl in your local development. Then yarn link '@wework/floormap.gl' in your node application

Example usage in HTML

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>FloorMapGL</title>
</head>
<body>
  <div id="floormap"></div>
  <script type="text/javascript" src="dist/floormap.gl.min.js" ></script>
  <script>
    // create a renderer
    var renderer = new FloorMapGL({
      target: document.getElementById('floormap'),
      size: {
        width: 500,
        height: 500
      },
      backgroundColor: 'grey',
      antialias: true,
      pixelRatio: 2
    })

    // draw a simple mesh
    renderer.draw({
      id: '001',
      tags: ['hello'],
      type: 'MESH',
      style: {
        color: 'rgb(155, 255, 55)',
        side: 'FRONT',
        outline: {
          color: 'rgb(255, 0, 0)',
          only: false
        }
      },
      points: [
        { x: -1, y: -1 },
        { x: 1, y: -1 },
        { x: 1, y: 1 },
        { x: -1, y: 1 }
      ],
      interactable: true,
      extrude: 0,
      position: { x: 0, y: 0, z: 0 },
      rotation: { x: 0, y: 0, z: 0 },
      scale: { x: 10, y: 10, z: 10 }
    },
    false,
    (success) => {
      console.log(success)
    },
    (error) => {
      console.log(e)
    })
  </script>
</body>
</html>

Example usage in ReactJs application

link the floormap.gl package in your node application

yarn link '@wework/floormap.gl'

or download the package directly from the npm

yarn add '@wework/floormap.gl'

Example usage in a react application

import React, { Component } from 'react'
import FloorMapGL from '@wework/floormap.gl'

class FloorMap extends Component {
  componentDidMount() {
    this.renderer = new FloorMapGL({
      target: this.mount,
      size: {
        width: 500,
        height: 500
      },
      backgroundColor: 'grey',
      antialias: true,
      pixelRatio: 2
    })

    this.renderer.draw(
      {
        id: '001',
        tags: ['hello'],
        type: 'MESH',
        style: {
          color: 'rgb(155, 255, 55)',
          side: 'FRONT',
          outline: {
            color: 'rgb(255, 0, 0)',
            only: false
          }
        },
        points: [
          { x: -1, y: -1 },
          { x: 1, y: -1 },
          { x: 1, y: 1 },
          { x: -1, y: 1 }
        ],
        interactable: true,
        extrude: 0,
        position: { x: 0, y: 0, z: 0 },
        rotation: { x: 0, y: 0, z: 0 },
        scale: { x: 10, y: 10, z: 10 }
      },
      false,
      (success) => {
        console.log(success)
      },
      (error) => {
        console.log(e)
      }
    )
  }

  render() {
    return (
      <div
        ref={(mount) => {
          this.mount = mount
        }}
      />
    )
  }
}

export default FloorMap

Creating a new Renderer

import FloorMapGL from '@wework/floormap.gl'

var renderer = new FloorMapGL({
  target: domElement,
  size: {
    width: 720,
    height: 480
  },
  backgroundColor: 'black',
  antialias: true,
  pixelRatio: 2,
  camera: {
    maxPolarAngle: 90,
    enableRotate: true,
    zoomLevel: 1
  },
  groundPlane: true
})
NameTypeDescription
targetobjectdomElement, it is required
sizesizeObjectwindow size of the renderer
backgroundColorstringbackground color of the renderer, default is 'black'
antialiasbooleanOptions to smooth the jagged line, default is true
pixelRationumberThe rendering resolution, default is 2
cameracameraObjectcamera settings
groundPlanebooleaninvisible ground plane for the mouse interaction, default is false
sizeObject
NameTypeDescription
widthnumberthe width of the renderer window, by default is 700
heightnumberthe height of the renderer window, by default is 400
cameraObject
NameTypeDescription
zoomLevelnumberzoom level of the camera view, default is 1
enableRotatebooleanto enable to control and rotate the camera view angle, default is true
maxPolarAnglenumbermax horizontal rotation of the camera in degree, default is 90

List of APIs in floormap.gl

draw

Draw the list of renderObjects onto the screen

.draw(renderObjects: array, transparentUpdate: boolean, onSuccess: function, onError: function)

ParametersTypeDescription
arrayOfParamsarrayarray of RenderObjects
transparentUpdatebooleanflag to enable clearing and re-render the scene again for transparent object
onSuccessfunctioncallback function when successfully render this draw call
onErrorfunctioncallback function when there is an error occur in this draw call
Example:
// create your renderObjects and push into your array first
const renderObjects = []
renderObjects.push({
  id: '001',
  tags: '',
  type: 'MESH',
  style: {
    color: 'rgb(155, 255, 55)',
    side: 'FRONT',
    outline: {
      color: 'rgb(255, 0, 0)',
      only: false
    }
  },
  points: [{ x: -1, y: -1 }, { x: 1, y: -1 }, { x: 1, y: 1 }, { x: -1, y: 1 }],
  interactable: true,
  visible: true,
  extrude: 0,
  position: { x: 0, y: 0, z: 0 },
  rotation: { x: 0, y: 0, z: 0 },
  scale: { x: 1, y: 1, z: 1 },
  draw: true
})

// draw all the renderObjects using .draw
renderer.draw(
  renderObjects,
  false,
  (success) => {
    console.log(success)
  },
  (errors) => {
    console.log(errors)
  }
)

// to update the renderObjects in the future
// using the same .draw() api to update the specific renderObject by id
renderer.draw(
  [
    {
      id: '001',
      style: {
        color: 'rgb(155, 255, 55)'
      },
      position: { x: 10, y: 0, z: 0 }
    }
  ],
  false,
  (success) => {
    console.log(success)
  },
  (errors) => {
    console.log(errors)
  }
)

updateWindowDimension

Update the size of the renderer window

.updateWindowDimension({ width: Number, height: Number })

ParametersTypeDescription
widthnumbernew width for the renderer window
heightnumbernew height for the renderer window
Example:
renderer.updateWindowDimension({ width, height })

setCameraViewPoint

Set the camera panning position

.setCameraViewPoint({ position: { x: Number, y: Number, z: Number } })

Example:
renderer.setCameraViewPoint({
  position: { x: 2, y: 0, z: 5 }
})

setCameraViewAngle

Set the camera rotation view angle

.setCameraViewAngle({ polarAngle: Number, azimuthAngle: Number })

Example:
renderer.setCameraViewAngle({ polarAngle: 45, azimuthAngle: -45 })

getIdsByTags

Get all the renderObjects id by tags

.getIdsByTags(tags: Array)

Example:
renderer.getIdsByTags(['tag1', 'tag2'])

fitContent

Adjust the renderer camera view frustum to fit the content space / given drawn boundaries

.fitContent(params: Object)

ParametersTypeDescription
idstringrenderObject id, default is the whole renderScene boundary
paddingnumberadd the padding for the camera view, default is 0
pointsarraycustom boundary shape, optional
Example:
this.renderer.fitContent()
this.renderer.fitContent({ padding: 50 })
this.renderer.fitContent({ id: '07-124', padding: 50 })
this.renderer.fitContent({ points: points, padding: 50 })

createInstancedMesh

.createInstancedMesh(renderObject: object)

Example:
renderer.createInstancedMesh({
  id: 'MESH_GROUP_1',
  mesh: {
    style: {
      color: 'rgb(155, 255, 55)',
      side: 'FRONT',
      outline: {
        color: 'rgb(255, 0, 0)',
        width: 0.2,
        only: false
      }
    },
    points: [...],
    extrude: 5
  },
})

addToInstancedMesh

.addToInstancedMesh(params: object)

Example:
renderer.addToInstancedMesh({
  id: 'UUID',
  instancingId: 'MESH_GROUP_1',
  style: {
    color: 'rgb(100,101,102)'
  },
  position: { x: 0, y: 0, z: 0 },
  rotation: { z: 0 },
  scale: { x: 1, y: 1, z: 1 }
})

drawInstancedMesh

.drawInstancedMesh(params: object)

Example:
renderer.drawInstancedMesh({ id: 'MESH_GROUP_1' })

addEventListener

subscribe to a renderer event

.addEventListener(eventName: String, callback: Function)

Example:
renderer.addEventListener(renderer.Events.ONMOUSEOVER, mouseEnterCallback)
renderer.addEventListener(renderer.Events.ONMOUSEOUT, mouseLeaveCallback)
renderer.addEventListener(renderer.Events.ONCLICK, onClickCallback)

removeEventListener

unsubscribe to a renderer event

.removeEventListener(eventName: String, callback: Function)

Example:
renderer.removeEventListener(renderer.Events.ONMOUSEOVER, mouseEnterCallback)
renderer.removeEventListener(renderer.Events.ONMOUSEOUT, mouseLeaveCallback)
renderer.removeEventListener(renderer.Events.ONCLICK, onClickCallback)

removeAllEventListeners

unsubscribe all the renderer event listeners

.removeAllEventListeners()


remove

remove specific renderObject by id

.remove(id: String)

Example:
renderer.remove(id)

removeAll

remove all the renderObjects in the renderer

.removeAll()

Example:
renderer.removeAll()

Events

NameValueDescription
ONCLICK'onclick'when the user select the interactable renderObject by clicking/touching
ONHOVER'onmousehover'when the user mouse hovering the interactable renderObject
ONMOUSEOVER'onmouseover'when moving the mouse pointer onto a renderObject
ONMOUSEOUT'onmouseout'when moving the mouse pointer out of a renderObject
ONMOUSEMOVE'onmousemove'when moving the mouse pointer
ONRENDER'onrender'when .draw api successfully sent all the data to the gpu to render
ONERROR'onerror'when floormap.gl renderer encounters any internal errors
ONTEXTURELOAD'ontextureload'when texture is loaded and sent to the gpu

ONCLICK

PayloadTypeDescription
idstringid of the renderObject
pointobjectintersection point in the world space, x y z value
mousePosobjectthe mouse position in screen space, x y value

ONHOVER

PayloadTypeDescription
idstringid of the renderObject
pointobjectintersection point in the world space, x y z value
mousePosobjectthe mouse position in screen space, x y value

ONMOUSEOVER

PayloadTypeDescription
idstringid of the renderObject
pointobjectintersection point in the world space, x y z value
mousePosobjectthe mouse position in screen space, x y value

ONMOUSEOUT

PayloadTypeDescription
idstringid of the renderObject
pointobjectintersection point in the world space, x y z value
mousePosobjectthe mouse position in screen space, x y value

ONMOUSEMOVE

PayloadTypeDescription
idstringid of the renderObject
pointobjectintersection point in the world space, x y z value
mousePosobjectthe mouse position in screen space, x y value

ONRENDER

PayloadTypeDescription
frameTimenumberthe time taken to complete this draw call
memoryobjectgpu memory info
renderobjectrender info
renderObjectsarraylist of renderObjects to render in the draw call

ONERROR

PayloadTypeDescription
errorobjectError() object

ONTEXTURELOAD

PayloadTypeDescription
frameTimenumberthe time taken to complete this draw call
memoryobjectgpu memory info
renderobjectrender info
renderObjectsarraylist of renderObjects to render in the draw call

RenderObjects

Mesh

ParametersTypeDescription
id !requiredstringid of the renderObject
tagsarrayrenderObject tags
type !requiredstringrenderObject type, 'MESH', 'SPRITE', 'TEXT' or 'LINE'
stylemeshStyleObjectstyle of the renderObject
points !requiredarraypoints to construct a shape
geometryTranslatetransformObjecttranslation for the geometry in object space
interactablebooleaninteraction of this object disable or enable
visiblebooleanvisibility flag
extrudenumberextrude level for the mesh
positiontransformObjectposition of the renderObject
rotationtransformObjectrotation of the renderObject
scaletransformObjectscale of the renderObject
Example:
{
  id: '001', // required
  tags: ['level 3'],
  type: 'MESH',
  style: {
    color: 'rgb(155, 255, 55)',
    side: 'FRONT',
    texture: {
      img: '',
      repeat: 0.5
    }
    outline: {
      color: 'rgb(255, 0, 0)',
      width: 0.2,
      only: false
    }
  },
  points: [
    { x: -1, y: -1 },
    { x: 1, y: -1 },
    { x: 1, y: 1 },
    { x: -1, y: 1 }
  ],
  geometryTranslate: {x: -1, y: -1, z: 0},
  interactable: true,
  visible: true,
  extrude: 2,
  position: { x: 0, y: 0, z: 0 },
  rotation: { x: 0, y: 0, z: 0 },
  scale: { x: 1, y: 1, z: 1 }
}
Draw Outcome:

Text

ParametersTypeDescription
id !requiredstringid of the renderObject
tagsarrayrenderObject tags
type !requiredstringrenderObject type, 'MESH', 'SPRITE', 'TEXT' or 'LINE'
styletextStyleObjectstyle of the renderObject
text !requiredstringtext to draw on the screen
interactablebooleaninteraction of this object disable or enable
visiblebooleanvisibility flag
positiontransformObjectposition of the renderObject
scalarnumbertext size
Example:
{
  id: '002',
  tags: ['nothing'],
  type: 'TEXT',
  position: { x: 0, y: 0, z: 0 },
  scalar: 1.0,
  center: { x: 0.5, y: 0.5 },
  style: {
    color: 'rgb(255, 255, 255)',
    fontFamily: 'Arial, Helvetica, sans-serif',
    textAlign: 'center',
    fontWeight: 'normal', // normal, bold, bolder, lighter
    fontStyle: 'italic' // normal, italic, oblique
  },
  text: 'hello world',
  interactable: false,
  visible: true
}
Draw Outcome:

Sprite

ParametersTypeDescription
id !requiredstringid of the renderObject
tagsarrayrenderObject tags
type !requiredstringrenderObject type, 'MESH', 'SPRITE', 'TEXT' or 'LINE'
positiontransformObjectposition of the renderObject
styletextStyleObjectstyle of the renderObject
interactablebooleaninteraction of this object disable or enable
visiblebooleanvisibility flag
Example:
{
  id: '003',
  tags: ['level 3'],
  type: 'SPRITE',
  position: { x: 2, y: 0 },
  center: { x: 0.5, y: 0.5 },
  style: {
    color: 'rgb(255, 255, 255)',
    img: 'https://cdn.spacemob.co/img/meeting_room@2x.png',
    width: 'auto',
    height: 'auto',
    maxWidth: 3,
    maxHeight: 3
  },
  interactable: false,
  visible: true
}
Draw Outcome:

Line

ParametersTypeDescription
id !requiredstringid of the renderObject
tagsarrayrenderObject tags
type !requiredstringrenderObject type, 'MESH', 'SPRITE', 'TEXT' or 'LINE'
lineWidthnumberthe width of the line
points !requiredarraypoints for constructing the line
positiontransformObjectposition of the renderObject
stylelineStyleObjectstyle of the renderObject
interactablebooleaninteraction of this object disable or enable
visiblebooleanvisibility flag
Example:
{
  id: '004',
  tags: ['level 3'],
  type: 'LINE',
  lineWidth: 1.0,
  points: [
    { x: 0, y: 0 },
    { x: 100, y: 0 },
    { x: 100, y: 90 },
    { x: 200, y: 20 }
  ],
  style: {
    color: 'rgb(255, 0, 255)',
    opacity: 0.7
  },
  interactable: false,
  visible: true
}
Draw Outcome:

Type Definitions

transformObject

ParametersTypeDescription
xnumberset value in x-axis
ynumberset value in y-axis
znumberset value in z-axis

meshStyleObject

ParametersTypeDescription
colorstringcolor of the object
sidestring'FRONT', 'BACK' or 'DOUBLE', default is 'FRONT'
texturenumbertexture setting of the renderObject
texture.imgstringimg path or url
texture.repeatnumberhow many times to repeat the image on the object
outlinenumberoutline setting of the renderObject
outline.colornumberoutline color of the object
outline.onlynumberif true then will be outline only object

textStyleObject

ParametersTypeDescription
colorstringcolor of the text
fontFamilystringdefault is 'Arial, Helvetica, sans-serif'
textAlignnumbertext alignment, 'center', 'left' or 'right', default is 'center'
fontWeightstring'normal', 'bold', 'bolder' or 'lighter', default is 'normal'
fontStylenumber'normal', 'italic' or 'oblique', default is 'normal'

spriteStyleObject

ParametersTypeDescription
colorstringcolor of the sprite
imgstringimage path or url
widthnumber or stringwidth of the sprite, default is 'auto'
heightnumber or stringheight of the sprite, default is 'auto'
maxWidthnumbermaximum width of the sprite
maxHeightnumbermaximum height of the sprite

lineStyleObject

ParametersTypeDescription
colorstringcolor of the line
opacitynumbertransparency of the line
3.13.2

2 years ago

3.7.7

2 years ago

2.26.0

2 years ago

2.25.5

2 years ago

2.25.2

2 years ago

3.0.0

2 years ago

2.28.0

2 years ago

2.6.2-rc.1

3 years ago

2.6.2-rc.0

3 years ago

2.5.1-rc.5

3 years ago

1.6.1-rc.82

4 years ago

2.1.0

4 years ago

1.6.0

4 years ago

1.5.4-rc.2

4 years ago

1.5.3

4 years ago

1.5.3-rc.6

4 years ago

1.5.3-rc.4

4 years ago

1.5.2

4 years ago

1.5.2-rc.2

4 years ago

1.5.1

4 years ago

1.5.1-rc.2

4 years ago

1.5.0

4 years ago

1.4.3-rc.22

4 years ago

1.4.2

4 years ago

1.4.2-rc.3

4 years ago

1.4.1

4 years ago

1.4.1-rc.15

4 years ago

1.4.1-tsdx-2.20

4 years ago

1.3.3-tsdx-2.15

4 years ago

1.3.3-tsdx2.15

4 years ago

1.3.3-tsdx.15

4 years ago

1.3.3-rc.8

5 years ago

1.4.0

5 years ago

1.3.3-rc.6

5 years ago

1.3.3

5 years ago

1.3.3-rc.5

5 years ago

1.3.3-rc.4

5 years ago

1.3.1-rc.12

5 years ago

1.3.2

5 years ago

1.3.1-rc.8

5 years ago

1.3.1-rc.7

5 years ago

1.3.1

5 years ago

1.3.1-rc.0

5 years ago

1.3.0

5 years ago

1.2.5-rc.39

5 years ago

1.2.4-rc.15

5 years ago

1.2.4

5 years ago

1.2.4-rc.12

5 years ago

1.2.4-rc.11

5 years ago

1.2.3-rc.6

5 years ago

1.2.3

5 years ago

1.2.3-next.3

5 years ago

1.2.3-next.2

5 years ago

1.2.3-ss.6

5 years ago

1.2.3-ss.5

5 years ago

1.2.3-ss.2

5 years ago

1.2.3-next.1

5 years ago

1.2.3-next.0

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.1-next.48

5 years ago

1.2.1-next.47

5 years ago

1.2.1-next.46

5 years ago

1.2.1-next.45

5 years ago

1.2.1-next.44

5 years ago

1.2.1-next.40

5 years ago

1.2.1-next.39

5 years ago

1.2.1-next.38

5 years ago

1.2.1-next.37

5 years ago

1.2.1-next.36

5 years ago

1.2.1-next.35

5 years ago

1.2.1-next.34

5 years ago

1.2.1-rc.30

5 years ago

1.2.0

5 years ago

1.2.0-rc3

5 years ago

1.2.0-rc2

5 years ago

1.2.0-rc1

5 years ago

1.1.1

5 years ago

1.1.1-rc1

5 years ago

1.1.0

5 years ago

1.1.0-rc1

5 years ago

1.0.1

5 years ago

1.0.1-rc1

5 years ago

1.0.0

5 years ago

1.0.0-rc3

5 years ago

1.0.0-rc2

5 years ago

1.0.0-rc1

5 years ago

0.9.1

5 years ago

0.9.1-rc1

5 years ago

0.9.0

5 years ago

0.8.5-rc2

5 years ago

0.8.5-rc1

5 years ago

0.8.4

5 years ago

0.8.3

5 years ago

0.8.3-rc1

5 years ago

0.8.2

5 years ago

0.8.2-rc6

5 years ago

0.8.2-rc5

5 years ago

0.8.2-rc4

5 years ago

0.8.2-rc3

5 years ago

0.8.2-rc1

5 years ago

0.8.1

5 years ago

0.8.1-rc1

5 years ago

0.8.0-rc3

5 years ago

0.8.0-rc1

5 years ago

0.8.0

5 years ago

0.7.0

5 years ago

0.6.0

5 years ago

0.5.3

5 years ago

0.5.2

5 years ago

0.5.1

5 years ago

0.5.0

5 years ago

0.4.2

5 years ago

0.4.1

5 years ago