Draw a line on a page between two arbitrary points using a DOM element (usually a div). No SVG, no VML, no canvas.
$.line(fromPoint, toPoint[, options]);
Draws a line from fromPoint
to toPoint
using a DOM node (a <div>
by default).
fromPoint
and toPoint
are objects containing x
and y
(numeric) properties that define points on the page (relative to the top left corner of the page, not the screen).
options
is an optional (duh) object as defined below.
The options
object can have the following parameters:
elem
<div>
is created.className
<div>
– only used if elem
option is falsey.lineWidth
lineColor
returnValues
Default options are defined in the $.line.defaults
object as shown:
$.line.defaults = {
elem: '', // if falsey, creates a new <div>
className: 'jquery-line',
lineWidth: 1,
lineColor: '#000',
returnValues: false
};
These can be re-defined to make new default options for all subsequent calls to $.line()
.
If the fromPoint
or toPoint
objects are not valid, the return value is false
.
If the returnValues
option is false
(the default), the return value is a jQuery object for the line element.
If the returnValues
option is true
, the return value is an object containing the following properties:
from
– The fromPoint
argument passed to the functionto
– The toPoint
argument passed to the functioncenter
– The centre point of the line (and also the point of rotation) – contains x
and y
propertiesrotation
– The amount of rotation applied to the line – contains deg
(degrees) and rad
(radians) propertiesTo be honest this was more of an experiment for my own needs than anything else, and an excuse to play around with CSS3 transforms. If it suits your needs, that’s good, but if some other solution works for you, that’s fine too.