473,663 Members | 2,743 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 1805

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

Similar topics

5
6220
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 buttons, which is fine most of the time (he comes from a Mac environment, so I accept mouse clicks from the right or left button when he's working on the PC). But every once in a while, he'll press and hold the right mouse button, move onto a JButton,...
1
2471
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 using a couple of 'div' layers including transparent gifs in 'higher' zIndex layers to create the circular 'window' to the lower layers. But now I am learning that although I can see the HTML on the lower layer, the transparent gif layer above it...
3
3203
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 container form) or if the MDI child form's Visible property is set to False (after the MDI child form was shown). This is an enormous problem for my app because I must show different MDI child forms based on the state of my application and many of the...
3
2066
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 forecolor, backcolor and font. The labels also are rotatable and the text can also be flipped 180 degrees (the flipped text part is still being worked on). I have one context menu for all 30 labels that allows for the property changes to the labels....
2
15445
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 events allowing the user control to react to the mouse click. Setting the Enabled property on the label to False comes close, but I don't want the font color to change. Does anyone have an idea how .NET implements the code behind the Enabled...
1
1455
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 example getting all the mouse events?
1
2168
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
3469
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 mouse events of course) within a picturebox control? Thanks
0
8436
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8858
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8548
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8634
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6186
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5657
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4182
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1757
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.