Skip to content

Commit

Permalink
Merge branch 'release/1.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
jondavidjohn committed Oct 26, 2013
2 parents 9addca8 + 07d6f42 commit 9addf35
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 51 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
= 1.0.1
* Fixed Bug detecting the webkit backingStore
* General code cleanup

= 1.0.0
* Initial Release
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ by the provided ratio.
```js
var getPixelRatio = function(context) {
var backingStore = context.backingStorePixelRatio ||
context.webkitBackingtorePixelRatio ||
context.webkitBackingStorePixelRatio ||
context.mozBackingStorePixelRatio ||
context.msBackingStorePixelRatio ||
context.oBackingStorePixelRatio ||
Expand All @@ -51,7 +51,7 @@ To use this module, simply include it before any of your canvas code
## TODO

- More Complete context function converage
- Figure out how to write tests
- Figure out how to write tests for this type of thing

## Development

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hidpi-canvas",
"description": "A JavaScript drop-in module to polyfill consistent and automatic HiDPI Canvas support.",
"version": "1.0.0",
"version": "1.0.1",
"license": "Apache 2.0",
"homepage": "https://github.com/jondavidjohn/hidpi-canvas-polyfill",
"bugs": "https://github.com/jondavidjohn/hidpi-canvas-polyfill/issues",
Expand Down
81 changes: 48 additions & 33 deletions src/CanvasRenderingContext2D.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
(function(prototype) {

var getPixelRatio = function(context) {
var func, value,

getPixelRatio = function(context) {
var backingStore = context.backingStorePixelRatio ||
context.webkitBackingtorePixelRatio ||
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',
Expand All @@ -24,32 +35,30 @@
'quadraticCurveTo': 'all',
'rect': 'all',
'translate': 'all'
},
func, value;

for (func in ratioArgs) {
if (ratioArgs.hasOwnProperty(func)) {
(function(value) {
prototype[func] = (function(_super) {
return function() {
var ratio = getPixelRatio(this), i, len,
args = Array.prototype.slice.call(arguments);

if (value === 'all') {
return _super.apply(this, args.map(function(a) { return a * ratio; }));
}
else if (Object.prototype.toString.call(value) == '[object Array]') {
for (i = 0, len = value.length; i < len; i++) {
args[value[i]] *= ratio;
}
return _super.apply(this, args);
}
};
})(prototype[func]);
})(ratioArgs[func]);
}
}
};

forEach(ratioArgs, function(value, key) {
prototype[key] = (function(_super) {
return function() {
var i, len,
ratio = getPixelRatio(this),
args = Array.prototype.slice.call(arguments);

if (value === 'all') {
args = args.map(function(a) {
return a * ratio;
});
}
else if (Array.isArray(value)) {
for (i = 0, len = value.length; i < len; i++) {
args[value[i]] *= ratio;
}
}

return _super.apply(this, args);
};
})(prototype[key]);
});

// Text
//
Expand All @@ -61,9 +70,12 @@
args[1] *= ratio; // x
args[2] *= ratio; // y

this.font = this.font.replace(/(\d+)(px|em|rem|pt)/g, function(w, m, u) {
return (m * ratio) + u;
});
this.font = this.font.replace(
/(\d+)(px|em|rem|pt)/g,
function(w, m, u) {
return (m * ratio) + u;
}
);

return _super.apply(this, args);
};
Expand All @@ -77,9 +89,12 @@
args[1] *= ratio; // x
args[2] *= ratio; // y

this.font = this.font.replace(/(\d+)(px|em|rem|pt)/g, function(w, m, u) {
return (m * ratio) + u;
});
this.font = this.font.replace(
/(\d+)(px|em|rem|pt)/g,
function(w, m, u) {
return (m * ratio) + u;
}
);

return _super.apply(this, args);
};
Expand Down
20 changes: 5 additions & 15 deletions src/HTMLCanvasElement.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
(function(prototype) {

var vendorPrefixes = [
'webkit',
'moz',
'ms',
'o'
];

prototype.getContext = (function(_super) {
return function(type) {
context = _super.call(this, type);
var backingStore, ratio,
context = _super.call(this, type);

if (type === '2d') {

var backingStore, ratio, v;

backingStore = context.backingStorePixelRatio ||
context.webkitBackingtorePixelRatio ||
context.webkitBackingStorePixelRatio ||
context.mozBackingStorePixelRatio ||
context.msBackingStorePixelRatio ||
context.oBackingStorePixelRatio ||
Expand All @@ -27,13 +18,12 @@
if (ratio > 1) {
this.style.height = this.height + 'px';
this.style.width = this.width + 'px';
this.width = this.width * ratio;
this.height = this.height * ratio;
this.width *= ratio;
this.height *= ratio;
}
}

return context;
};
})(prototype.getContext);

})(HTMLCanvasElement.prototype);

0 comments on commit 9addf35

Please sign in to comment.