Implement item#rasterize(resolution).

This commit is contained in:
Jonathan Puckey 2011-02-24 19:22:19 +01:00
parent 014d1053a7
commit d77741db4f

View file

@ -509,14 +509,29 @@ Item = Base.extend({
* Rasterizes the item into a newly created Raster object. The item itself
* is not removed after rasterization.
*
* @param type the color mode of the raster {@default same as document}
* @param resolution the resolution of the raster in dpi {@default 72}
* @param antialiasing the amount of anti-aliasing {@default 4}
* @param width {@default automatic}
* @param height {@default automatic}
* @return the newly created Raster item
*/
// TODO: rasterize
rasterize: function(resolution) {
// TODO: why would we want to pass a size to rasterize? Seems to produce
// weird results on Scriptographer. Also we can't use antialiasing, since
// Canvas doesn't support it yet. Document colorMode is also out of the
// question for now.
if(!resolution)
resolution = 72;
// TODO: use strokebounds for this:
var bounds = this.bounds;
var scale = resolution / 72;
var canvas = CanvasProvider.getCanvas(bounds.size.multiply(scale));
var context = canvas.getContext('2d');
var matrix = new Matrix().scale(scale).translate(-bounds.x, -bounds.y);
matrix.applyToContext(context);
this.draw(context);
var raster = new Raster(canvas);
raster.position = this.bounds.center;
raster.scale(1 / scale);
return raster;
},
/**
* The item's position within the art board. This is the