Skip to main content

utils

DG.Browser

A namespace with static properties for browser/feature detection.

if (DG.Browser.ie6) {
alert('Upgrade your browser, dude!');
}

Properties

PropertyTypeDescription
ieBooleantrue for all Internet Explorer versions (not Edge).
ielt9Booleantrue for Internet Explorer versions less than 9.
edgeBooleantrue for the Edge web browser.
webkitBooleantrue for webkit-based browsers like Chrome and Safari (including mobile versions).
geckoBooleantrue for gecko-based browsers like Firefox.
androidBooleantrue for any browser running on an Android platform.
android23Booleantrue for browsers running on Android 2 or Android 3.
chromeBooleantrue for the Chrome browser.
safariBooleantrue for the Safari browser.
ie3dBooleantrue for all Internet Explorer versions supporting CSS transforms.
webkit3dBooleantrue for webkit-based browsers supporting CSS transforms.
gecko3dBooleantrue for gecko-based browsers supporting CSS transforms.
opera12Booleantrue for the Opera browser supporting CSS transforms (version 12 or later).
any3dBooleantrue for all browsers supporting CSS transforms.
mobileBooleantrue for all browsers running in a mobile device.
mobileWebkitBooleantrue for all webkit-based browsers in a mobile device.
mobileWebkit3dBooleantrue for all webkit-based browsers in a mobile device supporting CSS transforms.
mobileOperaBooleantrue for the Opera browser in a mobile device.
mobileGeckoBooleantrue for gecko-based browsers running in a mobile device.
touchBooleantrue for all browsers supporting touch events.
msPointerBooleantrue for browsers implementing the Microsoft touch events model (notably IE10).
pointerBooleantrue for all browsers supporting pointer events.
retinaBooleantrue for browsers on a high-resolution "retina" screen.
canvasBooleantrue when the browser supports <canvas>.
vmlBooleantrue if the browser supports VML.
svgBooleantrue when the browser supports SVG.

DG.Util

Various utility functions.

Functions

FunctionReturnsDescription
extend( <Object> dest, <Object> src? ) ObjectMerges the properties of the src object (or multiple objects) into dest object and returns the latter. Has an DG.extend shortcut.
create( <Object> proto, <Object> properties? ) ObjectCompatibility polyfill for Object.create
bind( <Function> fn, ) FunctionReturns a new function bound to the arguments passed, like Function.prototype.bind. Has a DG.bind() shortcut.
stamp( <Object> obj ) NumberReturns the unique ID of an object, assiging it one if it doesn't have it.
throttle( <Function> fn, <Number> time, <Object> context ) FunctionReturns a function which executes function fn with the given scope context (so that the this keyword refers to context inside fn's code). The arguments received by the bound function will be any arguments passed when binding the function, followed by any arguments passed when invoking the bound function. Has an DG.bind shortcut.
wrapNum( <Number> num, <Number[]> range, <Boolean> includeMax? ) NumberReturns the number num modulo range in such a way so it lies within range[0] and range[1]. The returned value will be always smaller than range[1] unless includeMax is set to true.
falseFn()FunctionReturns a function which always returns false.
formatNum( <Number> num, <Number> digits? ) NumberReturns the number num rounded to digits decimals, or to 5 decimals by default.
trim( <String> str ) StringCompatibility polyfill for String.prototype.trim
splitWords( <String> str ) String[]Trims and splits the string on whitespace and returns the array of parts.
setOptions( <Object: options: Object> obj ) ObjectMerges the given properties to the options of the obj object, returning the resulting options. See Class options. Has an DG.setOptions shortcut.
getParamString( <Object> obj, <String> existingUrl?, <Boolean> uppercase? ) StringConverts an object into a parameter URL string, e.g. {a: "foo", b: "bar"} translates to '?a=foo&b=bar'. If existingUrl is set, the parameters will be appended at the end. If uppercase is true, the parameter names will be uppercased (e.g. '?A=foo&B=bar') Simple templating facility, accepts a template string of the form 'Hello {a}, {b}' and a data object like {a: 'foo', b: 'bar'}, returns evaluated string ('Hello foo, bar'). You can also specify functions instead of strings for data values — they will be evaluated passing data as an argument.
isArray(obj)BooleanCompatibility polyfill for Array.isArray
indexOf()NumberCompatibility polyfill for Array.prototype.indexOf
requestAnimFrame( <Function> fn, <Object> context?, <Boolean> immediate? ) requestId: NumberSchedules fn to be executed when the browser repaints. fn is bound to context if given. When immediate is set, fn is called immediately if the browser doesn't have native support for window.requestAnimationFrame, otherwise it's delayed. Returns an id that can be used to cancel the request.
cancelAnimFrame( <Number> id ) Cancels a previous requestAnimFrame. See also window.cancelAnimationFrame.

Properties

PropertyTypeDescription
lastIdNumberLast unique ID used by stamp()
emptyImageUrlStringData URI string containing a base64-encoded empty GIF image. Used as a hack to free memory from unused images on WebKit-powered mobile devices (by setting image src to this string).

DG.LineUtil

Various utility functions for polyine points processing, used by Leaflet internally to make polylines lightning-fast.

Functions

FunctionReturnsDescription
simplify( <Point[]> points, <Number> tolerance ) Point[]Dramatically reduces the number of points in a polyline while retaining its shape and returns a new array of simplified points, using the Douglas-Peucker algorithm. Used for a huge performance boost when processing/displaying Leaflet polylines for each zoom level and also reducing visual noise. tolerance affects the amount of simplification (lesser value means higher quality but slower and with more points). Also released as a separated micro-library Simplify.js.
pointToSegmentDistance( <Point> p, <Point> p1, <Point> p2 ) NumberReturns the distance between point p and segment p1 to p2.
closestPointOnSegment( <Point> p, <Point> p1, <Point> p2 ) NumberReturns the closest point from a point p on a segment p1 to p2.

DG.PolyUtil

Various utility functions for polygon geometries.

Functions

FunctionReturnsDescription
clipPolygon( <Point[]> points, <Bounds> bounds, <Boolean> round? ) Point[]Clips the polygon geometry defined by the given points by the given bounds (using the Sutherland-Hodgeman algorithm). Used by Leaflet to only show polygon points that are on the screen or near, increasing performance. Note that polygon points needs different algorithm for clipping than polyline, so there's a seperate method for it.