Skip to content

Commit

Permalink
Add dpi/scale options for custom resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
eKoopmans committed Mar 28, 2017
1 parent e58fc69 commit 90f59e6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ function renderWindow(node, container, options, windowWidth, windowHeight) {
canvas = crop(renderer.canvas, {width: renderer.canvas.width, height: renderer.canvas.height, top: 0, left: 0, x: 0, y: 0});
} else if (node === clonedWindow.document.body || node === clonedWindow.document.documentElement || options.canvas != null) {
canvas = renderer.canvas;
} else if (options.scale) {
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};
var cropBounds = {};
for (var key in origBounds) {
if (origBounds.hasOwnProperty(key)) { cropBounds[key] = origBounds[key] * options.scale; }
}
canvas = crop(renderer.canvas, cropBounds);
canvas.style.width = origBounds.width + 'px';
canvas.style.height = origBounds.height + 'px';
} else {
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});
}
Expand Down
17 changes: 14 additions & 3 deletions src/renderers/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@ var log = require('../log');
function CanvasRenderer(width, height) {
Renderer.apply(this, arguments);
this.canvas = this.options.canvas || this.document.createElement("canvas");
this.ctx = this.canvas.getContext("2d");
if (!this.options.canvas) {
this.canvas.width = width;
this.canvas.height = height;
if (this.options.dpi) {
this.options.scale = this.options.dpi / 96; // 1 CSS inch = 96px.
}
if (this.options.scale) {
this.canvas.style.width = width + 'px';
this.canvas.style.height = height + 'px';
this.canvas.width = Math.floor(width * this.options.scale);
this.canvas.height = Math.floor(height * this.options.scale);
this.ctx.scale(this.options.scale, this.options.scale);
} else {
this.canvas.width = width;
this.canvas.height = height;
}
}
this.ctx = this.canvas.getContext("2d");
this.taintCtx = this.document.createElement("canvas").getContext("2d");
this.ctx.textBaseline = "bottom";
this.variables = {};
Expand Down

0 comments on commit 90f59e6

Please sign in to comment.