2011-02-21 12:43:56 -05:00
|
|
|
Raster = Item.extend({
|
|
|
|
beans: true,
|
|
|
|
|
|
|
|
// TODO: implement url / type, width, height
|
|
|
|
// TODO: have PlacedSymbol & Raster inherit from a shared class?
|
|
|
|
initialize: function(image) {
|
|
|
|
this.base();
|
2011-02-21 13:04:51 -05:00
|
|
|
if (image) {
|
2011-02-21 12:43:56 -05:00
|
|
|
this.image = image;
|
|
|
|
var width = image.width;
|
|
|
|
var height = image.height;
|
|
|
|
this.size = new Size(width, height);
|
|
|
|
this._bounds = new Rectangle(-width / 2, -height / 2, width, height);
|
|
|
|
this.matrix = new Matrix();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// TODO: getSize / setSize
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The width of the raster in pixels.
|
|
|
|
*/
|
|
|
|
getWidth: function() {
|
|
|
|
return this.size.width;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The height of the raster in pixels.
|
|
|
|
*/
|
|
|
|
getHeight: function() {
|
|
|
|
return this.size.height;
|
|
|
|
},
|
|
|
|
|
|
|
|
// TODO: getPpi
|
|
|
|
// TODO: getSubImage
|
|
|
|
// TODO: getImage
|
|
|
|
// TODO: drawImage
|
|
|
|
|
|
|
|
// TODO: support getAverageColor paramaters: point, rect, path
|
2011-02-21 12:49:44 -05:00
|
|
|
// TODO: Idea for getAverageColor(path): set globalCompositeOperation = 'xor',
|
|
|
|
// then fillRect with black, then draw the path, then draw the image, then
|
|
|
|
// resize and count values.
|
2011-02-21 12:43:56 -05:00
|
|
|
getAverageColor: function() {
|
|
|
|
var size = 32;
|
|
|
|
var tempCanvas = CanvasProvider.getCanvas(size, size);
|
|
|
|
var ctx = tempCanvas.getContext('2d');
|
|
|
|
ctx.drawImage(this.image, 0, 0, size, size);
|
|
|
|
var pixels = ctx.getImageData(0.5, 0.5, size, size).data;
|
2011-02-21 13:04:51 -05:00
|
|
|
var channels = [0, 0, 0];
|
2011-02-21 12:43:56 -05:00
|
|
|
|
2011-02-21 13:04:51 -05:00
|
|
|
for (var i = 0; i < size; i++) {
|
2011-02-21 12:43:56 -05:00
|
|
|
var offset = i * size;
|
|
|
|
var alpha = pixels[offset + 3] / 255;
|
2011-02-21 13:04:51 -05:00
|
|
|
channels[0] += pixels[offset] * alpha;
|
|
|
|
channels[1] += pixels[offset + 1] * alpha;
|
|
|
|
channels[2] += pixels[offset + 2] * alpha;
|
2011-02-21 12:43:56 -05:00
|
|
|
}
|
|
|
|
|
2011-02-21 13:04:51 -05:00
|
|
|
for (var i = 0; i < 3; i++)
|
|
|
|
channels[i] /= size * 255;
|
2011-02-21 12:43:56 -05:00
|
|
|
|
|
|
|
CanvasProvider.returnCanvas(tempCanvas);
|
2011-02-21 13:04:51 -05:00
|
|
|
return Color.read(channels);
|
2011-02-21 12:43:56 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
// TODO: getPixel(point)
|
|
|
|
// TODO: test this
|
|
|
|
getPixel: function(x, y) {
|
2011-02-21 13:52:41 -05:00
|
|
|
var pixels = this.context.getImageData(x + 0.5, y + 0.5, 1, 1).data;
|
|
|
|
var channels = [];
|
|
|
|
for(var i = 0; i < 4; i++)
|
|
|
|
channels.push(pixels[i] / 255);
|
2011-02-21 12:43:56 -05:00
|
|
|
return Color.read(channels);
|
|
|
|
},
|
|
|
|
|
|
|
|
// TODO: setPixel(point, color)
|
|
|
|
// setPixel: function(x, y, color) {
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
|
2011-02-21 13:52:41 -05:00
|
|
|
getContext: function() {
|
|
|
|
if (!this._context)
|
|
|
|
this._context = this.canvas.getContext('2d');
|
|
|
|
return this._context;
|
|
|
|
},
|
|
|
|
|
|
|
|
setContext: function(context) {
|
|
|
|
this._context = context;
|
|
|
|
},
|
|
|
|
|
|
|
|
getCanvas: function() {
|
|
|
|
if (!this._canvas) {
|
|
|
|
this._canvas = CanvasProvider.getCanvas(this.size.width, this.size.height);
|
|
|
|
this.ctx = this._canvas.getContext('2d');
|
|
|
|
this.ctx.drawImage(this.image, 0, 0);
|
|
|
|
}
|
|
|
|
return this._canvas;
|
|
|
|
},
|
|
|
|
|
|
|
|
setCanvas: function(canvas) {
|
|
|
|
CanvasProvider.returnCanvas(this._canvas);
|
|
|
|
this._ctx = null;
|
|
|
|
this._canvas = canvas;
|
|
|
|
},
|
|
|
|
|
2011-02-21 12:43:56 -05:00
|
|
|
transformContent: function(matrix, flags) {
|
|
|
|
var bounds = this.bounds;
|
|
|
|
var coords = [bounds.x, bounds.y,
|
|
|
|
bounds.x + bounds.width, bounds.y + bounds.height];
|
|
|
|
matrix.transform(coords, 0, coords, 0, 2);
|
|
|
|
this.matrix.preConcatenate(matrix);
|
|
|
|
bounds.x = coords[0];
|
|
|
|
bounds.y = coords[1];
|
|
|
|
bounds.width = coords[2] - coords[0];
|
|
|
|
bounds.height = coords[3] - coords[1];
|
|
|
|
},
|
|
|
|
|
|
|
|
getBounds: function() {
|
|
|
|
return this._bounds;
|
|
|
|
},
|
|
|
|
|
|
|
|
draw: function(ctx) {
|
|
|
|
ctx.save();
|
2011-02-21 13:04:51 -05:00
|
|
|
this.matrix.applyToContext(ctx);
|
2011-02-21 13:52:41 -05:00
|
|
|
var image = this._canvas ? this._canvas : this.image;
|
|
|
|
ctx.drawImage(image, -this.size.width / 2, -this.size.height / 2);
|
2011-02-21 12:43:56 -05:00
|
|
|
ctx.restore();
|
|
|
|
}
|
|
|
|
});
|