2017-01-24 6 views
1

Ich arbeite an einem Projekt mit Fabric js. Ich habe versucht, mein Problem zu minimieren, also hoffe ich, dass der Code nicht zu durcheinander ist. Ich schaffe einige Objekte, die miteinander verknüpft sind:FabricJS optisches Aussehen von Linien

  • A-Linie, die ein Start enthält und einen Endpunkt
  • Ein Kreis, der Startpoint 1 Zeile und Endpunkt einer anderen Leitung ist

mit dieser kombination kann ich verschiedene formen (wie ein polygon) erzeugen und auch meine move-funktionen für sie verändern.

Wenn ein Kreis gezogen wird, werden die zugehörigen Linien skaliert und verschoben. (in meinem Code können Sie die Linien auch verschieben und die Form wird danach geändert, aber ich habe es nicht in dieses Beispiel, BC dieser kurze Auszug sollte genug sein, um zu zeigen, was mein Problem ist.)

Ich habe ein wenig Beispiel in jsfiddle: https://jsfiddle.net/bxgox7cr/

Wenn Sie an dem Enden der Linien anschaut, kann man deutlich einen Schnitt sehen, so bald das Auge erkennen, dass dies nicht eine zusammenhängende Form, sondern vielmehr einige Linien sind, die jeder der Nähe andere. Gibt es eine Möglichkeit, das Aussehen der Linien so zu verändern, dass die Form "geschlossen" aussieht?

Hier mein Code ist, habe ich versucht, einige Kommentare zu setzen, dass es leicht zu lesen ist:

var canvas = new fabric.Canvas('canvas'); 

fabric.Object.prototype.originX = fabric.Object.prototype.originY = 'center'; 
document.getElementById("canvas").tabIndex = 1000; 

/** ------------creating a Line Object, which contains a start and an endpoint ------------**/ 
fabric.LineWithPoints = fabric.util.createClass(fabric.Line, { 
    initialize: function(points, options) { 
    options || (options = {}); 
    this.callSuper('initialize', points, options); 
    options && 
     this.set('type', options.type), 
     this.set('name', options.name), 
     this.set('start_point', options.start_point), 
     this.set('end_point', options.end_point), 
     this.set('current_x', options.current_x), 
     this.set('current_y', options.current_y) 
    }, 
    setStartPointAndEndPoint: function(start_point, end_point) { 
    this.set({ 
     start_point: start_point, 
     end_point: end_point 
    }); 
    }, 
    setValues: function(new_x1, new_y1, new_x2, new_y2) { 
    //  console.log(this); 
    this.set({ 
     x1: new_x1, 
     x2: new_x2, 
     y1: new_y1, 
     y2: new_y2 
    }); 
    this.setCoords(); 
    } 
}); 

/**--- modifie the circle element, adding new functions for the movement of the object-------*/ 
fabric.LinePoint = fabric.util.createClass(fabric.Circle, { 
    initialize: function(options) { 
    options || (options = {}); 
    this.callSuper('initialize', options); 
    options && 
     this.set('subtype', 'line_point'), 
     this.set('x', this.left), 
     this.set('y', this.top) 
    }, 
    setPointCoordinates: function(new_left, new_top) { 
    this.set({ 
     x: new_left, 
     y: new_top, 
     left: new_left, 
     top: new_top 
    }); 
    this.setCoords(); 
    }, 
    move: function(new_left, new_top) { 
    var wall_1 = this.line1; 
    var wall_2 = this.line2; 

    this.setPointCoordinates(new_left, new_top); 
    wall_1.setValues(wall_1.x1, wall_1.y1, this.getLeft(), this.getTop()); 
    wall_2.setValues(this.getLeft(), this.getTop(), wall_2.x2, wall_2.y2); 
    canvas.renderAll(); 
    }, 
}); 

/**------------------- Moving Function------------------------------------------------- */ 

canvas.on('object:moving', function(event) { 
    var object = event.target; 

    if (object.subtype == "line_point") { 
    object.move(object.getLeft(), object.getTop()); 
    } 
}); 


/**------------------------------ create functions for the objects -----------------------*/ 

function newCircleObject(left, top, wall_1, wall_2) { 
    var circle = new fabric.LinePoint({ 
    left: left, 
    top: top, 
    strokeWidth: 2, 
    radius: 15, 
    fill: 'grey', 
    stroke: 'black', 
    opacity: 0.1, 
    perPixelTargetFind: true, 
    subtype: 'line_point', 
    includeDefaultValues: false 
    }); 

    circle.hasControls = false; 
    circle.hasBorders = false; 
    circle.line1 = wall_1; 
    circle.line2 = wall_2; 

    return circle; 
} 

function newWallObject(coords) { 
    var wall = new fabric.LineWithPoints(coords, { 
    stroke: 'black', 
    strokeWidth: 6, 
    lockScalingX: true, 
    lockScalingY: true, 
    perPixelTargetFind: true, 
    subtype: 'line', 
    type: 'line', 
    padding: 10, 
    includeDefaultValues: false 
    }); 

    wall.hasControls = false; 
    wall.hasBorders = false; 
    return wall; 
} 


/**------------------------------ adding the shapes--------------------------------*/ 

var wall_1 = newWallObject([100, 100, 100, 500]); 
var wall_2 = newWallObject([100, 500, 500, 500]); 
var wall_3 = newWallObject([500, 500, 500, 100]); 
var wall_4 = newWallObject([500, 100, 100, 100]); 

var end_point_1 = newCircleObject(wall_1.x1, wall_1.y1, wall_4, wall_1); 
var end_point_2 = newCircleObject(wall_2.x1, wall_2.y1, wall_1, wall_2); 
var end_point_3 = newCircleObject(wall_3.x1, wall_3.y1, wall_2, wall_3); 
var end_point_4 = newCircleObject(wall_4.x1, wall_4.y1, wall_3, wall_4); 

wall_1.setStartPointAndEndPoint(end_point_1.name, end_point_2.name); 
wall_2.setStartPointAndEndPoint(end_point_2.name, end_point_3.name); 
wall_3.setStartPointAndEndPoint(end_point_3.name, end_point_4.name); 
wall_4.setStartPointAndEndPoint(end_point_4.name, end_point_1.name); 

canvas.add(wall_1, wall_2, wall_3, wall_4, end_point_1, end_point_2, end_point_3, end_point_4); 

Antwort

1

strokeLineCap: 'round', hinzufügen:

function newWallObject(coords) { 
    var wall = new fabric.LineWithPoints(coords, { 
    stroke: 'black', 
    strokeWidth: 6, 
    lockScalingX: true, 
    lockScalingY: true, 
    perPixelTargetFind: true, 
    strokeLineCap: 'round', 
    subtype: 'line', 
    type: 'line', 
    padding: 10, 
    includeDefaultValues: false 
    }); 

    wall.hasControls = false; 
    wall.hasBorders = false; 
    return wall; 
} 

ich aufsah: http://fabricjs.com/docs/fabric.Object.html#strokeLineCap