create fabricjs custom bounding box -
i used fabric.circle
create semi circle. not able set bounding box. how can set bounding box cover whole semi circle.
$(document).ready(function() { //getting canvas var canvas = new fabric.canvas("canvas"); var pointarray = []; var semicircle, ismousedown = false; $('#selection').click(function() { canvas.selection = true; changestatus(true); //remove mouse event canvas.off('mouse:down', onmousedown); canvas.off('mouse:move', onmousemove); canvas.off('mouse:up', onmouseup); }) function changestatus(value) { canvas.foreachobject(function(obj) { obj.selectable = value; }) canvas.renderall(); } $('#semicircle').click(function() { canvas.selection = false; changestatus(false); //register mouse event canvas.on('mouse:down', onmousedown); canvas.on('mouse:move', onmousemove); canvas.on('mouse:up', onmouseup); }); function onmousedown(event) { //defining procedure ismousedown = true; var pointer = canvas.getpointer(event.e); //creating semicircle object if (pointarray.length < 1) pointarray.push(pointer); if (pointarray.length == 1) { semicircle = new fabric.circle({ radius: 0, stroke:'magenta', fill: '', left: pointer.x, top: pointer.y, originx: 'center', originy: 'center', selectable: false, }); canvas.add(semicircle); } } function onmousemove(options) { // defining procedure if (!ismousedown) { return; } if (semicircle != null) { var pointer = canvas.getpointer(options.e); pointarray[1] = pointer; var radius = parseint(math.sqrt(math.pow((pointarray[1].x - pointarray[0].x), 2) + math.pow((pointarray[1].y - pointarray[0].y), 2))); var centerx = (pointarray[1].x + pointarray[0].x) / 2; centery = (pointarray[1].y + pointarray[0].y) / 2, dx1 = pointarray[1].x - pointarray[0].x, dy1 = pointarray[1].y - pointarray[0].y, firstangle = math.atan2(dy1, dx1), secondangle = firstangle + math.pi; semicircle.set({ left: centerx, top: centery, radius: radius / 2, startangle: firstangle, endangle: secondangle, endpoint: pointer }); semicircle.setcoords(); canvas.renderall(); } } function onmouseup() { //alert("mouse up!"); ismousedown = false; if (pointarray.length == 2) { semicircle = null; canvas.renderall(); pointarray = []; } } });
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.18/fabric.js" charset="utf-8"></script> <script src="https://code.jquery.com/jquery-3.2.1.js" charset="utf-8"></script> <input type="button" id="semicircle" value="semicircle"> <input type="button" id="selection" value="selection"><br> <canvas id="canvas" width=500 height=500 style="height:500px;width:500px;"></canvas>
Comments
Post a Comment