Rename Raster#getSubImage() to #getSubCanvas(), and use insert: false combined with #insertAbove() to correctly insert newly created rasters.

This commit is contained in:
Jürg Lehni 2013-08-14 12:21:47 -07:00
parent 5981ddf89f
commit cc565b3fe2
3 changed files with 15 additions and 7 deletions

View file

@ -1307,9 +1307,13 @@ var Item = Base.extend(Callback, /** @lends Item# */{
matrix.applyToContext(ctx);
// See Project#draw() for an explanation of Base.merge()
this.draw(ctx, Base.merge({ transforms: [matrix] }));
var raster = new Raster(canvas);
raster.setPosition(topLeft.add(size.divide(2)));
ctx.restore();
var raster = new Raster({
canvas: canvas,
insert: false
});
raster.setPosition(topLeft.add(size.divide(2)));
raster.insertAbove(this);
// NOTE: We don't need to release the canvas since it now belongs to the
// Raster!
return raster;

View file

@ -244,7 +244,7 @@ var Raster = Item.extend(/** @lends Raster# */{
this._size = new Size(image.width, image.height);
this._canvas = null;
this._context = null;
this._changed(/*#=*/ Change.GEOMETRY);
this._changed(/*#=*/ Change.GEOMETRY | /*#=*/ Change.PIXELS);
},
/**
@ -330,7 +330,7 @@ var Raster = Item.extend(/** @lends Raster# */{
*
* @return {Canvas} the sub image as a Canvas object
*/
getSubImage: function(rect) {
getSubCanvas: function(rect) {
rect = Rectangle.read(arguments);
var ctx = CanvasProvider.getContext(rect.getSize());
ctx.drawImage(this.getCanvas(), rect.x, rect.y,
@ -349,9 +349,13 @@ var Raster = Item.extend(/** @lends Raster# */{
*/
getSubRaster: function(rect) {
rect = Rectangle.read(arguments);
var raster = new Raster(this.getSubImage(rect));
var raster = new Raster({
canvas: this.getSubCanvas(rect),
insert: false
});
raster.translate(rect.getCenter().subtract(this.getSize().divide(2)));
raster._matrix.preConcatenate(this._matrix);
raster.insertAbove(this);
return raster;
},

View file

@ -91,10 +91,10 @@ asyncTest('Raster#getPixel / setPixel', function(callback) {
};
});
asyncTest('Raster#getSubImage', function(callback) {
asyncTest('Raster#getSubCanvas', function(callback) {
var raster = new Raster('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAABlJREFUeNpi+s/AwPCfgYmR4f9/hv8AAQYAHiAFAS8Lwy8AAAAASUVORK5CYII=');
raster.onLoad = function() {
var canvas = raster.getSubImage(new Rectangle({
var canvas = raster.getSubCanvas(new Rectangle({
point: [1, 0],
size: [1, 2]
}));