1.1.1 • Published 6 months ago

subjx v1.1.1

Weekly downloads
550
License
MIT
Repository
github
Last release
6 months ago

Demos

Basic example

Drag, zoom and pan SVG

Usage

Library provides dragging/resizing/rotating/snapping SVG/HTML Elements.

Installation

Run npm install to install with npm.

npm install subjx

Including via a <script> tag:

<script src="../dist/js/subjx.js"></script>

Get started

Main function subjx returns Subjx instance which based on elements finded by passed parameters:

import subjx from 'subjx';
import 'subjx/dist/style/subjx.css';

// possible parameters
const xElem = subjx( 'selector' ) |
                subjx( element ) |
                subjx( elementArray );

Transformation(drag/resize/rotate)

// enabling tool by `drag` method with the optional parameters
// by default just call `.drag()`
const xDraggable = xElem.drag();

// for disabling use `disable` method for each object
xDraggable.disable();

"Draggable" API

// getter returns root DOM element of 'controls'
xDraggable.controls;

// provides access to useful options
xDraggable.storage;
// for example: to get reference to any handle's DOM
const {
  handles: { tl, tr, ...etc }
} = xDraggable.storage;

// enables dragging
// there is no need to call this method manually
xDraggable.enable(options);

// disables dragging, removes controls and handles
xDraggable.disable();

 // adds event listener for some events
xDraggable.on(eventName, cb);

// removes event listener for some events
xDraggable.off(eventName, cb);

// Event names
const EVENTS = [
    'dragStart',
    'drag',
    'dragEnd',
    'resizeStart',
    'resize',
    'resizeEnd',
    'rotateStart',
    'rotate',
    'rotateEnd'
];

// execute dragging manually
xDraggable.exeDrag({
    dx, // drag along the x axis
    dy // drag along the y axis
});

// execute resizing manually
xDraggable.exeResize({
    dx, // resize along the x axis
    dy, // resize along the y axis
    revX, // reverse resizing along the x axis
    revY, // reverse resizing along the y axis
    doW, // allow width resizing
    doH  // allow height resizing
});

// execute rotating manually
xDraggable.exeRotate({
    delta // radians
});

// Align element inside container: ['t', 'l', 'r', 'b', 'v', 'h']
xDraggable.applyAlignment('tr');

// Call this method when applying scale or viewBox values changing
// useful when element's container was transformed from outside
xDraggable.fitControlsToSize();

// Sets the origin for an element's transformations
xDraggable.setTransformOrigin(
    {
        x, // absolute the origin's position x coordinate
        y, // absolute he origin's position y coordinate
        dx, // offset the origin's position x coordinate
        dy // offset the origin's position y coordinate
    },
    pin // leaves current origin fixed if true or not if false
);

// Sets transform origin to default
xDraggable.resetTransformOrigin();

// Returns element's current dimensions
xDraggable.getDimensions();

Options

PropertyDescriptionTypeDefault
containerTransformation coordinate system'selector' | elementelement.parentNode
controlsContainerParent element of 'controls''selector' | elementelement.parentNode
axisConstrain movement along an axisstring: 'x' | 'y' | 'xy''xy'
snapSnapping to grid in pixels/radiansobject{ x: 10, y: 10, angle: 10 }
eachMimic behavior with other '.draggable' elementsobject{ move: false, resize: false, rotate: false }
proportionsKeep aspect ratio on resizing / scalingbooleanfalse
draggableAllow or deny an actionbooleantrue
resizableAllow or deny an actionbooleantrue
rotatableAllow or deny an actionbooleantrue
scalableApplies scaling only to root elementbooleanfalse
restrictRestricts element dragging/resizing/rotation'selector' | element-
rotatorAnchorRotator anchor directionstring: 'n' | 's' | 'w' | 'e''e'
rotatorOffsetRotator offsetnumber50
transformOriginSets the origin for an element's transformationsboolean | Arrayfalse

Notice: In most cases, it is recommended to use 'proportions' option

Methods

subjx('.draggable').drag({
    onInit(elements) {
        // fires on tool activation
    },
    onMove({ clientX, clientY, dx, dy, transform }) {
        // fires on moving
    },
    onResize({ clientX, clientY, dx, dy, transform, width, height }) {
        // fires on resizing
    },
    onRotate({ clientX, clientY, delta, transform }) {
        // fires on rotation
    },
    onDrop({ clientX, clientY }) {
        // fires on drop
    },
    onDestroy(el) {
        // fires on tool deactivation
    }
});

Subscribing new draggable element to previously activated(useful with each option)

const options = {};
const observable = subjx.createObservable();
subjx('.draggable').drag(options, observable);

// pass Observable to new element
const createDraggableAndSubscribe = e => {
    subjx(e.target).drag(options, observable);
};

Allowed SVG elements: g, path, rect, ellipse, line, polyline, polygon, circle

Cloning

Options

const xCloneable = xElem.clone({
    // dropping area
    stack: 'selector',
    // set clone parent
    appendTo: 'selector',
    // set clone additional style
    style: {
        border: '1px dashed green',
        background: 'transparent'
    }
});

Methods

subjx('.cloneable').clone({
    onInit(el) {
        // fires on tool activation
    },
    onMove(dx, dy) {
        // fires on moving
    },
    onDrop(e) {
        // fires on drop
    },
    onDestroy() {
        // fires on tool deactivation
    }
});

Disabling

xCloneable.disable();

License

MIT (c) Karen Sarksyan

1.1.1

6 months ago

1.1.1-rc.1

1 year ago

1.1.0-rc.5

2 years ago

1.1.0

2 years ago

1.1.0-rc.4

2 years ago

1.1.0-rc.3

2 years ago

1.0.0

2 years ago

1.0.0-rc.3

2 years ago

1.1.0-rc.2

2 years ago

1.0.0-rc.4

2 years ago

1.1.0-rc.1

2 years ago

3.0.0-rc.1

2 years ago

1.0.0-rc.2

2 years ago

1.0.0-rc.1

2 years ago

0.3.9

3 years ago

0.3.8

3 years ago

0.3.7

3 years ago

0.3.6

3 years ago

0.3.5

3 years ago

0.3.4

3 years ago

0.3.2

3 years ago

0.3.3

3 years ago

0.3.1

3 years ago

0.3.0-beta.0

3 years ago

0.2.7

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.3

4 years ago

0.2.4

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.9

4 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago