Javascript examples for Canvas:Mouse
free draw Circle in fabricjs using mouse?
<html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.3.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.5.0/fabric.min.js"></script> <script type="text/javascript"> $(window).load(function(){//from w w w . j a va 2s . c o m var canvas = new fabric.Canvas("canvas2"); var circle, isDown, origX, origY; canvas.on('mouse:down', function(o){ isDown = true; var pointer = canvas.getPointer(o.e); origX = pointer.x; origY = pointer.y; circle = new fabric.Circle({ left: origX, top: origY, originX: 'left', originY: 'top', radius: pointer.x-origX, angle: 0, fill: '', stroke:'red', strokeWidth:3, }); canvas.add(circle); }); canvas.on('mouse:move', function(o){ if (!isDown) return; var pointer = canvas.getPointer(o.e); var radius = Math.max(Math.abs(origY - pointer.y),Math.abs(origX - pointer.x))/2; if (radius > circle.strokeWidth) { radius -= circle.strokeWidth/2; } circle.set({ radius: radius}); if(origX>pointer.x){ circle.set({originX: 'right' }); } else { circle.set({originX: 'left' }); } if(origY>pointer.y){ circle.set({originY: 'bottom' }); } else { circle.set({originY: 'top' }); } canvas.renderAll(); }); canvas.on('mouse:up', function(o){ isDown = false; }); }); </script> </head> <body> <canvas id="canvas2" width="500" height="500" style="height:500px;width:500px;"></canvas> </body> </html>