Skip to content

Commit 90f59e6

Browse files
committed
Add dpi/scale options for custom resolution
1 parent e58fc69 commit 90f59e6

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/core.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ function renderWindow(node, container, options, windowWidth, windowHeight) {
9999
canvas = crop(renderer.canvas, {width: renderer.canvas.width, height: renderer.canvas.height, top: 0, left: 0, x: 0, y: 0});
100100
} else if (node === clonedWindow.document.body || node === clonedWindow.document.documentElement || options.canvas != null) {
101101
canvas = renderer.canvas;
102+
} else if (options.scale) {
103+
var origBounds = {width: options.width != null ? options.width : bounds.width, height: options.height != null ? options.height : bounds.height, top: bounds.top, left: bounds.left, x: 0, y: 0};
104+
var cropBounds = {};
105+
for (var key in origBounds) {
106+
if (origBounds.hasOwnProperty(key)) { cropBounds[key] = origBounds[key] * options.scale; }
107+
}
108+
canvas = crop(renderer.canvas, cropBounds);
109+
canvas.style.width = origBounds.width + 'px';
110+
canvas.style.height = origBounds.height + 'px';
102111
} else {
103112
canvas = crop(renderer.canvas, {width: options.width != null ? options.width : bounds.width, height: options.height != null ? options.height : bounds.height, top: bounds.top, left: bounds.left, x: 0, y: 0});
104113
}

src/renderers/canvas.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,22 @@ var log = require('../log');
55
function CanvasRenderer(width, height) {
66
Renderer.apply(this, arguments);
77
this.canvas = this.options.canvas || this.document.createElement("canvas");
8+
this.ctx = this.canvas.getContext("2d");
89
if (!this.options.canvas) {
9-
this.canvas.width = width;
10-
this.canvas.height = height;
10+
if (this.options.dpi) {
11+
this.options.scale = this.options.dpi / 96; // 1 CSS inch = 96px.
12+
}
13+
if (this.options.scale) {
14+
this.canvas.style.width = width + 'px';
15+
this.canvas.style.height = height + 'px';
16+
this.canvas.width = Math.floor(width * this.options.scale);
17+
this.canvas.height = Math.floor(height * this.options.scale);
18+
this.ctx.scale(this.options.scale, this.options.scale);
19+
} else {
20+
this.canvas.width = width;
21+
this.canvas.height = height;
22+
}
1123
}
12-
this.ctx = this.canvas.getContext("2d");
1324
this.taintCtx = this.document.createElement("canvas").getContext("2d");
1425
this.ctx.textBaseline = "bottom";
1526
this.variables = {};

0 commit comments

Comments
 (0)