Add support for ChangeFlag notifications to Raster.

This commit is contained in:
Jürg Lehni 2011-06-20 20:13:24 +01:00
parent ea9eacf578
commit 3aff54517e
2 changed files with 18 additions and 13 deletions

View file

@ -30,8 +30,10 @@ var ChangeFlag = {
ATTRIBUTE: 32, ATTRIBUTE: 32,
// Text content // Text content
CONTENT: 64, CONTENT: 64,
// Raster pixels
PIXELS: 128,
// Clipping in one of the child items // Clipping in one of the child items
CLIPPING: 128 CLIPPING: 256
}; };
// Shortcuts to often used ChangeFlag values including APPEARANCE // Shortcuts to often used ChangeFlag values including APPEARANCE
@ -41,5 +43,6 @@ var Change = {
STROKE: ChangeFlag.STROKE | ChangeFlag.STYLE | ChangeFlag.APPEARANCE, STROKE: ChangeFlag.STROKE | ChangeFlag.STYLE | ChangeFlag.APPEARANCE,
STYLE: ChangeFlag.STYLE | ChangeFlag.APPEARANCE, STYLE: ChangeFlag.STYLE | ChangeFlag.APPEARANCE,
ATTRIBUTE: ChangeFlag.ATTRIBUTE | ChangeFlag.APPEARANCE, ATTRIBUTE: ChangeFlag.ATTRIBUTE | ChangeFlag.APPEARANCE,
CONTENT: ChangeFlag.CONTENT | ChangeFlag.APPEARANCE CONTENT: ChangeFlag.CONTENT | ChangeFlag.APPEARANCE,
PIXELS: ChangeFlag.PIXELS | ChangeFlag.APPEARANCE
}; };

View file

@ -75,7 +75,7 @@ var Raster = this.Raster = Item.extend({
// Setting canvas internally sets _size // Setting canvas internally sets _size
this.setCanvas(CanvasProvider.getCanvas(size)); this.setCanvas(CanvasProvider.getCanvas(size));
// Draw image back onto new canvas // Draw image back onto new canvas
this.getContext().drawImage(image, 0, 0, size.width, size.height); this.getContext(true).drawImage(image, 0, 0, size.width, size.height);
}, },
/** /**
@ -122,9 +122,13 @@ var Raster = this.Raster = Item.extend({
* @bean * @bean
*/ */
getContext: function() { getContext: function() {
if (!this._context) { if (!this._context)
this._context = this.getCanvas().getContext('2d'); this._context = this.getCanvas().getContext('2d');
} // Support a hidden parameter that indicates if the context will be used
// to modify the Raster object. We can notify such changes ahead since
// they are only used afterwards for redrawing.
if (arguments[0])
this._changed(Change.PIXELS);
return this._context; return this._context;
}, },
@ -136,7 +140,7 @@ var Raster = this.Raster = Item.extend({
if (!this._canvas) { if (!this._canvas) {
this._canvas = CanvasProvider.getCanvas(this._size); this._canvas = CanvasProvider.getCanvas(this._size);
if (this._image) if (this._image)
this.getContext().drawImage(this._image, 0, 0); this.getContext(true).drawImage(this._image, 0, 0);
} }
return this._canvas; return this._canvas;
}, },
@ -148,8 +152,7 @@ var Raster = this.Raster = Item.extend({
this._size = new Size(canvas.width, canvas.height); this._size = new Size(canvas.width, canvas.height);
this._image = null; this._image = null;
this._context = null; this._context = null;
// TODO: _changed() this._changed(Change.GEOMETRY);
this._bounds = null;
}, },
/** /**
@ -171,8 +174,7 @@ var Raster = this.Raster = Item.extend({
this._size = new Size(image.naturalWidth, image.naturalHeight); this._size = new Size(image.naturalWidth, image.naturalHeight);
this._canvas = null; this._canvas = null;
this._context = null; this._context = null;
// TODO: _changed() this._changed(Change.GEOMETRY);
this._bounds = null;
}, },
// DOCS: document Raster#getSubImage // DOCS: document Raster#getSubImage
@ -199,7 +201,7 @@ var Raster = this.Raster = Item.extend({
*/ */
drawImage: function(image, point) { drawImage: function(image, point) {
point = Point.read(arguments, 1); point = Point.read(arguments, 1);
this.getContext().drawImage(image, point.x, point.y); this.getContext(true).drawImage(image, point.x, point.y);
}, },
/** /**
@ -320,7 +322,7 @@ var Raster = this.Raster = Item.extend({
var hasPoint = arguments.length == 2; var hasPoint = arguments.length == 2;
point = Point.read(arguments, 0, hasPoint ? 1 : 2); point = Point.read(arguments, 0, hasPoint ? 1 : 2);
color = Color.read(arguments, hasPoint ? 1 : 2); color = Color.read(arguments, hasPoint ? 1 : 2);
var ctx = this.getContext(), var ctx = this.getContext(true),
imageData = ctx.createImageData(1, 1), imageData = ctx.createImageData(1, 1),
alpha = color.getAlpha(); alpha = color.getAlpha();
imageData.data[0] = color.getRed() * 255; imageData.data[0] = color.getRed() * 255;
@ -362,7 +364,7 @@ var Raster = this.Raster = Item.extend({
*/ */
setData: function(data, point) { setData: function(data, point) {
point = Point.read(arguments, 1); point = Point.read(arguments, 1);
this.getContext().putImageData(data, point.x, point.y); this.getContext(true).putImageData(data, point.x, point.y);
}, },
_transform: function(matrix, flags) { _transform: function(matrix, flags) {