Remove pixelRatio handling from CanvasProvider and move it to Item#draw()

This commit is contained in:
Jürg Lehni 2014-05-15 17:14:37 +02:00
parent af5e1609dd
commit f39b7603de
2 changed files with 10 additions and 17 deletions

View file

@ -15,20 +15,13 @@
var CanvasProvider = { var CanvasProvider = {
canvases: [], canvases: [],
getCanvas: function(width, height, pixelRatio) { getCanvas: function(width, height) {
var canvas, var canvas,
init = true; clear = true;
if (typeof width === 'object') { if (typeof width === 'object') {
pixelRatio = height;
height = width.height; height = width.height;
width = width.width; width = width.width;
} }
if (!pixelRatio) {
pixelRatio = 1;
} else if (pixelRatio !== 1) {
width *= pixelRatio;
height *= pixelRatio;
}
if (this.canvases.length) { if (this.canvases.length) {
canvas = this.canvases.pop(); canvas = this.canvases.pop();
} else { } else {
@ -36,7 +29,7 @@ var CanvasProvider = {
canvas = document.createElement('canvas'); canvas = document.createElement('canvas');
/*#*/ } else { // __options.environment != 'browser' /*#*/ } else { // __options.environment != 'browser'
canvas = new Canvas(width, height); canvas = new Canvas(width, height);
init = false; // It's already initialized through constructor. clear = false; // It's already cleared through constructor.
/*#*/ } // __options.environment != 'browser' /*#*/ } // __options.environment != 'browser'
} }
var ctx = canvas.getContext('2d'); var ctx = canvas.getContext('2d');
@ -44,7 +37,7 @@ var CanvasProvider = {
// using clearRect and visa versa. // using clearRect and visa versa.
if (canvas.width === width && canvas.height === height) { if (canvas.width === width && canvas.height === height) {
// +1 is needed on some browsers to really clear the borders // +1 is needed on some browsers to really clear the borders
if (init) if (clear)
ctx.clearRect(0, 0, width + 1, height + 1); ctx.clearRect(0, 0, width + 1, height + 1);
} else { } else {
canvas.width = width; canvas.width = width;
@ -52,13 +45,11 @@ var CanvasProvider = {
} }
// We save on retrieval and restore on release. // We save on retrieval and restore on release.
ctx.save(); ctx.save();
if (pixelRatio !== 1)
ctx.scale(pixelRatio, pixelRatio);
return canvas; return canvas;
}, },
getContext: function(width, height, pixelRatio) { getContext: function(width, height) {
return this.getCanvas(width, height, pixelRatio).getContext('2d'); return this.getCanvas(width, height).getContext('2d');
}, },
// release can receive either a canvas or a context. // release can receive either a canvas or a context.

View file

@ -3661,8 +3661,10 @@ var Item = Base.extend(Callback, /** @lends Item# */{
// Set ctx to the context of the temporary canvas, so we draw onto // Set ctx to the context of the temporary canvas, so we draw onto
// it, instead of the mainCtx. // it, instead of the mainCtx.
mainCtx = ctx; mainCtx = ctx;
ctx = CanvasProvider.getContext( ctx = CanvasProvider.getContext(bounds.getSize().ceil().add(1)
bounds.getSize().ceil().add(new Size(1, 1)), pixelRatio); .multiply(pixelRatio));
if (pixelRatio !== 1)
ctx.scale(pixelRatio, pixelRatio);
} }
ctx.save(); ctx.save();
// Get the transformation matrix for non-scaling strokes. // Get the transformation matrix for non-scaling strokes.