Giter Site home page Giter Site logo

ned's Introduction

Ned

Simple javascript node editor using svg graphics. A working example can be found here.

Some SVG2 features are required. Currently working and tested in Chrome.

The panning and zooming in the example uses ariutta svg-pan-zoom but any pan and/or zoom could be used.

Examples

How To Use

For a complete look on how everything works you can take a look at the index.html.

Editor

Create and init an editor using Ned.Create(arg). The argument to function should be a CSS selector or a DOM Element.

var editor = Ned.create("#svg");
// or
var svgElement = document.querySelector("#svg");
var editor = Ned.create(svgElement);

After creating an editor you can setup the options and create nodes.

Options

  • snapping: value to snap nodes to (0 or false is disabled). Default is 0.
  • singleInputs: when true input connectors only allow a single connection. Default is false.
  • singleOutputs: when true output connectors only allow a single connection. Default is false.
  • screenToWorld: function to translate screen coördinates to world coördinates. This is used to corectly position nodes and paths when you enable a pan and/or zoom on the svg. Default is (pos) => { return pos; }.

The screenToWorld function should be correctly set before any connections are made. Any paths added before this will be drawn incorrectly. The caliing context of this method is the editor trying to translate the position.

ScreenToWorld example with svg position offset.

editor.screenToWorld = function(pos) {
	var rect = this.svg.getBoundingClientRect();

	return { 
		x: (pos.x - rect.left), 
		y: (pos.y - rect.top)
	};
};

ScreenToWorld example with svg position offset and pan and zoom using ariutta svg-pan-zoom.

var panZoom = svgPanZoom(editor.svg);

editor.screenToWorld = function(pos) {
	var rect = this.svg.getBoundingClientRect();
	var pan = this.panZoom.getPan();
	var zoom = this.panZoom.getZoom();

	return { 
		x: (((pos.x - rect.left) - pan.x) / zoom), 
		y: (((pos.y - rect.top) - pan.y) / zoom)
	};
};

Nodes

Nodes can simple be created by calling

// create a new node
var n1 = editor.createNode("Node title");

// position and size the node
n1.position = { x: 100, y: 180};
n1.size = { width: 100, height: 60 };

Connectors

Connectors are the link points of the nodes. Any node can have as many or little connectors as needed. Connectors are split in two groups: input and output. To add connectors you call

// a node n1 was already created
var n1i1 = n1.addInput("Input name");
var n1o1 = n1.addOutput("Output name");

Connections can be made by simply calling the connectTo function on a connector and passing in the target connector. If a connection can not be made the function will return false.

n1o1.connectTo(n2i1);

Depending on the editor settings for singleInputs and singleOutputs the number of possible connections is limited.

Customization

Most of the visual customization can be done inside the css.

Custom content can be added to the eForeign object. A working example can be found here.

var n1 = editor.createNode("Test node");
n1.position = { x: 100, y: 180};
n1.size = { width: 400, height: 200 };

n1.eForeign.insertAdjacentHTML("afterbegin", 
  `<div class="nodeContent">
  <select>
    <option>test 1</option>
    <option>test 2</option>
    <option>test 3</option>
  </select>
  <textarea>Text must come here</textarea>
  </div>
  `);

SVG structure

This is the default svg structure without any other libraries or other things in the svg. Except for the first ROOT svg element the structure is "DOM object ClassName".

`svg` ROOT  
  │  
  ├─ `g` Nodegroup  
  │   ├─ `svg` NodeContainer  
  │   │   ├─ `rect` Background  
  │   │   ├─ `g` Header  
  │   │   │   ├─ `rect`  
  │   │   │   └─ `text`  
  │   │   ├─ `svg` Inputs  
  │   │   │   ├─ `svg` Input  
  │   │   │   │   ├─ `text`  
  │   │   │   │   └─ `circle`  
  │   │   │   └─ ...  
  │   │   ├─ `svg` Outputs  
  │   │   │   ├─ `svg` Output  
  │   │   │   │   ├─ `text`  
  │   │   │   │   └─ `circle`  
  │   │   │   └─ ...  
  │   │   └─ `foreignObject`  
  │   └─ ...  
  └─ `g` PathGroup  
      ├─ `path` Path  
      └─ ...  

ned's People

Contributors

depuits avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.