473,663 Members | 2,694 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Drawing ellipses with Canvas

Is there a right/best way to draw an ellipse using Canvas? (With VML
there's the v:oval element which makes the exercise trivial). I tried
drawing a 360 degree arc (a circle) and then scaling it but that also
scales the thickness of the circumference.

I'm trying bezier curves but without visual feedback it's pretty hard to
draw. And quadratic beziers give, at least with the control points I can
set, an oblate form. Whereas the only way I can set a cubic bezier's
control points is by guessing.

Andrew Poulos
Oct 18 '06 #1
5 5574

Andrew Poulos wrote:
Is there a right/best way to draw an ellipse using Canvas? (With VML
there's the v:oval element which makes the exercise trivial). I tried
drawing a 360 degree arc (a circle) and then scaling it but that also
scales the thickness of the circumference.

I'm trying bezier curves but without visual feedback it's pretty hard to
draw. And quadratic beziers give, at least with the control points I can
set, an oblate form. Whereas the only way I can set a cubic bezier's
control points is by guessing.

Andrew Poulos
See this document:
http://developer.apple.com/documenta...es/Canvas.html

Oct 18 '06 #2
Andrew Poulos wrote:
Is there a right/best way to draw an ellipse using Canvas? (With VML
there's the v:oval element which makes the exercise trivial). I tried
drawing a 360 degree arc (a circle) and then scaling it but that also
scales the thickness of the circumference.
This example
function addEllipsePath (context, cx, cy, r1, r2, startAngle, endAngle) {
context.save();
context.transla te(cx, cy);
context.scale(r 1, r2);
context.arc(0, 0, 1, startAngle, endAngle, 0);
context.restore ();
}

var canvas = document.create Element('canvas ');
canvas.width = 300;
canvas.height = 300;
canvas.style.bo rder = '1px solid green';

document.body.a ppendChild(canv as);

var context = canvas.getConte xt('2d');
context.strokeS tyle = 'rgba(255, 0, 0, 1)';
addEllipsePath( context, 150, 150, 30, 60, 0, Math.PI * 2);
context.lineWid th = 3;
context.stroke( );
works for me with Firefox 1.5 on Windows, but Opera 9.02 does not show
the ellipse. Please report back whether it works with Safari.

The idea of the above approach was taken from
<http://www.redgrittybr ick.org/postscript/respectively
<http://www.redgrittybr ick.org/postscript/ellipse.htmlwhi ch talks
about doing ellipses with Postscript.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 18 '06 #3
Martin Honnen wrote:
Andrew Poulos wrote:
>Is there a right/best way to draw an ellipse using Canvas? (With VML
there's the v:oval element which makes the exercise trivial). I tried
drawing a 360 degree arc (a circle) and then scaling it but that also
scales the thickness of the circumference.

This example
function addEllipsePath (context, cx, cy, r1, r2, startAngle, endAngle) {
context.save();
context.transla te(cx, cy);
context.scale(r 1, r2);
context.arc(0, 0, 1, startAngle, endAngle, 0);
context.restore ();
}

var canvas = document.create Element('canvas ');
canvas.width = 300;
canvas.height = 300;
canvas.style.bo rder = '1px solid green';

document.body.a ppendChild(canv as);

var context = canvas.getConte xt('2d');
context.strokeS tyle = 'rgba(255, 0, 0, 1)';
addEllipsePath( context, 150, 150, 30, 60, 0, Math.PI * 2);
context.lineWid th = 3;
context.stroke( );
Thanks, now I'm unsure why my ellipse experiment (of which I no longer
have a copy) caused the line width to scale when I scaled the circle.
works for me with Firefox 1.5 on Windows, but Opera 9.02 does not show
the ellipse. Please report back whether it works with Safari.
I don't own a Mac so it'll be a little while before I get to testing on
Safari.

One follow up question. I'm making the canvas fill the entire browser
window (because there'll be items drawn at various places about the
screen) but doing so means that I can't click "through" the transparent
part of the canvas to click buttons underneath. Is there a way to let
clicks pass through?

Andrew Poulos
Oct 19 '06 #4
Andrew Poulos wrote:
Thanks, now I'm unsure why my ellipse experiment (of which I no longer
have a copy) caused the line width to scale when I scaled the circle.
Because you probably did not save before doing the translation and
scaling and calling arc to then restore before you stroke.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 19 '06 #5
Martin Honnen wrote:

[cllipse in canvas]
works for me with Firefox 1.5 on Windows, but Opera 9.02 does not show
the ellipse. Please report back whether it works with Safari.
I have checked with Safari 2.0 myself and it nicely draws the ellipse
like Firefox.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 20 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2531
by: Leif K-Brooks | last post by:
I'm considering writing a fairly basic vector drawing program using PyGTK. What's the best way to render it? What I'm thinking of is having everything render on a PIL image, then display that in the GTK window or save it depending on what's being done. Is that the best strategy? Will it be fast enough?
2
11264
by: billy | last post by:
I'm working on a sort of paint program that has the usual canvas pane in the middle of the main form. I'm working on that class and have a couple quick questions.. actually more like I'm looking for opinions. 1) I want a ruler running along from top left to top right and from top left to bottom left. I want to use a skinny UserControl or something that runs the respective length. I could simply track the mouse and draw the little line on...
4
4515
by: Peter Oliphant | last post by:
There doesn't seem to be any documentation on how to create and/or use an instance of System::Drawing::Graphics. On-line MSDN talks about this class, and says: " The Graphics class provides methods for drawing objects to the display device. A Graphics object is associated with a specific device context. " Great. So how about explaining HOW to do this? Or perhaps a code example? Or at least a CONSTRUCTOR (or some explanation on how to...
2
1431
by: Martin Horn | last post by:
Hi all, I'm adding basic drawing capability to an application that I am writing, and I have been having problems with making the graphics output persist when the application is re-sized,covered etc. This is the solution I have come up with, but it seems a bit convoluted. Before I get too far into the development of the program, could someone cast an eye over my code and tell me if I have approached it in the correct way, or suggest a...
7
2173
by: Pontifex | last post by:
Hi all, I heard or read that the next version of HTML will provide long-overdue drawing commands. Which version of Firefox, Opera or IE supports such new fancy stuff? Thanks!
1
1785
by: Andrew | last post by:
Hello Everyone I am receiving an error in an application I am working on. The application when its done will be a Dungeons and Dragons Network game. I am having problems with the Networked Canvas basically for drawing the dungeon maps If I initialize two of the Tkinter Canvas widgets with in the same window I can draw across the network, however if I open a second instance of the Application and initialize the canvas, on windows I recv
4
3025
by: DM | last post by:
Hi All, I've recently been given the task of modifying some legacy VB 6 code to do with drawing rotated ellipses on a map at different zoom levels. I guess I could find a formula for drawing one manually using the DrawLine function or I could use the Windows graphics libraries to squash a perfect circle and rotate it... Is there an easier way to accomplish this or will I have to resort to manual
15
4021
by: javelin | last post by:
I need to be able to create a javascript based drawing/signature box, and be able to save it. Can someone refer me to a script that will allow this, or even a 3rd party package for sale? It can't be Java or ActiveX. Thank you.
7
3550
by: raylopez99 | last post by:
I have a logical drawing space much bigger than the viewport (the screen) and I'd like to center the viewport (the screen) to be at the center of the logical drawing space. After following the excellent transforms specified on Bob Powell's site, I still wonder if there's an easier way of centering it than the following procedure? Here is what I do now (it's awkward but it works): 1) I follow the transforms specified on Bob Powell's...
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,...
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...
0
7371
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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();...
1
2763
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 we have to send another system
2
2000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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.