Interactive demos of hcg-resizable, a lightweight zero-dependency JavaScript
library for resizable elements. Every option is shown below:
handles, minWidth, minHeight, maxWidth,
maxHeight, aspectRatio, containment,
disabled, data-hcg-* attributes, option() / setOption(),
contain(), lifecycle callbacks, and instance methods.
Version .
hcg-resizable - Full documentation
Default setup enables all eight edge and corner handles: n, e, s, w, ne, se, sw, nw.
new HcgResizable('#box-all', {
onStart(event, size) { console.log('start', size); },
onEnd(event, size) { console.log('end', size.width, size.height); }
});
Pass a handles array to enable only the handles you need. Here only the bottom-right corner is active.
new HcgResizable('#box-corner', {
handles: ['se']
});
Resize horizontally without changing height. North and west handles also adjust top and left so the opposite edge stays anchored.
new HcgResizable('#box-edges', {
handles: ['e', 'w']
});
aspectRatio: true locks the width/height ratio present when the resize starts.
new HcgResizable('#box-aspect', {
aspectRatio: true,
onResize(event, size) {
console.log(size.width, size.height);
}
});
Pass a number for a fixed ratio (width divided by height). Useful for video or image crop UIs.
new HcgResizable('#box-aspect-num', {
aspectRatio: 16 / 9
});
containment: 'parent' clamps size so the element cannot grow past the dashed stage edges.
new HcgResizable('#box-bounded', {
containment: 'parent'
});
var stage = document.getElementById('stage');
new HcgResizable('#box', { containment: stage });
Clamp width and height while dragging. This box stays between 80×60 px and 220×180 px.
new HcgResizable('#box-minmax', {
minWidth: 80,
minHeight: 60,
maxWidth: 220,
maxHeight: 180
});
Set or read dimensions programmatically. setSize also writes data-hcg-size on the element.
Current size: -
var resize = new HcgResizable('#box-setsize');
resize.setSize(180, 120);
var size = resize.getSize(); // { width: 180, height: 120 }
Turn resizing off without destroying the instance. Handles stay in the DOM but ignore pointer input.
Resizing: enabled
var resize = new HcgResizable('#box-toggle');
resize.disable();
resize.enable();
resize.destroy();
Add data-hcg-resizable and option attributes in HTML, then call HcgResizable.init() once your DOM is ready.
<div
data-hcg-resizable
data-hcg-handles="se"
data-hcg-min-width="80"
data-hcg-min-height="60"
data-hcg-max-width="200"
data-hcg-max-height="160">
data-hcg-*
</div>
HcgResizable.init();
Change handles, min/max, or aspect ratio on a live instance with setOption(name, value). Read values with option(name).
handles: se | aspectRatio: false
var r = new HcgResizable('#box', { handles: ['se'] });
r.setOption('handles', ['n', 'e', 's', 'w', 'ne', 'se', 'sw', 'nw']);
r.setOption('aspectRatio', true);
r.option('minWidth'); // getter
Pass a CSS selector string to containment, not only 'parent'.
new HcgResizable('#box', {
containment: '#selector-stage'
});
Resize the box to the right edge, then make the browser window narrower. contain() runs automatically on window resize when containment is set.
var r = new HcgResizable('#box', {
containment: '#resize-stage'
});
r.contain(); // manual re-clamp
When a selector matches several nodes, the factory returns an array of instances.
var items = HcgResizable('.box-multi', { handles: ['se'] });
if (Array.isArray(items)) {
items.forEach(function (r) { r.disable(); });
}
Lifecycle callbacks from every demo above are logged here in real time.