$(function () {
// the renderer
var renderer = new Highcharts.Renderer($('#container')[0], 600, 600),
// rect
// however, you can use any method that highcharts does
// arc, circle, image, path, rect, text
r,
// boolean to check whether the mouse is down
isDown = false,
// x anchor (the x point where the mouse was initially clicked)
anchorX,
// y anchor (the y point where the mouse was initially clicked
anchorY;
// event handler for mousedown event
$('#container').mousedown(function (e) {
if(!isDown){
anchorX = e.pageX;
anchorY = e.pageY;
r = renderer.rect(anchorX, anchorY, 0, 0, 5)
.attr({
'stroke-width': 2,
stroke: 'blue',
fill: 'lightblue'
}).add();
}
isDown = !isDown;
});
// event handler for mousemove event
$('#container').mousemove(function (e) {
if (isDown) {
var x = r.attr('x'),
y = r.attr('y'),
w = r.attr('width'),
h = r.attr('height'),
newX,
newY,
newWidth,
newHeight;
newWidth = e.clientX - anchorX;
newHeight = e.clientY - anchorY;
// width & height cannot be negative so change x if negative
r.attr({
x: newWidth < 0 ? e.clientX : anchorX,
y: newHeight < 0 ? e.clientY : anchorY,
width: newWidth < 0 ? Math.abs(newWidth) : newWidth,
height: newHeight < 0 ? Math.abs(newHeight) : newHeight
});
}
});
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.