mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-01 02:38:43 -05:00
PlacedSymbol/ Raster: Use Matrix#applyToContext in draw functions. Also clean up Raster a bit.
This commit is contained in:
parent
5a786c300b
commit
cd5b2a7198
2 changed files with 12 additions and 20 deletions
|
@ -11,7 +11,7 @@ PlacedSymbol = Item.extend({
|
||||||
if (arguments.length > 1) {
|
if (arguments.length > 1) {
|
||||||
var arg = arguments[1];
|
var arg = arguments[1];
|
||||||
if (arg instanceof Matrix) {
|
if (arg instanceof Matrix) {
|
||||||
this.matrix = arguments[2]
|
this.matrix = arguments[2];
|
||||||
} else {
|
} else {
|
||||||
var offset = Point.read(arguments, 1);
|
var offset = Point.read(arguments, 1);
|
||||||
this.matrix = new Matrix().translate(offset);
|
this.matrix = new Matrix().translate(offset);
|
||||||
|
@ -41,11 +41,7 @@ PlacedSymbol = Item.extend({
|
||||||
draw: function(ctx) {
|
draw: function(ctx) {
|
||||||
// TODO: we need to preserve strokewidth, but still transform the fill
|
// TODO: we need to preserve strokewidth, but still transform the fill
|
||||||
ctx.save();
|
ctx.save();
|
||||||
var matrix = this.matrix;
|
this.matrix.applyToContext(ctx);
|
||||||
ctx.setTransform(
|
|
||||||
matrix._m00, matrix._m01, matrix._m10,
|
|
||||||
matrix._m11, matrix._m02, matrix._m12
|
|
||||||
);
|
|
||||||
this.symbol.definition.draw(ctx);
|
this.symbol.definition.draw(ctx);
|
||||||
ctx.restore();
|
ctx.restore();
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ Raster = Item.extend({
|
||||||
// or wait for user to call such a function and only then do so?
|
// or wait for user to call such a function and only then do so?
|
||||||
initialize: function(image) {
|
initialize: function(image) {
|
||||||
this.base();
|
this.base();
|
||||||
if(image) {
|
if (image) {
|
||||||
this.image = image;
|
this.image = image;
|
||||||
var width = image.width;
|
var width = image.width;
|
||||||
var height = image.height;
|
var height = image.height;
|
||||||
|
@ -48,21 +48,21 @@ Raster = Item.extend({
|
||||||
var ctx = tempCanvas.getContext('2d');
|
var ctx = tempCanvas.getContext('2d');
|
||||||
ctx.drawImage(this.image, 0, 0, size, size);
|
ctx.drawImage(this.image, 0, 0, size, size);
|
||||||
var pixels = ctx.getImageData(0.5, 0.5, size, size).data;
|
var pixels = ctx.getImageData(0.5, 0.5, size, size).data;
|
||||||
var rgba = [0, 0, 0];
|
var channels = [0, 0, 0];
|
||||||
|
|
||||||
for(var i = 0; i < size; i++) {
|
for (var i = 0; i < size; i++) {
|
||||||
var offset = i * size;
|
var offset = i * size;
|
||||||
var alpha = pixels[offset + 3] / 255;
|
var alpha = pixels[offset + 3] / 255;
|
||||||
rgba[0] += pixels[offset] * alpha;
|
channels[0] += pixels[offset] * alpha;
|
||||||
rgba[1] += pixels[offset + 1] * alpha;
|
channels[1] += pixels[offset + 1] * alpha;
|
||||||
rgba[2] += pixels[offset + 2] * alpha;
|
channels[2] += pixels[offset + 2] * alpha;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(var i = 0; i < 3; i++)
|
for (var i = 0; i < 3; i++)
|
||||||
rgba[i] = rgba[i] / (size * 255);
|
channels[i] /= size * 255;
|
||||||
|
|
||||||
CanvasProvider.returnCanvas(tempCanvas);
|
CanvasProvider.returnCanvas(tempCanvas);
|
||||||
return Color.read(rgba);
|
return Color.read(channels);
|
||||||
},
|
},
|
||||||
|
|
||||||
// TODO: getPixel(point)
|
// TODO: getPixel(point)
|
||||||
|
@ -95,11 +95,7 @@ Raster = Item.extend({
|
||||||
|
|
||||||
draw: function(ctx) {
|
draw: function(ctx) {
|
||||||
ctx.save();
|
ctx.save();
|
||||||
var matrix = this.matrix;
|
this.matrix.applyToContext(ctx);
|
||||||
ctx.setTransform(
|
|
||||||
matrix._m00, matrix._m01, matrix._m10,
|
|
||||||
matrix._m11, matrix._m02, matrix._m12
|
|
||||||
);
|
|
||||||
var image = this.image;
|
var image = this.image;
|
||||||
ctx.drawImage(this.image, -this.size.width / 2, -this.size.height / 2);
|
ctx.drawImage(this.image, -this.size.width / 2, -this.size.height / 2);
|
||||||
ctx.restore();
|
ctx.restore();
|
||||||
|
|
Loading…
Reference in a new issue