From 8810563a8f6138e5a647f5b7bcbd86f4f80d8e9d Mon Sep 17 00:00:00 2001 From: Jonathan Puckey Date: Wed, 18 May 2011 16:00:55 +0200 Subject: [PATCH] Improve Raster#getAverageColor. --- src/item/Raster.js | 26 +++++++++++++------------- src/path/Path.js | 2 +- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/item/Raster.js b/src/item/Raster.js index b5727583..83fb23ce 100644 --- a/src/item/Raster.js +++ b/src/item/Raster.js @@ -249,7 +249,6 @@ var Raster = this.Raster = Item.extend({ if (object instanceof PathItem) { // TODO: what if the path is smaller than 1 px? // TODO: how about rounding of bounds.size? - // TODO: test with compound paths. path = object; bounds = object.getBounds(); } else if (object.width) { @@ -258,30 +257,31 @@ var Raster = this.Raster = Item.extend({ bounds = new Rectangle(object.x - 0.5, object.y - 0.5, 1, 1); } - - var canvas = CanvasProvider.getCanvas(bounds.getSize()), + var size = bounds.getSize(), + canvas = CanvasProvider.getCanvas(size), ctx = canvas.getContext('2d'), - delta = bounds.getTopLeft().multiply(-1); - ctx.translate(delta.x, delta.y); + delta = bounds.getTopLeft(); + ctx.save(); + ctx.translate(-delta.x, -delta.y); if (path) { - var style = object.getStyle(); - path.draw(ctx, {}); + path.draw(ctx, { ignoreStyle: true }); ctx.clip(); - path.setStyle(style); } this.matrix.applyToContext(ctx); ctx.drawImage(this._canvas || this._image, -this._size.width / 2, -this._size.height / 2); image = canvas; + ctx.restore(); } else { image = this.image; } - var size = new Size(32), - sampleCanvas = CanvasProvider.getCanvas(size), + var sampleSize = Size.min(size, new Size(32, 32)), + width = sampleSize.width, + height = sampleSize.height, + sampleCanvas = CanvasProvider.getCanvas(sampleSize), ctx = sampleCanvas.getContext('2d'); - ctx.drawImage(image, 0, 0, size.width, size.height); - var pixels = ctx.getImageData(0.5, 0.5, - size.width, size.height).data, + ctx.drawImage(image, 0, 0, width, height); + var pixels = ctx.getImageData(0.5, 0.5, width, height).data, color = getAverageColor(pixels); CanvasProvider.returnCanvas(sampleCanvas); if (image instanceof HTMLCanvasElement) diff --git a/src/path/Path.js b/src/path/Path.js index 4bb1e4ac..2b9b2267 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -580,7 +580,7 @@ var Path = this.Path = PathItem.extend({ if (param.selection) { ctx.stroke(); drawHandles(ctx, this._segments); - } else { + } else if (!param.ignoreStyle) { // If the path is part of a compound path or doesn't have a fill // or stroke, there is no need to continue. var fillColor = this.getFillColor(),