Fares Omrani
Fares Omrani

Reputation: 255

Javascript drag & drop canvas with limits

I create a page that makes me move a canvas (by changing X and Y position). This is my code that works: https://jsfiddle.net/rrmwub4h/1/

var canvas = document.getElementById("id1");
var ctx = canvas.getContext("2d");
ctx.font = "30px Arial";
ctx.fillText("Drag and drop me",10,50);

document.getElementById('id1').onmousedown = function () {
    _drag_init(this);
    return false;
};
document.onmousemove = _move_elem;
document.onmouseup = _destroy;

var selected = null, // Object of the element to be moved
x_pos = 0, y_pos = 0, // Stores x & y coordinates of the mouse pointer
x_elem = 0, y_elem = 0; // Stores top, left values (edge) of the element
function _drag_init(elem) {
    selected = elem;
    x_elem = x_pos - selected.offsetLeft;
    y_elem = y_pos - selected.offsetTop;
}
function _move_elem(e) {
    x_pos = document.all ? window.event.clientX : e.pageX;
    y_pos = document.all ? window.event.clientY : e.pageY;
    if (selected !== null) {
        selected.style.left = (x_pos - x_elem) + 'px';
        selected.style.top = (y_pos - y_elem) + 'px';
    }
}
// Destroy the object when we are done
function _destroy() {
    selected = null;
}

But I wanna make limits for X and Y positions when I move the canvas (for exemple varies between 0 and 300px and not surpass the limit of 300px)

How can I add this conditon to my code ?

Upvotes: 0

Views: 1272

Answers (1)

Canvas
Canvas

Reputation: 5897

jsFiddle https://jsfiddle.net/CanvasCode/rrmwub4h/2/

var canvas = document.getElementById("id1");
var ctx = canvas.getContext("2d");
ctx.font = "30px Arial";
ctx.fillText("Drag and drop me",10,50);

document.getElementById('id1').onmousedown = function () {
    _drag_init(this);
    return false;
};
document.onmousemove = _move_elem;
document.onmouseup = _destroy;

var selected = null, // Object of the element to be moved
x_pos = 0, y_pos = 0, // Stores x & y coordinates of the mouse pointer
x_elem = 0, y_elem = 0; // Stores top, left values (edge) of the element
// Will be called when user starts dragging an element
function _drag_init(elem) {
    // Store the object of the element which needs to be moved
    selected = elem;
    x_elem = x_pos - selected.offsetLeft;
    y_elem = y_pos - selected.offsetTop;
    //Ext.getCmp("GridMarkeurs").getSelectionModel().select(cmp, true);
}
function _move_elem(e) {
    x_pos = document.all ? window.event.clientX : e.pageX;
    y_pos = document.all ? window.event.clientY : e.pageY;
    if (selected !== null) {
        selected.style.left = (x_pos - x_elem) + 'px';
        selected.style.top = (y_pos - y_elem) + 'px';
    }

    if(x_pos >= 300)
    {
        selected.style.left = "300px";
    }

    if(y_pos >= 300)
    {
        selected.style.top = "300px";
    }

}
// Destroy the object when we are done
function _destroy() {
    selected = null;
}

Just add an if statement inside of your move_elem function to see if the x_pos or y_pos is past a value.

Upvotes: 1

Related Questions