| OLD | NEW |
| (Empty) | |
| 1 // From Grumdrig |
| 2 // stackoverflow.com/questions/1255512/how-to-draw-a-rounded-rectangle-on-html-c
anvas |
| 3 CanvasRenderingContext2D.prototype.roundRect = function (x, y, w, h, r) { |
| 4 "use strict"; |
| 5 |
| 6 if (w < 2 * r) { |
| 7 r = w / 2; |
| 8 } |
| 9 if (h < 2 * r) { |
| 10 r = h / 2; |
| 11 } |
| 12 this.beginPath(); |
| 13 this.moveTo(x + r, y); |
| 14 this.arcTo(x + w, y, x + w, y + h, r); |
| 15 this.arcTo(x + w, y + h, x, y + h, r); |
| 16 this.arcTo(x, y + h, x, y, r); |
| 17 this.arcTo(x, y, x + w, y, r); |
| 18 this.closePath(); |
| 19 return this; |
| 20 }; |
| 21 |
| 22 // Creates a new RangeGraph object that contains a canvas element that |
| 23 // it draws to. |
| 24 function RangeGraph(max, existing) { |
| 25 "use strict"; |
| 26 |
| 27 this.max = max; |
| 28 |
| 29 // If we already have a <canvas> element to draw to, use that |
| 30 this.element = existing || document.createElement("canvas"); |
| 31 this.context = this.element.getContext('2d'); |
| 32 |
| 33 this.cornerLeft = 2; |
| 34 this.cornerTop = 2; |
| 35 |
| 36 this.toDraw = []; |
| 37 |
| 38 // The drawing styles. One constant list to draw from, |
| 39 this.STYLES = ["rgb(100, 10,10)", "rgb(100, 10, 100)", |
| 40 "rgb(70, 130, 70)", "rgb(70, 70, 130)" ]; |
| 41 // another list to pop off of |
| 42 this.styles = this.STYLES; |
| 43 } |
| 44 |
| 45 RangeGraph.prototype.setWidth = function (width) { |
| 46 "use strict"; |
| 47 this.width = width; |
| 48 this.element.width = this.width + this.cornerLeft * 2; |
| 49 this.height = 13; |
| 50 this.element.height = this.height + this.cornerTop * 2; |
| 51 }; |
| 52 |
| 53 RangeGraph.prototype.extend = function (max) { |
| 54 "use strict"; |
| 55 this.end = max; |
| 56 }; |
| 57 |
| 58 RangeGraph.prototype.plot = function (start, end) { |
| 59 "use strict"; |
| 60 |
| 61 // Always keep them in order |
| 62 if (end > start) { |
| 63 this.toDraw.push([start, end]); |
| 64 } else { |
| 65 this.toDraw.push([end, start]); |
| 66 } |
| 67 }; |
| 68 |
| 69 RangeGraph.prototype.draw = function () { |
| 70 "use strict"; |
| 71 |
| 72 // Fill the canvas wit white. |
| 73 this.context.fillStyle = "white"; |
| 74 this.context.clearRect(0, 0, this.width, this.height); |
| 75 |
| 76 // The light-grey background. |
| 77 this.context.fillStyle = "rgb(200,200,200)"; |
| 78 this.context.roundRect(0, 0, this.element.width, this.element.height, 2); |
| 79 this.context.fill(); |
| 80 |
| 81 // The inner shadow. |
| 82 this.context.fillStyle = "rgba(170,170,170, 0.7)"; |
| 83 this.context.roundRect(this.cornerLeft, this.cornerTop, this.width, |
| 84 this.height, 2); |
| 85 this.context.fill(); |
| 86 |
| 87 // Translate because line drawing on whole pixels does |
| 88 // terrible anti-aliasing. |
| 89 this.context.translate(0.5, 0.5); |
| 90 |
| 91 goog.array.forEach(this.toDraw, function (pair) { |
| 92 var start = pair[0], |
| 93 end = pair[1], |
| 94 startPercent = start / this.max, |
| 95 endPercent = end / this.max, |
| 96 startPx = Math.floor(startPercent * this.width), |
| 97 endPx = Math.ceil(endPercent * this.width); |
| 98 |
| 99 // The main color bar |
| 100 this.context.fillStyle = this.styles.pop() || "red"; |
| 101 this.context.roundRect(this.cornerLeft + startPx, this.cornerTop, |
| 102 endPx - startPx, this.height - 1, 2); |
| 103 this.context.fill(); |
| 104 |
| 105 // The gradient up on top |
| 106 this.context.fillStyle = "rgba(200,200,200,0.4)"; |
| 107 this.context.roundRect(this.cornerLeft + startPx, this.cornerTop, |
| 108 endPx - startPx, |
| 109 Math.floor(this.height / 2.5) + 0.5, 2); |
| 110 this.context.fill(); |
| 111 |
| 112 |
| 113 // The black outline |
| 114 this.context.roundRect(this.cornerLeft + startPx, this.cornerTop, |
| 115 endPx - startPx, this.height - 1, 2); |
| 116 this.context.strokeStyle = "rgba(0, 0, 0, 0.7)"; |
| 117 this.context.stroke(); |
| 118 |
| 119 }, this); |
| 120 |
| 121 |
| 122 // The tick marks |
| 123 this.context.fillStyle = "rgba(0,0,0,0.4)"; |
| 124 var percent; |
| 125 for (percent = 0.1; percent <= 0.9; percent += 0.1) { |
| 126 var pxPos = Math.floor(percent * this.width); |
| 127 this.context.fillRect(pxPos + 0.5, 2, 1, this.height - 1); |
| 128 } |
| 129 |
| 130 // Re-translate for the next call |
| 131 this.context.translate(-0.5, -0.5); |
| 132 |
| 133 |
| 134 this.toDraw = []; |
| 135 this.styles = this.STYLES; |
| 136 }; |
| OLD | NEW |