mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-08 05:42:07 -05:00
Shorten Raster#getAverageColor().
This commit is contained in:
parent
b103af4216
commit
e8d4a16ab2
1 changed files with 7 additions and 14 deletions
|
@ -188,27 +188,20 @@ var Raster = this.Raster = Item.extend({
|
||||||
} else if (object.x) {
|
} else if (object.x) {
|
||||||
bounds = Rectangle.create(object.x - 0.5, object.y - 0.5, 1, 1);
|
bounds = Rectangle.create(object.x - 0.5, object.y - 0.5, 1, 1);
|
||||||
}
|
}
|
||||||
var width = bounds.width,
|
var sampleSize = 32,
|
||||||
height = bounds.height,
|
width = Math.min(bounds.width, sampleSize),
|
||||||
sampleSize = 32,
|
height = Math.min(bounds.height, sampleSize);
|
||||||
scaleX = Math.min(sampleSize / width, 1),
|
var ctx = Raster._sampleContext;
|
||||||
scaleY = Math.min(sampleSize / height, 1),
|
if (!ctx) {
|
||||||
top = bounds.y,
|
|
||||||
left = bounds.x;
|
|
||||||
width *= scaleX;
|
|
||||||
height *= scaleY;
|
|
||||||
var ctx;
|
|
||||||
if (!Raster._sampleContext) {
|
|
||||||
ctx = Raster._sampleContext = CanvasProvider.getCanvas(
|
ctx = Raster._sampleContext = CanvasProvider.getCanvas(
|
||||||
new Size(sampleSize)).getContext('2d');
|
new Size(sampleSize)).getContext('2d');
|
||||||
} else {
|
} else {
|
||||||
ctx = Raster._sampleContext;
|
|
||||||
// Clear the sample canvas:
|
// Clear the sample canvas:
|
||||||
ctx.clearRect(0, 0, sampleSize, sampleSize);
|
ctx.clearRect(0, 0, sampleSize, sampleSize);
|
||||||
}
|
}
|
||||||
ctx.save();
|
ctx.save();
|
||||||
ctx.scale(scaleX, scaleY)
|
ctx.scale(width / bounds.width, height / bounds.height);
|
||||||
ctx.translate(-left, -top);
|
ctx.translate(-bounds.x, -bounds.y);
|
||||||
// If a path was passed, draw it as a clipping mask:
|
// If a path was passed, draw it as a clipping mask:
|
||||||
if (path) {
|
if (path) {
|
||||||
path.draw(ctx, { ignoreStyle: true });
|
path.draw(ctx, { ignoreStyle: true });
|
||||||
|
|
Loading…
Reference in a new issue