0

我是 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);
}
4

2 回答 2

1

拖动时是否在制作新对象?我看不到它是代码的一部分。

你的意思是这样的吗??>>小提琴

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();
}
于 2013-04-23T16:52:20.507 回答
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.");
        }
    }
于 2016-02-29T10:29:10.163 回答