/*------------------------------------------------------------- mc.drawRect is a method for drawing rectangles and rounded rectangles. Regular rectangles are sufficiently easy that I often just rebuilt the method in any file I needed it in, but the rounded rectangle was something I was needing more often, hence the method. The rounding is very much like that of the rectangle tool in Flash where if the rectangle is smaller in either dimension than the rounding would permit, the rounding scales down to fit. -------------------------------------------------------------*/ MovieClip.prototype.drawRect = function(x, y, w, h, cornerRadius) { // ============== // mc.drawRect() - by Ric Ewing (ric@formequalsfunction.com) - version 1.1 - 4.7.2002 // // x, y = top left corner of rect // w = width of rect // h = height of rect // cornerRadius = [optional] radius of rounding for corners (defaults to 0) // ============== if (arguments.length<4) { return; } // if the user has defined cornerRadius our task is a bit more complex. :) if (cornerRadius>0) { // init vars var theta, angle, cx, cy, px, py; // make sure that w + h are larger than 2*cornerRadius if (cornerRadius>Math.min(w, h)/2) { cornerRadius = Math.min(w, h)/2; } // theta = 45 degrees in radians theta = Math.PI/4; // draw top line this.moveTo(x+cornerRadius, y); this.lineTo(x+w-cornerRadius, y); //angle is currently 90 degrees angle = -Math.PI/2; // draw tr corner in two parts cx = x+w-cornerRadius+(Math.cos(angle+(theta/2))*cornerRadius/Math.cos(theta/2)); cy = y+cornerRadius+(Math.sin(angle+(theta/2))*cornerRadius/Math.cos(theta/2)); px = x+w-cornerRadius+(Math.cos(angle+theta)*cornerRadius); py = y+cornerRadius+(Math.sin(angle+theta)*cornerRadius); this.curveTo(cx, cy, px, py); angle += theta; cx = x+w-cornerRadius+(Math.cos(angle+(theta/2))*cornerRadius/Math.cos(theta/2)); cy = y+cornerRadius+(Math.sin(angle+(theta/2))*cornerRadius/Math.cos(theta/2)); px = x+w-cornerRadius+(Math.cos(angle+theta)*cornerRadius); py = y+cornerRadius+(Math.sin(angle+theta)*cornerRadius); this.curveTo(cx, cy, px, py); // draw right line this.lineTo(x+w, y+h-cornerRadius); // draw br corner angle += theta; cx = x+w-cornerRadius+(Math.cos(angle+(theta/2))*cornerRadius/Math.cos(theta/2)); cy = y+h-cornerRadius+(Math.sin(angle+(theta/2))*cornerRadius/Math.cos(theta/2)); px = x+w-cornerRadius+(Math.cos(angle+theta)*cornerRadius); py = y+h-cornerRadius+(Math.sin(angle+theta)*cornerRadius); this.curveTo(cx, cy, px, py); angle += theta; cx = x+w-cornerRadius+(Math.cos(angle+(theta/2))*cornerRadius/Math.cos(theta/2)); cy = y+h-cornerRadius+(Math.sin(angle+(theta/2))*cornerRadius/Math.cos(theta/2)); px = x+w-cornerRadius+(Math.cos(angle+theta)*cornerRadius); py = y+h-cornerRadius+(Math.sin(angle+theta)*cornerRadius); this.curveTo(cx, cy, px, py); // draw bottom line this.lineTo(x+cornerRadius, y+h); // draw bl corner angle += theta; cx = x+cornerRadius+(Math.cos(angle+(theta/2))*cornerRadius/Math.cos(theta/2)); cy = y+h-cornerRadius+(Math.sin(angle+(theta/2))*cornerRadius/Math.cos(theta/2)); px = x+cornerRadius+(Math.cos(angle+theta)*cornerRadius); py = y+h-cornerRadius+(Math.sin(angle+theta)*cornerRadius); this.curveTo(cx, cy, px, py); angle += theta; cx = x+cornerRadius+(Math.cos(angle+(theta/2))*cornerRadius/Math.cos(theta/2)); cy = y+h-cornerRadius+(Math.sin(angle+(theta/2))*cornerRadius/Math.cos(theta/2)); px = x+cornerRadius+(Math.cos(angle+theta)*cornerRadius); py = y+h-cornerRadius+(Math.sin(angle+theta)*cornerRadius); this.curveTo(cx, cy, px, py); // draw left line this.lineTo(x, y+cornerRadius); // draw tl corner angle += theta; cx = x+cornerRadius+(Math.cos(angle+(theta/2))*cornerRadius/Math.cos(theta/2)); cy = y+cornerRadius+(Math.sin(angle+(theta/2))*cornerRadius/Math.cos(theta/2)); px = x+cornerRadius+(Math.cos(angle+theta)*cornerRadius); py = y+cornerRadius+(Math.sin(angle+theta)*cornerRadius); this.curveTo(cx, cy, px, py); angle += theta; cx = x+cornerRadius+(Math.cos(angle+(theta/2))*cornerRadius/Math.cos(theta/2)); cy = y+cornerRadius+(Math.sin(angle+(theta/2))*cornerRadius/Math.cos(theta/2)); px = x+cornerRadius+(Math.cos(angle+theta)*cornerRadius); py = y+cornerRadius+(Math.sin(angle+theta)*cornerRadius); this.curveTo(cx, cy, px, py); } else { // cornerRadius was not defined or = 0. This makes it easy. this.moveTo(x, y); this.lineTo(x+w, y); this.lineTo(x+w, y+h); this.lineTo(x, y+h); this.lineTo(x, y); } };