Skip to content

Commit

Permalink
storing context settings before scaling and restore after
Browse files Browse the repository at this point in the history
  • Loading branch information
namniak committed Nov 29, 2015
1 parent f52bc54 commit 56bcbe6
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
canvas-text-wrapper
=================
##v0.6.3
## v0.6.4


## Syntax
Expand Down Expand Up @@ -63,8 +63,8 @@ CanvasTextWrapper(canvas, "Hello"); // default options will apply
```

## Test
In terminal go to ```canvas-text-wrapper``` folder do ```npm i``` and run ```npm t```

In terminal go to ```canvas-text-wrapper``` folder do ```npm i``` and run ```npm t```.
NOTE: Test requires ```beefy``` to be installed globally [beefy](http://didact.us/beefy/)

## Examples
[see here](http://namniak.github.io/canvas-text-wrapper/)
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "canvas-text-wrapper",
"version": "0.5.1",
"version": "0.6.4",
"ignore": [
"**/.*",
"**/*.log",
Expand Down
25 changes: 18 additions & 7 deletions canvas-text-wrapper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*! canvas-text-wrapper
* https://github.com/namniak/canvas-text-wrapper
* Version: 0.6.3
* Version: 0.6.4
* MIT License (http://www.opensource.org/licenses/mit-license.html)
*/

Expand Down Expand Up @@ -38,8 +38,13 @@

var scale = 1;
if (opts.renderHDPI && window.devicePixelRatio) {
var lineWidth = context.lineWidth;
var strokeStyle = context.strokeStyle;
var tempCtx = {};

This comment has been minimized.

Copy link
@martinpucala

martinpucala Nov 30, 2015

Contributor

could maybe the same be done through context's save() and restore() methods?

This comment has been minimized.

Copy link
@namniak

namniak Nov 30, 2015

Author Owner

theoretically yes. but unfortunately it's gonna loose context settings like lineWidth and gradient fills etc after setting new width/height. and storing/restoring context using those methods doesn't help. not quite sure why though. what I have is obviously not an elegant solution but that was quick and dirty fix which worked.


// store context settings in a temp object before scaling otherwise they will be lost
for (var key in context) {
tempCtx[key] = context[key];
}

var canvasWidth = canvas.width;
var canvasHeight = canvas.height;
scale = window.devicePixelRatio;
Expand All @@ -50,8 +55,14 @@
canvas.style.height = canvasHeight * scale * 0.5 + 'px';
context.scale(scale, scale);

context.lineWidth = lineWidth;
context.strokeStyle = strokeStyle;
// restore context settings
for (var key in tempCtx) {
try {
context[key] = tempCtx[key];
} catch (e) {

}
}
}

var EL_WIDTH = (!opts.fitParent ? canvas.width : canvas.parentNode.clientWidth) / scale;
Expand Down Expand Up @@ -285,8 +296,8 @@
if (typeof opts.strokeText !== 'boolean')
throw new TypeError('Property "strokeText" must be a Boolean.');

if (typeof opts.renderHDPI !== 'boolean')
throw new TypeError('Property "renderHDPI" must be a Boolean.');
if (typeof opts.renderHDPI !== 'boolean')
throw new TypeError('Property "renderHDPI" must be a Boolean.');
}
}

Expand Down
4 changes: 2 additions & 2 deletions canvas-text-wrapper.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "canvas-text-wrapper",
"description": "Split canvas text into lines on specified rule with optional alignment, padding, and more. Supports HDPI screens.",
"version": "0.6.3",
"version": "0.6.4",
"license": "MIT",
"main": "canvas-text-wrapper.min.js",
"keywords": [
Expand Down

0 comments on commit 56bcbe6

Please sign in to comment.