|
| 1 | +var HSBRect = (function () { |
| 2 | + function HSBRect(width, height) { |
| 3 | + if (typeof width === "undefined") { width = 0x80; } |
| 4 | + if (typeof height === "undefined") { height = 0x80; } |
| 5 | + this._hue = 0; |
| 6 | + this._sa = document.createElement('canvas'); |
| 7 | + this._sa.height = 1; |
| 8 | + this._sa.style.setProperty('position', 'absolute'); |
| 9 | + this._br = document.createElement('canvas'); |
| 10 | + this._br.width = 1; |
| 11 | + this._br.style.setProperty('position', 'absolute'); |
| 12 | + this._div = document.createElement('div'); |
| 13 | + this._div.style.setProperty('background-color', 'white'); |
| 14 | + this._div.appendChild(this._sa); |
| 15 | + this._div.appendChild(this._br); |
| 16 | + this.width = width; |
| 17 | + this.height = height; |
| 18 | + } |
| 19 | + HSBRect.prototype._renderSa = function () { |
| 20 | + var ctx = this._sa.getContext('2d'); |
| 21 | + var grad = ctx.createLinearGradient(0, 0, this._sa.width, 1); |
| 22 | + var c = 'hsla(' + this._hue + ',100%,50%,'; |
| 23 | + grad.addColorStop(0, c + '0)'); |
| 24 | + grad.addColorStop(1, c + '1)'); |
| 25 | + ctx.fillStyle = grad; |
| 26 | + ctx.clearRect(0, 0, this._sa.width, 1); |
| 27 | + ctx.fillRect(0, 0, this._sa.width, 1); |
| 28 | + }; |
| 29 | + HSBRect.prototype._renderBr = function () { |
| 30 | + var ctx = this._br.getContext('2d'); |
| 31 | + var grad = ctx.createLinearGradient(0, 0, 1, this._br.height); |
| 32 | + grad.addColorStop(0, 'rgba(0,0,0,0)'); |
| 33 | + grad.addColorStop(1, 'black'); |
| 34 | + ctx.fillStyle = grad; |
| 35 | + ctx.clearRect(0, 0, 1, this._br.height); |
| 36 | + ctx.fillRect(0, 0, 1, this._br.height); |
| 37 | + }; |
| 38 | + HSBRect.prototype._size = function () { |
| 39 | + var w = this._sa.width + 'px'; |
| 40 | + var h = this._br.height + 'px'; |
| 41 | + this._div.style.setProperty('width', w); |
| 42 | + this._div.style.setProperty('height', h); |
| 43 | + this._sa.style.setProperty('width', w); |
| 44 | + this._sa.style.setProperty('height', h); |
| 45 | + this._br.style.setProperty('width', w); |
| 46 | + this._br.style.setProperty('height', h); |
| 47 | + }; |
| 48 | + Object.defineProperty(HSBRect.prototype, "DOMElement", { |
| 49 | + get: function () { |
| 50 | + return this._div; |
| 51 | + }, |
| 52 | + enumerable: true, |
| 53 | + configurable: true |
| 54 | + }); |
| 55 | + Object.defineProperty(HSBRect.prototype, "hue", { |
| 56 | + get: function () { |
| 57 | + return this._hue; |
| 58 | + }, |
| 59 | + set: function (value) { |
| 60 | + this._hue = value; |
| 61 | + this._renderSa(); |
| 62 | + }, |
| 63 | + enumerable: true, |
| 64 | + configurable: true |
| 65 | + }); |
| 66 | + Object.defineProperty(HSBRect.prototype, "width", { |
| 67 | + get: function () { |
| 68 | + return this._sa.width; |
| 69 | + }, |
| 70 | + set: function (value) { |
| 71 | + this._sa.width = value; |
| 72 | + this._size(); |
| 73 | + this._renderSa(); |
| 74 | + }, |
| 75 | + enumerable: true, |
| 76 | + configurable: true |
| 77 | + }); |
| 78 | + Object.defineProperty(HSBRect.prototype, "height", { |
| 79 | + get: function () { |
| 80 | + return this._br.height; |
| 81 | + }, |
| 82 | + set: function (value) { |
| 83 | + this._br.height = value; |
| 84 | + this._size(); |
| 85 | + this._renderBr(); |
| 86 | + }, |
| 87 | + enumerable: true, |
| 88 | + configurable: true |
| 89 | + }); |
| 90 | + return HSBRect; |
| 91 | +})(); |
0 commit comments