473,508 Members | 2,367 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Drawing circle/ellipse on HTML5 canvas using mouse events

23 New Member
I want something like ellipse option in paint for drawing on my canvas. I have achieved this partially. The problem is i am not able to get radius of circle, currently i have hard coded it to 15. Also i want to draw ellipse(same as paint) not exact circle. This is my code for drawing circle on canvas using mouse events.Please help me with code to achieve my above mentioned requirements.
Expand|Select|Wrap|Line Numbers
  1. function tool_circle() {
  2.         var tool = this;
  3.         this.started = false;
  4.  
  5.         this.mousedown = function (ev) {
  6.             tool.started = true;
  7.             tool.x0 = ev._x;
  8.             tool.y0 = ev._y;
  9.         };
  10.  
  11.         this.mousemove = function (ev) {
  12.             if (!tool.started) {
  13.                 return;
  14.             }
  15.  
  16.             context.fillStyle = 'red';
  17.  
  18.             var distance = Math.sqrt(Math.pow(tool.x0 - ev._x, 2) + Math.pow(tool.y0 - ev._y));
  19.             context.beginPath();
  20.  
  21.             context.arc(tool.x0, tool.y0,15, 0, Math.PI * 2, false);
  22.             context.stroke();
  23.             context.fill();
  24.         };
  25.  
  26.         this.mouseup = function (ev) {
  27.             if (tool.started) {
  28.                 tool.mousemove(ev);
  29.                 tool.started = false;
  30.                 img_update();
  31.             }
  32.         };
  33.     }
Feb 6 '14 #1
0 1787

Sign in to post your reply or Sign up for a free account.

Similar topics

5
6200
by: John Champaign | last post by:
Hi all, I'm working on an educational applet for a child with special needs. He's got a bit of a trick to make my life more difficult... To interact with the applet he needs to click on...
1
2463
by: Eric Petruzzelli | last post by:
My application will utilize touch screens. I am using mouse events to Drag&Drop data. Will the mouse events be effective using a touchscreen? Or do I need to look into other methods? Eric.
2
2058
by: Catherine Lynn Smith | last post by:
I have been playing with a page design that would use a 'circular' area in the middle rather than the standard boring square box world of tables and frames. I have the design itself layed out...
3
3195
by: Lance | last post by:
I've noticed that controls that are contained in MDI child forms fail to raise MouseLeave events if the MDI child form's MdiParent property is set to Nothing (after it was set to an existing MDI...
3
2055
by: jcrouse | last post by:
I have created a form designer type application (with a lot of you peoples helpJ). It has label controls that are draggable at runtime. The user is also allowed to change some properties such as...
2
15413
by: bretth | last post by:
In a VB.Net Windows Forms application, I have a user control that handles mouse events. Another section of code programmatically adds a label to the control. I would like label to ignore all...
1
1450
by: zon7 | last post by:
I want my program to capture a part of the screen by drawing a rectangle with the mouse. But as I click the mouse out of the form, the program lost the focus. Is there any way to do this? For...
1
2166
by: sravan_reddy001 | last post by:
do we hav any methods defined in C# to handle all the mouse events like ->mouseover ->mousemoved ->mousedragged i just want to create a drawing box....
2
3449
by: kimiraikkonen | last post by:
Hi, I want to crop and save specified image area which is in a picturebox with freehand using cursor, using mouseDown, mouseUp and mouseMove events. How can crop an image with freehand (using...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7115
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7489
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5624
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1547
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.