I found that there are only can fill rectangle, but no rounded corner one, how can I do that?
相关问题
- Does using percentages instead of pixels change an
- Method for drawing circles in a pyramid pattern
- HTML5 - Canvas - Optimization for large images
- A function to preload images - need to draw them n
- Canvas fillStyle none in HTML5
相关文章
- HTML5 control
- canvas.toDataURL() not working properly [duplicate
- How to destroy / reload the canvas in html 5?
- Some Android devices extremely slow when rendering
- HTML5 Canvas: How to border a fillRect?
- Html over the Canvas?
- EaselJS Alpha Mask Filter
- Unable to Generate png Image from Html 5 Canvas
I needed to do the same thing and created a method to do it.
The HTML5 canvas doesn't provide a method to draw a rectangle with rounded corners.
How about using the
lineTo()
andarc()
methods?You can also use the
quadraticCurveTo()
method instead of thearc()
method.Here's one I wrote... uses arcs instead of quadratic curves for better control over radius. Also, it leaves the stroking and filling up to you
Here is an example:
To make the function more consistent with the normal means of using a canvas context, the canvas context class can be extended to include a '
fillRoundedRect
' method -- that can be called in the same wayfillRect
is called:The code checks to see if the prototype for the constructor for the canvas context object contains a '
fillRoundedRect
' property and adds one -- the first time around. It is invoked in the same manner as thefillRect
method:The method uses the
arcTo
method as Grumdring did. In the method,this
is a reference to thectx
object. The stroke argument defaults to false if undefined. The fill argument defaults to fill the rectangle if undefined.(Tested on Firefox, I don't know if all implementations permit extension in this manner.)
I started with @jhoff's solution, but rewrote it to use width/height parameters, and using
arcTo
makes it quite a bit more terse:Also returning the context so you can chain a little. E.g.:
The
drawPolygon
function below can be used to draw any polygon with rounded corners.See it running here.
The function receives an array with the polygon points, like this:
This is a port and a more generic version of a solution posted here.