HTML कैनवास पर एक गोल आयत कैसे बनाएं?


140

मैंने पाया कि केवल आयत भर सकते हैं, लेकिन कोई गोल कोने वाला नहीं, मैं ऐसा कैसे कर सकता हूं?


1
इस आदमी ने एक अच्छी विधि में गोल आयतों (भरा हुआ, सीमा के साथ ...) बनाने के लिए कैसे रखा: js-bits.blogspot.com/2010/07/…
nerdess

जवाबों:


47

एचटीएमएल 5 कैनवास गोल कोनों के साथ एक आयत बनाने के लिए एक विधि प्रदान नहीं करता है।

कैसे lineTo()और arc()तरीकों का उपयोग करने के बारे में ?

आप quadraticCurveTo()विधि के बजाय विधि का उपयोग भी कर सकते हैं arc()


किसी कारण के लिए, मैं फ़ायरफ़ॉक्स 3.5 और ओपेरा 10.0 में आर्कटू के साथ समस्या हो रहा है। इस साइट के समान: ditchnet.org/canvas/CanvasRoundedCornerExample.html
bgw

एफटीएफ के नवीनतम संस्करण में आर्कटॉ तय किया गया है।
ऐश ब्लू

3
क्या आप एक उदाहरण प्रदान कर सकते हैं?
जीन-पियरे बेकोट

324

मुझे एक ही काम करने की जरूरत थी और इसे करने के लिए एक तरीका बनाया।

// Now you can just call
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 = "#0f0";
ctx.fillStyle = "#ddd";
// Different radii for each corner, others default to 0
roundRect(ctx, 300, 5, 200, 100, {
  tl: 50,
  br: 25
}, true);

/**
 * Draws a rounded rectangle using the current state of the canvas.
 * If you omit the last three params, it will draw a rectangle
 * outline with a 5 pixel border radius
 * @param {CanvasRenderingContext2D} ctx
 * @param {Number} x The top left x coordinate
 * @param {Number} y The top left y coordinate
 * @param {Number} width The width of the rectangle
 * @param {Number} height The height of the rectangle
 * @param {Number} [radius = 5] The corner radius; It can also be an object 
 *                 to specify different radii for corners
 * @param {Number} [radius.tl = 0] Top left
 * @param {Number} [radius.tr = 0] Top right
 * @param {Number} [radius.br = 0] Bottom right
 * @param {Number} [radius.bl = 0] Bottom left
 * @param {Boolean} [fill = false] Whether to fill the rectangle.
 * @param {Boolean} [stroke = true] Whether to stroke the rectangle.
 */
function roundRect(ctx, x, y, width, height, radius, fill, stroke) {
  if (typeof stroke === 'undefined') {
    stroke = true;
  }
  if (typeof radius === 'undefined') {
    radius = 5;
  }
  if (typeof radius === 'number') {
    radius = {tl: radius, tr: radius, br: radius, bl: radius};
  } else {
    var defaultRadius = {tl: 0, tr: 0, br: 0, bl: 0};
    for (var side in defaultRadius) {
      radius[side] = radius[side] || defaultRadius[side];
    }
  }
  ctx.beginPath();
  ctx.moveTo(x + radius.tl, y);
  ctx.lineTo(x + width - radius.tr, y);
  ctx.quadraticCurveTo(x + width, y, x + width, y + radius.tr);
  ctx.lineTo(x + width, y + height - radius.br);
  ctx.quadraticCurveTo(x + width, y + height, x + width - radius.br, y + height);
  ctx.lineTo(x + radius.bl, y + height);
  ctx.quadraticCurveTo(x, y + height, x, y + height - radius.bl);
  ctx.lineTo(x, y + radius.tl);
  ctx.quadraticCurveTo(x, y, x + radius.tl, y);
  ctx.closePath();
  if (fill) {
    ctx.fill();
  }
  if (stroke) {
    ctx.stroke();
  }

}
<canvas id="rounded-rect" width="500" height="200">
  <!-- Insert fallback content here -->
</canvas>


2
बिल्कुल सही जवाब ... यह कैनवास के मूल निवासी कैसे नहीं है? धन्यवाद।
andygoestohollywood

1
कोड में एक बग होता है, उसे स्ट्रोक को भरने के लिए स्ट्रोक करने की आवश्यकता होती है, अन्यथा छोटी आयतों पर भराव स्ट्रोक को अधिलेखित कर देगा।
जिग मंडेल

2
मेरे पास उदाहरण नहीं है, लेकिन मुझे उस आदेश के लिए उस आदेश को संशोधित करना पड़ा जिसे मैंने अपने कोड पर परीक्षण किया था। इसका तार्किक, यह सही ढंग से स्ट्रोक कैसे कर सकता है (यदि रूट पृष्ठभूमि रंग का उपयोग करके चौरसाई के साथ) यदि आप अभी तक सुधार कर चुके हैं तो यह है?
जिग मेंडल

2
@ जुआन हे मेरा बुरा, मैंने ब्लॉग पोस्ट पर गौर किया और उसके बाद उस ख़बर को पकड़ा। मेरा मतलब संपादन को पूर्ववत करना था। गुडजॉब मैन + 1 यू यू!
Man

6
जिग मंडेल सही है: इसे भरा जाना चाहिए और फिर स्ट्रोक किया जाना चाहिए। कारण यह है कि यदि आप स्ट्रोक करते हैं और फिर भरते हैं तो लाइन की चौड़ाई आधी हो जाती है। इसे वास्तव में मोटी लाइन चौड़ाई (कहना, 20) के साथ आज़माएं और एक गोल आयत की तुलना करें जो पृष्ठभूमि रंग के साथ एक गोल आयत से भरा है जो भरा नहीं है। भरे हुए की लाइन की चौड़ाई, भरी हुई नहीं की लाइन की चौड़ाई की आधी होगी।
एंड्रयू स्टेसी

106

मैंने @ झॉफ के समाधान के साथ शुरुआत की थी, लेकिन इसे चौड़ाई / ऊंचाई के मापदंडों का उपयोग करने के लिए फिर से लिखा था, और इसका उपयोग करने arcToसे यह थोड़ा अधिक स्पष्ट हो जाता है:

CanvasRenderingContext2D.prototype.roundRect = function (x, y, w, h, r) {
  if (w < 2 * r) r = w / 2;
  if (h < 2 * r) r = h / 2;
  this.beginPath();
  this.moveTo(x+r, y);
  this.arcTo(x+w, y,   x+w, y+h, r);
  this.arcTo(x+w, y+h, x,   y+h, r);
  this.arcTo(x,   y+h, x,   y,   r);
  this.arcTo(x,   y,   x+w, y,   r);
  this.closePath();
  return this;
}

संदर्भ को वापस भी कर सकते हैं ताकि आप थोड़ी सी श्रृंखला बना सकें। उदाहरण के लिए:

ctx.roundRect(35, 10, 225, 110, 20).stroke(); //or .fill() for a filled rect

4
मैं कैनवस प्रतिपादन के संदर्भ में गड़बड़ नहीं करूंगा, केवल उस अच्छे समाधान को छोड़कर।
ऐश ब्लू

इस समाधान के साथ समस्या यह है कि आप स्वतंत्र रूप से प्रत्येक कोने के लिए त्रिज्या को नियंत्रित नहीं कर सकते हैं। पर्याप्त लचीला नहीं है। मेरा समाधान नीचे देखें।
कोर्गोरल

1
यह एक केंद्रित आयत है, अगर किसी को अपने शीर्ष बाएं कोने में एक की आवश्यकता होती है, तो (x,y)संदर्भ को सहेजें, अनुवाद जोड़ें (-w/2,-h/2)और संदर्भ को पुनर्स्थापित करें।
nessa.gp

धन्यवाद, यह केवल एक है जिसने मेरे लिए काम किया है इस प्रकार अब तक, मुझे अंक देने वाले अन्य लोग जब त्रिज्या ऊंचाई या चौड़ाई से अधिक या बड़े थे। लागू किया!
होवीजिएक

1
ध्यान दें कि यह समाधान किसी भी बहुभुज को गोल कोने बनाने के लिए काम करता है। एक बेला
डॉगुलेज़

23

जुआन, मैंने प्रत्येक आयत कोने के त्रिज्या को व्यक्तिगत रूप से बदलने की अनुमति देने के लिए आपकी विधि में थोड़ा सुधार किया:

/** 
 * Draws a rounded rectangle using the current state of the canvas.  
 * If you omit the last three params, it will draw a rectangle  
 * outline with a 5 pixel border radius  
 * @param {Number} x The top left x coordinate 
 * @param {Number} y The top left y coordinate  
 * @param {Number} width The width of the rectangle  
 * @param {Number} height The height of the rectangle 
 * @param {Object} radius All corner radii. Defaults to 0,0,0,0; 
 * @param {Boolean} fill Whether to fill the rectangle. Defaults to false. 
 * @param {Boolean} stroke Whether to stroke the rectangle. Defaults to true. 
 */
CanvasRenderingContext2D.prototype.roundRect = function (x, y, width, height, radius, fill, stroke) {
    var cornerRadius = { upperLeft: 0, upperRight: 0, lowerLeft: 0, lowerRight: 0 };
    if (typeof stroke == "undefined") {
        stroke = true;
    }
    if (typeof radius === "object") {
        for (var side in radius) {
            cornerRadius[side] = radius[side];
        }
    }

    this.beginPath();
    this.moveTo(x + cornerRadius.upperLeft, y);
    this.lineTo(x + width - cornerRadius.upperRight, y);
    this.quadraticCurveTo(x + width, y, x + width, y + cornerRadius.upperRight);
    this.lineTo(x + width, y + height - cornerRadius.lowerRight);
    this.quadraticCurveTo(x + width, y + height, x + width - cornerRadius.lowerRight, y + height);
    this.lineTo(x + cornerRadius.lowerLeft, y + height);
    this.quadraticCurveTo(x, y + height, x, y + height - cornerRadius.lowerLeft);
    this.lineTo(x, y + cornerRadius.upperLeft);
    this.quadraticCurveTo(x, y, x + cornerRadius.upperLeft, y);
    this.closePath();
    if (stroke) {
        this.stroke();
    }
    if (fill) {
        this.fill();
    }
} 

इसे इस तरह उपयोग करें:

var canvas = document.getElementById("canvas");
var c = canvas.getContext("2d");
c.fillStyle = "blue";
c.roundRect(50, 100, 50, 100, {upperLeft:10,upperRight:10}, true, true);

1
यह दृष्टिकोण गोल कोनों पर इतना नियंत्रण प्रदान करता है। यह स्वीकृत उत्तर क्यों नहीं है>
विघ्नेश राउत

@VighneshRaut शायद इसलिए क्योंकि यह उत्तर मूल स्वीकृत उत्तर को कॉपी / पेस्ट करता है और गोल कोनों को जोड़ता है। मैंने इसे स्वीकार किए गए उत्तर में शामिल किया और इस उत्तर को श्रेय दिया। स्वीकृत उत्तर का एक जीवंत उदाहरण है और वाक्यविन्यास सरल है यदि आप एक ही त्रिज्या (जो सबसे आम है) के साथ सभी कोनों को चाहते हैं। अंत में, यह उत्तर एक देशी वस्तु के प्रोटोटाइप को संशोधित करने का सुझाव देता है जो कि एक नहीं है
जुआन मेंडेस

12

drawPolygonनीचे समारोह आकर्षित करने के लिए इस्तेमाल किया जा सकता किसी भी गोलाकार कोनों के साथ बहुभुज।

इसे यहां चलाकर देखें।

function drawPolygon(ctx, pts, radius) {
  if (radius > 0) {
    pts = getRoundedPoints(pts, radius);
  }
  var i, pt, len = pts.length;
  ctx.beginPath();
  for (i = 0; i < len; i++) {
    pt = pts[i];
    if (i == 0) {          
      ctx.moveTo(pt[0], pt[1]);
    } else {
      ctx.lineTo(pt[0], pt[1]);
    }
    if (radius > 0) {
      ctx.quadraticCurveTo(pt[2], pt[3], pt[4], pt[5]);
    }
  }
  ctx.closePath();
}

function getRoundedPoints(pts, radius) {
  var i1, i2, i3, p1, p2, p3, prevPt, nextPt,
      len = pts.length,
      res = new Array(len);
  for (i2 = 0; i2 < len; i2++) {
    i1 = i2-1;
    i3 = i2+1;
    if (i1 < 0) {
      i1 = len - 1;
    }
    if (i3 == len) {
      i3 = 0;
    }
    p1 = pts[i1];
    p2 = pts[i2];
    p3 = pts[i3];
    prevPt = getRoundedPoint(p1[0], p1[1], p2[0], p2[1], radius, false);
    nextPt = getRoundedPoint(p2[0], p2[1], p3[0], p3[1], radius, true);
    res[i2] = [prevPt[0], prevPt[1], p2[0], p2[1], nextPt[0], nextPt[1]];
  }
  return res;
};

function getRoundedPoint(x1, y1, x2, y2, radius, first) {
  var total = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)),
      idx = first ? radius / total : (total - radius) / total;
  return [x1 + (idx * (x2 - x1)), y1 + (idx * (y2 - y1))];
};

फ़ंक्शन इस तरह बहुभुज बिंदुओं के साथ एक सरणी प्राप्त करता है:

var canvas = document.getElementById("cv");
var ctx = canvas.getContext("2d");
ctx.strokeStyle = "#000000";
ctx.lineWidth = 5;

drawPolygon(ctx, [[20,   20],
                  [120,  20],
                  [120, 120],
                  [ 20, 120]], 10);
ctx.stroke();

यह एक पोर्ट और यहां पोस्ट किए गए समाधान का अधिक सामान्य संस्करण है


9

यहाँ एक मैंने लिखा है ... त्रिज्या पर बेहतर नियंत्रण के लिए द्विघात वक्रों के बजाय आर्क्स का उपयोग करता है। इसके अलावा, यह आप को पथपाकर और भरने को छोड़ देता है

    /* Canvas 2d context - roundRect
 *
 * Accepts 5 parameters, the start_x and start_y points, the end_x and end_y points, and the radius of the corners
 * 
 * No return value
 */

CanvasRenderingContext2D.prototype.roundRect = function(sx,sy,ex,ey,r) {
    var r2d = Math.PI/180;
    if( ( ex - sx ) - ( 2 * r ) < 0 ) { r = ( ( ex - sx ) / 2 ); } //ensure that the radius isn't too large for x
    if( ( ey - sy ) - ( 2 * r ) < 0 ) { r = ( ( ey - sy ) / 2 ); } //ensure that the radius isn't too large for y
    this.beginPath();
    this.moveTo(sx+r,sy);
    this.lineTo(ex-r,sy);
    this.arc(ex-r,sy+r,r,r2d*270,r2d*360,false);
    this.lineTo(ex,ey-r);
    this.arc(ex-r,ey-r,r,r2d*0,r2d*90,false);
    this.lineTo(sx+r,ey);
    this.arc(sx+r,ey-r,r,r2d*90,r2d*180,false);
    this.lineTo(sx,sy+r);
    this.arc(sx+r,sy+r,r,r2d*180,r2d*270,false);
    this.closePath();
}

यहाँ एक उदाहरण है:

var _e = document.getElementById('#my_canvas');
var _cxt = _e.getContext("2d");
_cxt.roundRect(35,10,260,120,20);
_cxt.strokeStyle = "#000";
_cxt.stroke();

यह आपको त्रिज्या पर बेहतर नियंत्रण कैसे देता है? मैंने सोचा था कि आप x / y radii (अंडाकार कोनों) के लिए अनुमति देने जा रहे थे, और प्रत्येक कोने के लिए अलग-अलग रेडी निर्दिष्ट भी कर रहे थे
Juan Mendes

3
आपका r2dशायद कहा जाना चाहता है d2r
ग्रुमड्रिग

1
@ जुआनमेन्डेस: इस समाधान में गोल कोनों के (चाप-आधारित) आकार आपके (द्विघात-आधारित) समाधान की तुलना में अधिक गोलाकार होते हैं। मुझे लगता है कि "त्रिज्या पर बेहतर नियंत्रण" से उनका मतलब है।
ब्रेंट बर्नबर्न 27'13

मैंने इस पद्धति का उपयोग भी किया, यह क्वाड्रैटिक कर्व का उपयोग करने से बेहतर है। लेकिन अगर आप आयत की तुलना में कुछ अधिक जटिल हैं, तो यह वास्तव में दर्दनाक है। एंड्रॉइड कैनवास की तरह एक स्वचालित विधि थी।
अलेक्सी पेट्रेंको

7
    var canvas = document.createElement("canvas");
    document.body.appendChild(canvas);
    var ctx = canvas.getContext("2d");
    ctx.beginPath();
    ctx.moveTo(100,100);
    ctx.arcTo(0,100,0,0,30);
    ctx.arcTo(0,0,100,0,30);
    ctx.arcTo(100,0,100,100,30);
    ctx.arcTo(100,100,0,100,30);
    ctx.fill();

यह वही था जिसकी मुझे तलाश थी
डैनियल

अंत में एक संक्षिप्त और व्यापक उत्तर जो वास्तव में काम करता है। धन्यवाद।
फ्रांज स्कफका

5

तो यह लाइन जॉइन = "राउंड" का उपयोग करने पर आधारित है और उचित अनुपात, गणित और तर्क के साथ मैं इस फ़ंक्शन को बनाने में सक्षम हूं, यह सही नहीं है लेकिन आशा है कि यह मदद करता है। यदि आप प्रत्येक कोने में एक अलग दायरा बनाना चाहते हैं, तो एक नज़र डालें: https://p5js.org/reference/#/5/5 सही

ये रहा:

CanvasRenderingContext2D.prototype.roundRect = function (x,y,width,height,radius) {
    radius = Math.min(Math.max(width-1,1),Math.max(height-1,1),radius);
    var rectX = x;
    var rectY = y;
    var rectWidth = width;
    var rectHeight = height;
    var cornerRadius = radius;

    this.lineJoin = "round";
    this.lineWidth = cornerRadius;
    this.strokeRect(rectX+(cornerRadius/2), rectY+(cornerRadius/2), rectWidth-cornerRadius, rectHeight-cornerRadius);
    this.fillRect(rectX+(cornerRadius/2), rectY+(cornerRadius/2), rectWidth-cornerRadius, rectHeight-cornerRadius);
    this.stroke();
    this.fill();
}

CanvasRenderingContext2D.prototype.roundRect = function (x,y,width,height,radius) {
    radius = Math.min(Math.max(width-1,1),Math.max(height-1,1),radius);
    var rectX = x;
    var rectY = y;
    var rectWidth = width;
    var rectHeight = height;
    var cornerRadius = radius;

    this.lineJoin = "round";
    this.lineWidth = cornerRadius;
    this.strokeRect(rectX+(cornerRadius/2), rectY+(cornerRadius/2), rectWidth-cornerRadius, rectHeight-cornerRadius);
    this.fillRect(rectX+(cornerRadius/2), rectY+(cornerRadius/2), rectWidth-cornerRadius, rectHeight-cornerRadius);
    this.stroke();
    this.fill();
}
    var canvas = document.getElementById("myCanvas");
    var ctx = canvas.getContext('2d');
function yop() {
  ctx.clearRect(0,0,1000,1000)
  ctx.fillStyle = "#ff0000";
  ctx.strokeStyle = "#ff0000";  ctx.roundRect(Number(document.getElementById("myRange1").value),Number(document.getElementById("myRange2").value),Number(document.getElementById("myRange3").value),Number(document.getElementById("myRange4").value),Number(document.getElementById("myRange5").value));
requestAnimationFrame(yop);
}
requestAnimationFrame(yop);
<input type="range" min="0" max="1000" value="10" class="slider" id="myRange1"><input type="range" min="0" max="1000" value="10" class="slider" id="myRange2"><input type="range" min="0" max="1000" value="200" class="slider" id="myRange3"><input type="range" min="0" max="1000" value="100" class="slider" id="myRange4"><input type="range" min="1" max="1000" value="50" class="slider" id="myRange5">
<canvas id="myCanvas" width="1000" height="1000">
</canvas>


1
StackOverflow में आपका स्वागत है! चूंकि यह कोड समस्या को हल कर सकता है, इसलिए बेहतर है कि आप इस बारे में अधिक स्पष्टीकरण जोड़ें कि यह कैसे काम करता है।
Tân

3

ओपेरा, एफएफएस।

if (window["CanvasRenderingContext2D"]) {
    /** @expose */
    CanvasRenderingContext2D.prototype.roundRect = function(x, y, w, h, r) {
        if (w < 2*r) r = w/2;
        if (h < 2*r) r = h/2;
        this.beginPath();
        if (r < 1) {
            this.rect(x, y, w, h);
        } else {
            if (window["opera"]) {
                this.moveTo(x+r, y);
                this.arcTo(x+r, y, x, y+r, r);
                this.lineTo(x, y+h-r);
                this.arcTo(x, y+h-r, x+r, y+h, r);
                this.lineTo(x+w-r, y+h);
                this.arcTo(x+w-r, y+h, x+w, y+h-r, r);
                this.lineTo(x+w, y+r);
                this.arcTo(x+w, y+r, x+w-r, y, r);
            } else {
                this.moveTo(x+r, y);
                this.arcTo(x+w, y, x+w, y+h, r);
                this.arcTo(x+w, y+h, x, y+h, r);
                this.arcTo(x, y+h, x, y, r);
                this.arcTo(x, y, x+w, y, r);
            }
        }
        this.closePath();
    };
    /** @expose */
    CanvasRenderingContext2D.prototype.fillRoundRect = function(x, y, w, h, r) {
        this.roundRect(x, y, w, h, r);
        this.fill();
    };
    /** @expose */
    CanvasRenderingContext2D.prototype.strokeRoundRect = function(x, y, w, h, r) {
        this.roundRect(x, y, w, h, r);
        this.stroke();
    };
}

चूंकि Opera WebKit जा रहा है, इसलिए यह विरासत के मामले में भी मान्य होना चाहिए।


3

फ़ंक्शन को एक कैनवास संदर्भ का उपयोग करने के सामान्य साधनों के साथ अधिक सुसंगत बनाने के लिए, कैनवास संदर्भ वर्ग को एक ' fillRoundedRect' विधि शामिल करने के लिए बढ़ाया जा सकता है - जिसे उसी तरह fillRectसे कहा जा सकता है:

var canv = document.createElement("canvas");
var cctx = canv.getContext("2d");

// If thie canvasContext class doesn't have  a fillRoundedRect, extend it now
if (!cctx.constructor.prototype.fillRoundedRect) {
  // Extend the canvaseContext class with a fillRoundedRect method
  cctx.constructor.prototype.fillRoundedRect = 
    function (xx,yy, ww,hh, rad, fill, stroke) {
      if (typeof(rad) == "undefined") rad = 5;
      this.beginPath();
      this.moveTo(xx+rad, yy);
      this.arcTo(xx+ww, yy,    xx+ww, yy+hh, rad);
      this.arcTo(xx+ww, yy+hh, xx,    yy+hh, rad);
      this.arcTo(xx,    yy+hh, xx,    yy,    rad);
      this.arcTo(xx,    yy,    xx+ww, yy,    rad);
      if (stroke) this.stroke();  // Default to no stroke
      if (fill || typeof(fill)=="undefined") this.fill();  // Default to fill
  }; // end of fillRoundedRect method
} 

कोड यह देखने के लिए जांचता है कि क्या कैनवास के संदर्भ ऑब्जेक्ट के लिए कंस्ट्रक्टर के लिए प्रोटोटाइप में एक ' fillRoundedRect' गुण है और एक को जोड़ता है - पहली बार के आसपास। इसे उसी तरीके से लागू किया जाता है, जिस तरीके से fillRect:

  ctx.fillStyle = "#eef";  ctx.strokeStyle = "#ddf";
  // ctx.fillRect(10,10, 200,100);
  ctx.fillRoundedRect(10,10, 200,100, 5);

विधि का उपयोग arcToविधि के रूप में Grumdring किया था। विधि में, वस्तु thisका एक संदर्भ है ctx। अपरिभाषित होने पर स्ट्रोक का तर्क गलत को परिभाषित करता है। यदि कोई अपरिभाषित आयत को भरने के लिए भरण तर्क डिफॉल्ट करता है।

(फ़ायरफ़ॉक्स पर परीक्षण किया गया, मुझे नहीं पता कि सभी कार्यान्वयन इस तरीके से विस्तार की अनुमति देते हैं।)


1
मैं जोड़ने का सुझाव देता हूं: rad = Math.min( rad, ww/2, hh/2 );ताकि यह @ Grumdrig के संस्करण में बड़ी रेडी के साथ काम करे।
ब्रेंट बर्नबर्न

3

यहां कोनों को गोल करने के लिए एक लाइनजॉइन का उपयोग करके समाधान दिया गया है। काम करता है अगर आप सिर्फ एक ठोस आकार की जरूरत है, लेकिन इतना नहीं अगर आप एक पतली सीमा की जरूरत है कि सीमा त्रिज्या से छोटा है।

    function roundedRect(ctx, options) {

        ctx.strokeStyle = options.color;
        ctx.fillStyle = options.color;
        ctx.lineJoin = "round";
        ctx.lineWidth = options.radius;

        ctx.strokeRect(
            options.x+(options.radius*.5),
            options.y+(options.radius*.5),
            options.width-options.radius,
            options.height-options.radius
        );

        ctx.fillRect(
            options.x+(options.radius*.5),
            options.y+(options.radius*.5),
            options.width-options.radius,
            options.height-options.radius
        );

        ctx.stroke();
        ctx.fill();

    }

    const canvas = document.getElementsByTagName("CANVAS")[0];
    let ctx = canvas.getContext('2d');

    roundedRect(ctx, {
        x: 10,
        y: 10,
        width: 200,
        height: 100,
        radius: 10,
        color: "red"
    });

0

इस पंक्ति को जोड़ने का प्रयास करें, जब आप गोल कोनों को प्राप्त करना चाहते हैं: ctx.lineCap = "राउंड";


1
नमस्ते, अतिप्रवाह ढेर करने के लिए आपका स्वागत है। यहाँ एक नज़र है । क्या आपको यकीन है कि यह आयतों के लिए एक उपयोगी जवाब है?
जीरोन हेयेर
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.