|
| 1 | +/** |
| 2 | + * HiDPI Canvas Polyfill (1.0.3) |
| 3 | + * |
| 4 | + * Author: Jonathan D. Johnson (http://jondavidjohn.com) |
| 5 | + * Homepage: https://github.com/jondavidjohn/hidpi-canvas-polyfill |
| 6 | + * Issue Tracker: https://github.com/jondavidjohn/hidpi-canvas-polyfill/issues |
| 7 | + * License: Apache 2.0 |
| 8 | +*/ |
| 9 | +(function(prototype) { |
| 10 | + |
| 11 | + var func, value, |
| 12 | + |
| 13 | + getPixelRatio = function(context) { |
| 14 | + var backingStore = context.backingStorePixelRatio || |
| 15 | + context.webkitBackingStorePixelRatio || |
| 16 | + context.mozBackingStorePixelRatio || |
| 17 | + context.msBackingStorePixelRatio || |
| 18 | + context.oBackingStorePixelRatio || |
| 19 | + context.backingStorePixelRatio || 1; |
| 20 | + |
| 21 | + return (window.devicePixelRatio || 1) / backingStore; |
| 22 | + }, |
| 23 | + |
| 24 | + forEach = function(obj, func) { |
| 25 | + for (var p in obj) { |
| 26 | + if (obj.hasOwnProperty(p)) { |
| 27 | + func(obj[p], p); |
| 28 | + } |
| 29 | + } |
| 30 | + }, |
| 31 | + |
| 32 | + ratioArgs = { |
| 33 | + 'fillRect': 'all', |
| 34 | + 'clearRect': 'all', |
| 35 | + 'strokeRect': 'all', |
| 36 | + 'moveTo': 'all', |
| 37 | + 'lineTo': 'all', |
| 38 | + 'arc': [0,1,2], |
| 39 | + 'arcTo': 'all', |
| 40 | + 'bezierCurveTo': 'all', |
| 41 | + 'isPointinPath': 'all', |
| 42 | + 'isPointinStroke': 'all', |
| 43 | + 'quadraticCurveTo': 'all', |
| 44 | + 'rect': 'all', |
| 45 | + 'translate': 'all', |
| 46 | + 'createRadialGradient': 'all' |
| 47 | + }; |
| 48 | + |
| 49 | + forEach(ratioArgs, function(value, key) { |
| 50 | + prototype[key] = (function(_super) { |
| 51 | + return function() { |
| 52 | + var i, len, |
| 53 | + ratio = getPixelRatio(this), |
| 54 | + args = Array.prototype.slice.call(arguments); |
| 55 | + |
| 56 | + if (value === 'all') { |
| 57 | + args = args.map(function(a) { |
| 58 | + return a * ratio; |
| 59 | + }); |
| 60 | + } |
| 61 | + else if (Array.isArray(value)) { |
| 62 | + for (i = 0, len = value.length; i < len; i++) { |
| 63 | + args[value[i]] *= ratio; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return _super.apply(this, args); |
| 68 | + }; |
| 69 | + })(prototype[key]); |
| 70 | + }); |
| 71 | + |
| 72 | + // Text |
| 73 | + // |
| 74 | + prototype.fillText = (function(_super) { |
| 75 | + return function() { |
| 76 | + var ratio = getPixelRatio(this), |
| 77 | + args = Array.prototype.slice.call(arguments); |
| 78 | + |
| 79 | + args[1] *= ratio; // x |
| 80 | + args[2] *= ratio; // y |
| 81 | + |
| 82 | + this.font = this.font.replace( |
| 83 | + /(\d+)(px|em|rem|pt)/g, |
| 84 | + function(w, m, u) { |
| 85 | + return (m * ratio) + u; |
| 86 | + } |
| 87 | + ); |
| 88 | + |
| 89 | + _super.apply(this, args); |
| 90 | + |
| 91 | + this.font = this.font.replace( |
| 92 | + /(\d+)(px|em|rem|pt)/g, |
| 93 | + function(w, m, u) { |
| 94 | + return (m / ratio) + u; |
| 95 | + } |
| 96 | + ); |
| 97 | + }; |
| 98 | + })(prototype.fillText); |
| 99 | + |
| 100 | + prototype.strokeText = (function(_super) { |
| 101 | + return function() { |
| 102 | + var ratio = getPixelRatio(this), |
| 103 | + args = Array.prototype.slice.call(arguments); |
| 104 | + |
| 105 | + args[1] *= ratio; // x |
| 106 | + args[2] *= ratio; // y |
| 107 | + |
| 108 | + this.font = this.font.replace( |
| 109 | + /(\d+)(px|em|rem|pt)/g, |
| 110 | + function(w, m, u) { |
| 111 | + return (m * ratio) + u; |
| 112 | + } |
| 113 | + ); |
| 114 | + |
| 115 | + _super.apply(this, args); |
| 116 | + |
| 117 | + this.font = this.font.replace( |
| 118 | + /(\d+)(px|em|rem|pt)/g, |
| 119 | + function(w, m, u) { |
| 120 | + return (m / ratio) + u; |
| 121 | + } |
| 122 | + ); |
| 123 | + }; |
| 124 | + })(prototype.strokeText); |
| 125 | +})(CanvasRenderingContext2D.prototype); |
| 126 | +;(function(prototype) { |
| 127 | + prototype.getContext = (function(_super) { |
| 128 | + return function(type) { |
| 129 | + var backingStore, ratio, |
| 130 | + context = _super.call(this, type); |
| 131 | + |
| 132 | + if (type === '2d') { |
| 133 | + |
| 134 | + backingStore = context.backingStorePixelRatio || |
| 135 | + context.webkitBackingStorePixelRatio || |
| 136 | + context.mozBackingStorePixelRatio || |
| 137 | + context.msBackingStorePixelRatio || |
| 138 | + context.oBackingStorePixelRatio || |
| 139 | + context.backingStorePixelRatio || 1; |
| 140 | + |
| 141 | + ratio = (window.devicePixelRatio || 1) / backingStore; |
| 142 | + |
| 143 | + if (ratio > 1) { |
| 144 | + this.style.height = this.height + 'px'; |
| 145 | + this.style.width = this.width + 'px'; |
| 146 | + this.width *= ratio; |
| 147 | + this.height *= ratio; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return context; |
| 152 | + }; |
| 153 | + })(prototype.getContext); |
| 154 | +})(HTMLCanvasElement.prototype); |
0 commit comments