Reputation: 419
I'm new with the FabricJS library and I used it for the following, I have a draggable object on the left side of my screen, which is only drag-able in an y direction. A function reads out the y position of this object and then it depends the height of my triangle which needs to be drawn.
But after 2-3 seconds of drawing the triangle dissapears and doesn't show again....
This is my code.
function drawObject() {
if (being_dragged == true && y <= 440) {
document.getElementById(element).style.top = y + 'px';
y_height_1 = y;
if (y_height_1 > y_height_2) {
y_final--;
y_height_2 = y_height_1;
} else {
y_final++;
}
drawRoof();
}
}
function drawRoof() {
var canvas = new fabric.Canvas('canvas');
var roof = new fabric.Triangle({
width: 200,
height: y_final / 2,
fill: 'blue',
left: 150,
top: 200
});
canvas.add(roof);
}
Upvotes: 0
Views: 1075
Reputation: 3548
Please take a look on this working fiddle.
http://jsfiddle.net/Ahammadalipk/w8kkc/185/
window.onload = function () {
var canvas = new fabric.Canvas('canvas');
/*
NOTE: the start and end handlers are events for the <img> elements; the rest are bound to
the canvas container.
*/
function handleDragStart(e) {
[].forEach.call(images, function (img) {
img.classList.remove('img_dragging');
});
this.classList.add('img_dragging');
}
function handleDragOver(e) {
if (e.preventDefault) {
e.preventDefault();
}
e.dataTransfer.dropEffect = 'copy';
return false;
}
function handleDragEnter(e) {
this.classList.add('over');
}
function handleDragLeave(e) {
this.classList.remove('over');
}
function handleDrop(e) {
if (e.stopPropagation) {
e.stopPropagation(); // stops the browser from redirecting.
}
var img = document.querySelector('#images img.img_dragging');
var newImage = new fabric.Image(img, {
width: img.width,
height: img.height,
// Set the center of the new object based on the event coordinates relative
// to the canvas container.
left: e.layerX,
top: e.layerY
});
newImage.hasControls = newImage.hasBorders = false;
canvas.add(newImage);
return false;
}
function handleDragEnd(e) {
// this/e.target is the source node.
[].forEach.call(images, function (img) {
img.classList.remove('img_dragging');
});
}
if (Modernizr.draganddrop) {
var images = document.querySelectorAll('#images img');
[].forEach.call(images, function (img) {
img.addEventListener('dragstart', handleDragStart, false);
img.addEventListener('dragend', handleDragEnd, false);
});
var canvasContainer = document.getElementById("canvas-container");
canvasContainer.addEventListener('dragenter', handleDragEnter, false);
canvasContainer.addEventListener('dragover', handleDragOver, false);
canvasContainer.addEventListener('dragleave', handleDragLeave, false);
canvasContainer.addEventListener('drop', handleDrop, false);
} else {
alert("This browser doesn't support the HTML5 Drag and Drop API.");
}
}
Upvotes: 0
Reputation: 3746
Are you making a new object when you are dragging? I cant see its a part of the code.
Do you mean something like this?? >> Fiddle
var canvas = new fabric.Canvas('canvas');
var roof = new fabric.Triangle({ width: 200, height: 200, fill: 'blue', left: 250, top: 200 });
var handler = new fabric.Circle({ radius: 15, fill: '#ccc', top: 300, left: 40 });
handler.hasControls = handler.hasBorders = false;
handler.lockMovementX = true;
canvas.add(roof, handler);
canvas.on({'object:moving' : resizeTriangle});
function resizeTriangle(e) {
roof.height = e.target.getTop();
}
Upvotes: 1