我是FabricJS库的新手,我将它用于以下方面, 我的屏幕左侧有一个可拖动的物体,只能在y方向上拖动。函数读出该对象的y位置,然后它取决于我需要绘制的三角形的高度。
但是在绘制三角形消息后2-3秒,并且不再显示....
这是我的代码。
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);
}
答案 0 :(得分:1)
拖动时是否正在制作新物体?我看不出它是代码的一部分。
你的意思是这样吗? &GT;&GT; 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();
}
答案 1 :(得分:0)
请看看这个工作小提琴。
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.");
}
}