mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-05 20:32:00 -05:00
Improve Raster#getAverageColor.
This commit is contained in:
parent
3f3f6dc575
commit
8810563a8f
2 changed files with 14 additions and 14 deletions
|
@ -249,7 +249,6 @@ var Raster = this.Raster = Item.extend({
|
||||||
if (object instanceof PathItem) {
|
if (object instanceof PathItem) {
|
||||||
// TODO: what if the path is smaller than 1 px?
|
// TODO: what if the path is smaller than 1 px?
|
||||||
// TODO: how about rounding of bounds.size?
|
// TODO: how about rounding of bounds.size?
|
||||||
// TODO: test with compound paths.
|
|
||||||
path = object;
|
path = object;
|
||||||
bounds = object.getBounds();
|
bounds = object.getBounds();
|
||||||
} else if (object.width) {
|
} 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,
|
bounds = new Rectangle(object.x - 0.5, object.y - 0.5,
|
||||||
1, 1);
|
1, 1);
|
||||||
}
|
}
|
||||||
|
var size = bounds.getSize(),
|
||||||
var canvas = CanvasProvider.getCanvas(bounds.getSize()),
|
canvas = CanvasProvider.getCanvas(size),
|
||||||
ctx = canvas.getContext('2d'),
|
ctx = canvas.getContext('2d'),
|
||||||
delta = bounds.getTopLeft().multiply(-1);
|
delta = bounds.getTopLeft();
|
||||||
ctx.translate(delta.x, delta.y);
|
ctx.save();
|
||||||
|
ctx.translate(-delta.x, -delta.y);
|
||||||
if (path) {
|
if (path) {
|
||||||
var style = object.getStyle();
|
path.draw(ctx, { ignoreStyle: true });
|
||||||
path.draw(ctx, {});
|
|
||||||
ctx.clip();
|
ctx.clip();
|
||||||
path.setStyle(style);
|
|
||||||
}
|
}
|
||||||
this.matrix.applyToContext(ctx);
|
this.matrix.applyToContext(ctx);
|
||||||
ctx.drawImage(this._canvas || this._image,
|
ctx.drawImage(this._canvas || this._image,
|
||||||
-this._size.width / 2, -this._size.height / 2);
|
-this._size.width / 2, -this._size.height / 2);
|
||||||
image = canvas;
|
image = canvas;
|
||||||
|
ctx.restore();
|
||||||
} else {
|
} else {
|
||||||
image = this.image;
|
image = this.image;
|
||||||
}
|
}
|
||||||
var size = new Size(32),
|
var sampleSize = Size.min(size, new Size(32, 32)),
|
||||||
sampleCanvas = CanvasProvider.getCanvas(size),
|
width = sampleSize.width,
|
||||||
|
height = sampleSize.height,
|
||||||
|
sampleCanvas = CanvasProvider.getCanvas(sampleSize),
|
||||||
ctx = sampleCanvas.getContext('2d');
|
ctx = sampleCanvas.getContext('2d');
|
||||||
ctx.drawImage(image, 0, 0, size.width, size.height);
|
ctx.drawImage(image, 0, 0, width, height);
|
||||||
var pixels = ctx.getImageData(0.5, 0.5,
|
var pixels = ctx.getImageData(0.5, 0.5, width, height).data,
|
||||||
size.width, size.height).data,
|
|
||||||
color = getAverageColor(pixels);
|
color = getAverageColor(pixels);
|
||||||
CanvasProvider.returnCanvas(sampleCanvas);
|
CanvasProvider.returnCanvas(sampleCanvas);
|
||||||
if (image instanceof HTMLCanvasElement)
|
if (image instanceof HTMLCanvasElement)
|
||||||
|
|
|
@ -580,7 +580,7 @@ var Path = this.Path = PathItem.extend({
|
||||||
if (param.selection) {
|
if (param.selection) {
|
||||||
ctx.stroke();
|
ctx.stroke();
|
||||||
drawHandles(ctx, this._segments);
|
drawHandles(ctx, this._segments);
|
||||||
} else {
|
} else if (!param.ignoreStyle) {
|
||||||
// If the path is part of a compound path or doesn't have a fill
|
// If the path is part of a compound path or doesn't have a fill
|
||||||
// or stroke, there is no need to continue.
|
// or stroke, there is no need to continue.
|
||||||
var fillColor = this.getFillColor(),
|
var fillColor = this.getFillColor(),
|
||||||
|
|
Loading…
Reference in a new issue