Draw canvas rect radius top left top right js năm 2024
I've recently started using the canvas tag and needed to draw rounded corner rectangles. I did find a good post explaing how to do it but I didn't find a nice method so I decided to create one. I took Futomi Hatano's code and abstracted it into a method. Show /
And it can be used like the following: // My html contains a canvas with id "rounded-rect" 500X350 var ctx = document.getElementById("rounded-rect").getContext("2d"); // Draw using default border radius, // stroke it but no fill (function's default values) roundRect(ctx, 5, 5, 50, 50); // To change the color on the rectangle, just manipulate the context ctx.strokeStyle = "rgb(255, 0, 0)"; ctx.fillStyle = "rgba(255, 255, 0, .5)"; roundRect(ctx, 100, 5, 100, 100, 20, true); // Manipulate it again ctx.strokeStyle = " 2d6";ctx.fillStyle = " abc";roundRect(ctx, 100, 200, 200, 100, 50, true); Which will produce the following output: For Firefox and Chrome (and maybe other browsers), you can do the following for syntactic sugar CanvasRenderingContext2D.prototype.roundRect = function(x, y, width, height, radius, fill, stroke) { if (typeof stroke == "undefined" ) { }
if (typeof radius === "undefined") { }
this.beginPath();
this.moveTo(x + radius, y);
this.lineTo(x + width - radius, y);
this.quadraticCurveTo(x + width, y, x + width, y + radius);
this.lineTo(x + width, y + height - radius);
this.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
this.lineTo(x + radius, y + height);
this.quadraticCurveTo(x, y + height, x, y + height - radius);
this.lineTo(x, y + radius);
this.quadraticCurveTo(x, y, x + radius, y);
this.closePath();
if (stroke) { }
if (fill) { }
}
// Now you can just call
var ctx = document.getElementById("rounded-rect").getContext("2d");
ctx.roundRect(5, 5, 50, 50);Here's a JsFiddle that you can edit freely QuestionWe would like to know how to draw rounded rectangle with bezier Curve. Answer
The code above is rendered as follows: How do you make a rounded rectangle in canvas?var canvas = $("canvas"). get(0);. var ctx = canvas. getContext("2d");. canvas. width = $(document). width();. canvas. height = $(document). height();. CanvasRenderingContext2D. prototype. roundRect = function (x, y, width, height, radius) {. if (width < 2 * radius) radius = width / 2;. How to use Android canvas to draw a rectangle with only topleft and topright corners round?If I want a 300x300 rect, with the top left and right rounded by 50 pixels, you can do: canvas. save(); canvas. clipRect(0, 0, 300, 300); canvas. How to draw rectangle in javascript canvas?To draw the rectangle onto a canvas, you can use the fill() or stroke() methods. Note: To both create and render a rectangle in one step, use the fillRect() or strokeRect() methods. How do I add a border radius in Canva?Adding borders and rounded corners to elements. Click on an image or video.. On the toolbar above the editor, click on Border style.. Choose a border style.. Click and drag the slider to adjust the border weight and/or corner rounding. You may also enter a value in the field next to it.. |