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