summaryrefslogtreecommitdiff
path: root/client/lib/vendor/hidpi-canvas.js
diff options
context:
space:
mode:
Diffstat (limited to 'client/lib/vendor/hidpi-canvas.js')
-rw-r--r--client/lib/vendor/hidpi-canvas.js170
1 files changed, 170 insertions, 0 deletions
diff --git a/client/lib/vendor/hidpi-canvas.js b/client/lib/vendor/hidpi-canvas.js
new file mode 100644
index 0000000..d5323b9
--- /dev/null
+++ b/client/lib/vendor/hidpi-canvas.js
@@ -0,0 +1,170 @@
+/**
+ * HiDPI Canvas Polyfill (1.0.10)
+ *
+ * Author: Jonathan D. Johnson (http://jondavidjohn.com)
+ * Homepage: https://github.com/jondavidjohn/hidpi-canvas-polyfill
+ * Issue Tracker: https://github.com/jondavidjohn/hidpi-canvas-polyfill/issues
+ * License: Apache-2.0
+*/
+(function(prototype) {
+
+ var pixelRatio = (function() {
+ var canvas = window.document.createElement('canvas'),
+ context = canvas.getContext('2d'),
+ backingStore = context.backingStorePixelRatio ||
+ context.webkitBackingStorePixelRatio ||
+ context.mozBackingStorePixelRatio ||
+ context.msBackingStorePixelRatio ||
+ context.oBackingStorePixelRatio ||
+ context.backingStorePixelRatio || 1;
+
+ return (window.devicePixelRatio || 1) / backingStore;
+ })(),
+
+ forEach = function(obj, func) {
+ for (var p in obj) {
+ if (obj.hasOwnProperty(p)) {
+ func(obj[p], p);
+ }
+ }
+ },
+
+ ratioArgs = {
+ 'fillRect': 'all',
+ 'clearRect': 'all',
+ 'strokeRect': 'all',
+ 'moveTo': 'all',
+ 'lineTo': 'all',
+ 'arc': [0,1,2],
+ 'arcTo': 'all',
+ 'bezierCurveTo': 'all',
+ 'isPointinPath': 'all',
+ 'isPointinStroke': 'all',
+ 'quadraticCurveTo': 'all',
+ 'rect': 'all',
+ 'translate': 'all',
+ 'createRadialGradient': 'all',
+ 'createLinearGradient': 'all'
+ };
+
+ if (pixelRatio === 1) return;
+
+ forEach(ratioArgs, function(value, key) {
+ prototype[key] = (function(_super) {
+ return function() {
+ var i, len,
+ args = Array.prototype.slice.call(arguments);
+
+ if (value === 'all') {
+ args = args.map(function(a) {
+ return a * pixelRatio;
+ });
+ }
+ else if (Array.isArray(value)) {
+ for (i = 0, len = value.length; i < len; i++) {
+ args[value[i]] *= pixelRatio;
+ }
+ }
+
+ return _super.apply(this, args);
+ };
+ })(prototype[key]);
+ });
+
+ // Stroke lineWidth adjustment
+ prototype.stroke = (function(_super) {
+ return function() {
+ this.lineWidth *= pixelRatio;
+ _super.apply(this, arguments);
+ this.lineWidth /= pixelRatio;
+ };
+ })(prototype.stroke);
+
+ // Text
+ //
+ prototype.fillText = (function(_super) {
+ return function() {
+ var args = Array.prototype.slice.call(arguments);
+
+ args[1] *= pixelRatio; // x
+ args[2] *= pixelRatio; // y
+
+ this.font = this.font.replace(
+ /(\d+)(px|em|rem|pt)/g,
+ function(w, m, u) {
+ return (m * pixelRatio) + u;
+ }
+ );
+
+ _super.apply(this, args);
+
+ this.font = this.font.replace(
+ /(\d+)(px|em|rem|pt)/g,
+ function(w, m, u) {
+ return (m / pixelRatio) + u;
+ }
+ );
+ };
+ })(prototype.fillText);
+
+ prototype.strokeText = (function(_super) {
+ return function() {
+ var args = Array.prototype.slice.call(arguments);
+
+ args[1] *= pixelRatio; // x
+ args[2] *= pixelRatio; // y
+
+ this.font = this.font.replace(
+ /(\d+)(px|em|rem|pt)/g,
+ function(w, m, u) {
+ return (m * pixelRatio) + u;
+ }
+ );
+
+ _super.apply(this, args);
+
+ this.font = this.font.replace(
+ /(\d+)(px|em|rem|pt)/g,
+ function(w, m, u) {
+ return (m / pixelRatio) + u;
+ }
+ );
+ };
+ })(prototype.strokeText);
+})(window.CanvasRenderingContext2D.prototype);
+;(function(prototype) {
+ prototype.getContext = (function(_super) {
+ return function(type) {
+ var backingStore, ratio, context;
+
+
+ if (type == '2d-lodpi') {
+ context = _super.call(this, '2d');
+ }
+ else if (type === '2d') {
+ context = _super.call(this, '2d');
+
+ backingStore = context.backingStorePixelRatio ||
+ context.webkitBackingStorePixelRatio ||
+ context.mozBackingStorePixelRatio ||
+ context.msBackingStorePixelRatio ||
+ context.oBackingStorePixelRatio ||
+ context.backingStorePixelRatio || 1;
+
+ ratio = (window.devicePixelRatio || 1) / backingStore;
+
+ if (ratio > 1) {
+ this.style.height = this.height + 'px';
+ this.style.width = this.width + 'px';
+ this.width *= ratio;
+ this.height *= ratio;
+ }
+ }
+ else {
+ context = _super.call(this, type);
+ }
+
+ return context;
+ };
+ })(prototype.getContext);
+})(window.HTMLCanvasElement.prototype);