Rulez.js
Tihs is fork of rulez.js Rulez.js is a javascript library for showing svg based customizable rulers. It generates divisions and texts once and only for visible area.
The only difference: duplicating gaps are not stacked, the higher the element, the higher the priority
Instalation
npm install rulez-fork
Usage
Basic usage
Horizontal ruler
var someSvgElement = document;var rulez = element: someSvgElement;rulez;
Vertical ruler
var someSvgElement = document;var rulez = element: someSvgElement layout: 'vertical';rulez;
Alignment
Every horizontal ruler can be aligned to top or bottom (default is 'top'):
var someSvgElement = document;var rulez = element: someSvgElement alignment: 'bottom';rulez;
Every vertical ruler can be aligned to left or right (default is 'left'):
var someSvgElement = document;var rulez = element: someSvgElement layout: 'vertical' alignment: 'left';rulez;
Customizing rulers
Internally rulez.js use default config, but you can override it by passing more parameters in constructor. There is only one limitation to remember : maximum pixelGap used for divisions must be dividable by all other pixelGaps (pixelGaps of texts are included also). For example, next config is ok:
var rulez = ... divisions: pixelGap: 25 lineLength: 10 pixelGap: 100 lineLength: 20 texts: pixelGap: 5 pixelGap: 25 pixelGap: 50 ;
max pixelGap of divisions is 100 and it's dividable by every other pixelGap used in config(25, 5, 25, 50). Ruler with the next one config may be rendered incorrectly if scrollTo method is used:
var rulez = ... divisions: pixelGap: 25 lineLength: 10 pixelGap: 100 lineLength: 20 texts: pixelGap: 6 pixelGap: 25 pixelGap: 50 ;
max pixelGap of divisions is 100 and it's NOT dividable by 6.
Specifying units
By default rulez.js uses 'user' units(px). This can be changed by specifying units param in config. Possible values for units: 'em', 'ex', 'px', 'pt', 'pc', 'cm', 'mm', 'in' and ''(user units) svg units spec. Note that those units will be used for all params for divisions and texts(pixelGap, lineLength, strokeWidth, etc).
var rulez = units: 'pt';
Units conversion rate (how many pixels is in one specified unit) can be retrieved using method getUnitConversionRate
var rulez = units: 'pt';var unitsConversionRate = rulez;
Customizing divisions
Divisions can be changed by providing array of divisions config objects
var rulez = ... divisions: pixelGap: 25 lineLength: 10 pixelGap: 100 lineLength: 20 ;
The code above means that ruler will be created with two different division types:
- Long ones(20px) with big gap between them(100px).
- Short ones(10px) with small gap between them(25px)
Other parameters that can be changed are
strokeWidth : 1// width of division className: 'someClassName'// css class applied to every division type: 'rect'// 'rect' or 'line': type of svg element used to render division {// function that is called when division is added to ruler division; }
Customizing texts
Texts can be changed by providing array of texts config objects
var rulez = ... texts: pixelGap: 50 pixelGap: 100 ;
The code above means that ruler will be created with two different texts types:
- Texts with big gap between them(100px).
- Texts with smaller gap between them(50px)
Other parameters that can be changed are
className: 'someClassName'// css class applied to every textoffset: 20//offsets of texts in pixelsrotation:90//rotation in degrees of textsshowUnits:true//Wherever to show or not to show units alongside text{// function that is called when text is added to ruler text;}
Default configs
It's possible to use default configs that will used for all texts and divisions if they not specify parameters on their own. Any parameters that can be used for divisions or texts are also applicable for default configs.
divisionDefaults: strokeWidth: 1 type: 'rect' className: 'rulez-rect'textDefaults: rotation: 0 offset: 25 className: 'rulez-text'
Scrolling rullers to a specific position
Every ruller can be scrolled to a specific position by using
ruler;/* example */ruler;
Resizing
If width(height for vertical) is increased you'll need to resize ruler by calling resize method.
ruler;
Scaling
For scaling of ruler method setScale can be used. Internally it multiples text's value by provided scaleValue.
ruler;/* example */ruler;
Text centering
Starting from v0.1.0 text elements are centered by default.
textDefaults: rotation: 0 offset: 25 className: 'rulez-text' centerText: true
Possible values for 'by':
- 'width' - calculations are made by accessing textElement.getBoundingClientRect().width property
- 'height' - calculations are made by accessing textElement.getBoundingClientRect().height property Possible values for 'operation':
- 'sum' textElement.getBoundingClientRect()[by]/2 will be added to original position
- 'sub' textElement.getBoundingClientRect()[by]/2 will be subtracted from original position
Saving as image
Ruler can be saved as image(png base64)
rulezH;