|
| 1 | +/** |
| 2 | +* Phaser - Display - CSS3Filters |
| 3 | +* |
| 4 | +* Allows for easy addition and modification of CSS3 Filters on DOM objects (typically the Game.Stage.canvas). |
| 5 | +*/ |
| 6 | + |
| 7 | +Phaser.Plugins.CSS3Filters = function (parent) { |
| 8 | + |
| 9 | + this.parent = parent; |
| 10 | + |
| 11 | + this._blur = 0; |
| 12 | + this._grayscale = 0; |
| 13 | + this._sepia = 0; |
| 14 | + this._brightness = 0; |
| 15 | + this._contrast = 0; |
| 16 | + this._hueRotate = 0; |
| 17 | + this._invert = 0; |
| 18 | + this._opacity = 0; |
| 19 | + this._saturate = 0; |
| 20 | + |
| 21 | +}; |
| 22 | + |
| 23 | +Phaser.Plugins.CSS3Filters.prototype = { |
| 24 | + |
| 25 | + setFilter: function (local, prefix, value, unit) { |
| 26 | + |
| 27 | + this[local] = value; |
| 28 | + |
| 29 | + if (this.parent) |
| 30 | + { |
| 31 | + this.parent.style['-webkit-filter'] = prefix + '(' + value + unit + ')'; |
| 32 | + } |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | +}; |
| 37 | + |
| 38 | +Object.defineProperty(Phaser.Plugins.CSS3Filters.prototype, "blur", { |
| 39 | + |
| 40 | + get: function () { |
| 41 | + return this._blur; |
| 42 | + }, |
| 43 | + |
| 44 | + /** |
| 45 | + * Applies a Gaussian blur to the DOM element. The value of 'radius' defines the value of the standard deviation to the Gaussian function, |
| 46 | + * or how many pixels on the screen blend into each other, so a larger value will create more blur. |
| 47 | + * If no parameter is provided, then a value 0 is used. The parameter is specified as a CSS length, but does not accept percentage values. |
| 48 | + */ |
| 49 | + set: function (radius) { |
| 50 | + this.setFilter('_blur', 'blur', radius, 'px'); |
| 51 | + } |
| 52 | +}); |
| 53 | + |
| 54 | +Object.defineProperty(Phaser.Plugins.CSS3Filters.prototype, "grayscale", { |
| 55 | + get: function () { |
| 56 | + return this._grayscale; |
| 57 | + }, |
| 58 | + |
| 59 | + /** |
| 60 | + * Converts the input image to grayscale. The value of 'amount' defines the proportion of the conversion. |
| 61 | + * A value of 100% is completely grayscale. A value of 0% leaves the input unchanged. |
| 62 | + * Values between 0% and 100% are linear multipliers on the effect. If the 'amount' parameter is missing, a value of 100% is used. |
| 63 | + */ |
| 64 | + set: function (amount) { |
| 65 | + this.setFilter('_grayscale', 'grayscale', amount, '%'); |
| 66 | + } |
| 67 | +}); |
| 68 | + |
| 69 | +Object.defineProperty(Phaser.Plugins.CSS3Filters.prototype, "sepia", { |
| 70 | + get: function () { |
| 71 | + return this._sepia; |
| 72 | + }, |
| 73 | + |
| 74 | + /** |
| 75 | + * Converts the input image to sepia. The value of 'amount' defines the proportion of the conversion. |
| 76 | + * A value of 100% is completely sepia. A value of 0 leaves the input unchanged. |
| 77 | + * Values between 0% and 100% are linear multipliers on the effect. If the 'amount' parameter is missing, a value of 100% is used. |
| 78 | + */ |
| 79 | + set: function (amount) { |
| 80 | + this.setFilter('_sepia', 'sepia', amount, '%'); |
| 81 | + } |
| 82 | +}); |
| 83 | + |
| 84 | +Object.defineProperty(Phaser.Plugins.CSS3Filters.prototype, "brightness", { |
| 85 | + get: function () { |
| 86 | + return this._brightness; |
| 87 | + }, |
| 88 | + |
| 89 | + /** |
| 90 | + * Applies a linear multiplier to input image, making it appear more or less bright. |
| 91 | + * A value of 0% will create an image that is completely black. A value of 100% leaves the input unchanged. |
| 92 | + * Other values are linear multipliers on the effect. Values of an amount over 100% are allowed, providing brighter results. |
| 93 | + * If the 'amount' parameter is missing, a value of 100% is used. |
| 94 | + */ |
| 95 | + set: function (amount) { |
| 96 | + this.setFilter('_brightness', 'brightness', amount, '%'); |
| 97 | + } |
| 98 | +}); |
| 99 | + |
| 100 | +Object.defineProperty(Phaser.Plugins.CSS3Filters.prototype, "contrast", { |
| 101 | + get: function () { |
| 102 | + return this._contrast; |
| 103 | + }, |
| 104 | + |
| 105 | + /** |
| 106 | + * Adjusts the contrast of the input. A value of 0% will create an image that is completely black. |
| 107 | + * A value of 100% leaves the input unchanged. Values of amount over 100% are allowed, providing results with less contrast. |
| 108 | + * If the 'amount' parameter is missing, a value of 100% is used. |
| 109 | + */ |
| 110 | + set: function (amount) { |
| 111 | + this.setFilter('_contrast', 'contrast', amount, '%'); |
| 112 | + } |
| 113 | +}); |
| 114 | + |
| 115 | +Object.defineProperty(Phaser.Plugins.CSS3Filters.prototype, "hueRotate", { |
| 116 | + |
| 117 | + get: function () { |
| 118 | + return this._hueRotate; |
| 119 | + }, |
| 120 | + |
| 121 | + /** |
| 122 | + * Applies a hue rotation on the input image. The value of 'angle' defines the number of degrees around the color circle |
| 123 | + * the input samples will be adjusted. A value of 0deg leaves the input unchanged. If the 'angle' parameter is missing, |
| 124 | + * a value of 0deg is used. Maximum value is 360deg. |
| 125 | + */ |
| 126 | + set: function (angle) { |
| 127 | + this.setFilter('_hueRotate', 'hue-rotate', angle, 'deg'); |
| 128 | + } |
| 129 | +}); |
| 130 | + |
| 131 | +Object.defineProperty(Phaser.Plugins.CSS3Filters.prototype, "invert", { |
| 132 | + |
| 133 | + get: function () { |
| 134 | + return this._invert; |
| 135 | + }, |
| 136 | + |
| 137 | + /** |
| 138 | + * Inverts the samples in the input image. The value of 'amount' defines the proportion of the conversion. |
| 139 | + * A value of 100% is completely inverted. A value of 0% leaves the input unchanged. |
| 140 | + * Values between 0% and 100% are linear multipliers on the effect. If the 'amount' parameter is missing, a value of 100% is used. |
| 141 | + */ |
| 142 | + set: function (value) { |
| 143 | + this.setFilter('_invert', 'invert', value, '%'); |
| 144 | + } |
| 145 | +}); |
| 146 | + |
| 147 | +Object.defineProperty(Phaser.Plugins.CSS3Filters.prototype, "opacity", { |
| 148 | + |
| 149 | + get: function () { |
| 150 | + return this._opacity; |
| 151 | + }, |
| 152 | + |
| 153 | + /** |
| 154 | + * Applies transparency to the samples in the input image. The value of 'amount' defines the proportion of the conversion. |
| 155 | + * A value of 0% is completely transparent. A value of 100% leaves the input unchanged. |
| 156 | + * Values between 0% and 100% are linear multipliers on the effect. This is equivalent to multiplying the input image samples by amount. |
| 157 | + * If the 'amount' parameter is missing, a value of 100% is used. |
| 158 | + * This function is similar to the more established opacity property; the difference is that with filters, some browsers provide hardware acceleration for better performance. |
| 159 | + */ |
| 160 | + set: function (value) { |
| 161 | + this.setFilter('_opacity', 'opacity', value, '%'); |
| 162 | + } |
| 163 | +}); |
| 164 | + |
| 165 | +Object.defineProperty(Phaser.Plugins.CSS3Filters.prototype, "saturate", { |
| 166 | + |
| 167 | + get: function () { |
| 168 | + return this._saturate; |
| 169 | + }, |
| 170 | + |
| 171 | + /** |
| 172 | + * Saturates the input image. The value of 'amount' defines the proportion of the conversion. |
| 173 | + * A value of 0% is completely un-saturated. A value of 100% leaves the input unchanged. |
| 174 | + * Other values are linear multipliers on the effect. Values of amount over 100% are allowed, providing super-saturated results. |
| 175 | + * If the 'amount' parameter is missing, a value of 100% is used. |
| 176 | + */ |
| 177 | + set: function (value) { |
| 178 | + this.setFilter('_saturate', 'saturate', value, '%'); |
| 179 | + } |
| 180 | +}); |
0 commit comments