JSFiddle - React, Tailwind, and code Playground
by varun_1995
HTML
<canvas id="c" width="300" height="300" style="border:1px solid black;"></canvas>
<div id="mydiv">
<div id="mydivheader">
<input type="checkbox" id="hasControls" checked="">hasControls<br> <input type="checkbox" id="visible" checked="">visible<br>
</div>
</div>
CSS
#mydiv {
position: absolute;
z-index: 9;
background-color: #f1f1f1;
border: 1px solid #d3d3d3;
}
JavaScript
(function() {
var canvas = this.__canvas = new fabric.Canvas('c');
var circle = new fabric.Circle({
radius: 20,
fill: 'green',
left: 20,
top: 20,
transparentCorners: false
})
var rect = new fabric.Rect({
left: 50,
top: 80,
originX: 'left',
originY: 'top',
width: 150,
height: 120,
angle: -10,
fill: 'rgba(255,0,0,0.5)',
//visible: false,
transparentCorners: false
});
canvas.add(rect,circle).setActiveObject(circle);
function observeBoolean(property) {
if(canvas.getActiveObject().get('type')==="rect"){
document.getElementById(property).onclick = function() {
canvas.item(0)[property] = this.checked;
canvas.renderAll();
};
}
else{
document.getElementById(property).onclick = function() {
canvas.item(1)[property] = this.checked;
canvas.renderAll();
};
}
}
observeBoolean('hasControls');
observeBoolean('visible');
dragElement(document.getElementById("mydiv"));
function dragElement(elmnt) {
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
if (document.getElementById(elmnt.id + "header")) {
/* if present, the header is where you move the DIV from:*/
document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
} else {
/* otherwise, move the DIV from anywhere inside the DIV:*/
elmnt.onmousedown = dragMouseDown;
}
function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
// get the mouse cursor position at startup:
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
// call a function whenever the cursor moves:
document.onmousemove = elementDrag;
}
function elementDrag(e) {
e = e || window.event;
e.preventDefault();
// calculate the new cursor position:
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
// set the element's new position:
elmnt.style.top =...