Last active
April 24, 2017 05:03
-
-
Save zjaml/78b926af6c37f6474fe64b23c6764c36 to your computer and use it in GitHub Desktop.
Fiddle here https://jsfiddle.net/6e17oxc3/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var canvas = this.__canvas = new fabric.Canvas('c'); | |
canvas.selection = false; | |
function drawArrow(fromx, fromy, tox, toy) { | |
var angle = Math.atan2(toy - fromy, tox - fromx); | |
var headlen = 15; // arrow head size | |
// bring the line end back some to account for arrow head. | |
tox = tox - (headlen) * Math.cos(angle); | |
toy = toy - (headlen) * Math.sin(angle); | |
// calculate the points. | |
var points = [ | |
{ | |
x: fromx, // start point | |
y: fromy | |
}, { | |
x: fromx - (headlen / 4) * Math.cos(angle - Math.PI / 2), | |
y: fromy - (headlen / 4) * Math.sin(angle - Math.PI / 2) | |
},{ | |
x: tox - (headlen / 4) * Math.cos(angle - Math.PI / 2), | |
y: toy - (headlen / 4) * Math.sin(angle - Math.PI / 2) | |
}, { | |
x: tox - (headlen) * Math.cos(angle - Math.PI / 2), | |
y: toy - (headlen) * Math.sin(angle - Math.PI / 2) | |
},{ | |
x: tox + (headlen) * Math.cos(angle), // tip | |
y: toy + (headlen) * Math.sin(angle) | |
}, { | |
x: tox - (headlen) * Math.cos(angle + Math.PI / 2), | |
y: toy - (headlen) * Math.sin(angle + Math.PI / 2) | |
}, { | |
x: tox - (headlen / 4) * Math.cos(angle + Math.PI / 2), | |
y: toy - (headlen / 4) * Math.sin(angle + Math.PI / 2) | |
}, { | |
x: fromx - (headlen / 4) * Math.cos(angle + Math.PI / 2), | |
y: fromy - (headlen / 4) * Math.sin(angle + Math.PI / 2) | |
},{ | |
x: fromx, | |
y: fromy | |
} | |
]; | |
var pline = new fabric.Polyline(points, { | |
fill: 'white', | |
stroke: 'black', | |
opacity: 1, | |
strokeWidth: 2, | |
originX: 'left', | |
originY: 'top', | |
selectable: true | |
}); | |
canvas.add(pline); | |
canvas.renderAll(); | |
} | |
//draw an arrow! | |
drawArrow(100, 100, 150, 150); | |
// click and drag to draw more arrow! | |
var startX, startY, endX, endY; | |
canvas.on('mouse:down', function() { | |
var pointer = canvas.getPointer(event.e); | |
startX = pointer.x; | |
startY = pointer.y; | |
}); | |
canvas.on('mouse:up', function() { | |
var pointer = canvas.getPointer(event.e); | |
endX = pointer.x; | |
endY = pointer.y; | |
drawArrow(startX, startY, endX, endY); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment