paper.js/src/item/Raster.js

395 lines
10 KiB
JavaScript
Raw Normal View History

2011-03-06 19:50:44 -05:00
/*
* Paper.js
*
* This file is part of Paper.js, a JavaScript Vector Graphics Library,
* based on Scriptographer.org and designed to be largely API compatible.
2011-03-07 20:41:50 -05:00
* http://paperjs.org/
2011-03-06 19:50:44 -05:00
* http://scriptographer.org/
*
2011-03-07 20:41:50 -05:00
* Distributed under the MIT license. See LICENSE file for details.
*
2011-03-06 19:50:44 -05:00
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
* http://lehni.org/ & http://jonathanpuckey.com/
*
2011-03-07 20:41:50 -05:00
* All rights reserved.
2011-03-06 19:50:44 -05:00
*/
var Raster = this.Raster = Item.extend({
2011-05-23 10:09:04 -04:00
/** @lends Raster# */
2011-02-21 12:43:56 -05:00
beans: true,
// TODO: implement url / type, width, height
// TODO: have PlacedSymbol & Raster inherit from a shared class?
2011-05-23 10:09:04 -04:00
// DOCS: document Raster constructor
/**
* Creates a new raster item and places it in the active layer.
*
* @constructs Raster
* @param {HTMLImageElement|Canvas|string} [object]
*
* @class The Raster item represents an image in a Paper.js project.
*
* @extends Item
*/
initialize: function(object) {
2011-02-21 12:43:56 -05:00
this.base();
if (object.getContext) {
this.setCanvas(object);
} else {
//#ifdef BROWSER
// If it's a string, get the element with this id first.
if (typeof object === 'string')
object = document.getElementById(object);
//#endif // BROWSER
this.setImage(object);
2011-02-21 12:43:56 -05:00
}
this.matrix = new Matrix();
2011-02-21 12:43:56 -05:00
},
clone: function() {
2011-05-21 06:50:02 -04:00
var image = this._image;
if (!image) {
// If the Raster contains a Canvas object, we need to create
// a new one and draw this raster's canvas on it.
image = CanvasProvider.getCanvas(this._size);
image.getContext('2d').drawImage(this._canvas, 0, 0);
}
2011-05-21 12:02:09 -04:00
var copy = new Raster(image);
copy.matrix = this.matrix.clone();
return this._clone(copy);
},
/**
2011-05-23 10:09:04 -04:00
* The size of the raster in pixels.
*
* @type Size
* @bean
*/
getSize: function() {
return this._size;
},
setSize: function() {
var size = Size.read(arguments),
// Get reference to image before changing canvas
image = this.getImage();
2011-03-04 21:40:38 -05:00
// Setting canvas internally sets _size
this.setCanvas(CanvasProvider.getCanvas(size));
2011-03-04 21:40:38 -05:00
// Draw image back onto new canvas
2011-03-06 09:08:41 -05:00
this.getContext().drawImage(image, 0, 0, size.width, size.height);
},
2011-02-21 12:43:56 -05:00
/**
* The width of the raster in pixels.
2011-05-23 10:09:04 -04:00
*
* @type number
* @bean
2011-02-21 12:43:56 -05:00
*/
getWidth: function() {
return this._size.width;
2011-02-21 12:43:56 -05:00
},
2011-02-21 12:43:56 -05:00
/**
* The height of the raster in pixels.
2011-05-23 10:09:04 -04:00
*
* @type number
* @bean
2011-02-21 12:43:56 -05:00
*/
getHeight: function() {
return this._size.height;
2011-02-21 12:43:56 -05:00
},
/**
* Pixels per inch of the raster at it's current size.
2011-05-23 10:09:04 -04:00
*
* @type Size
* @bean
*/
getPpi: function() {
2011-05-02 06:23:42 -04:00
var matrix = this.matrix,
orig = new Point(0, 0).transform(matrix),
u = new Point(1, 0).transform(matrix).subtract(orig),
v = new Point(0, 1).transform(matrix).subtract(orig);
return new Size(
2011-03-04 21:40:38 -05:00
72 / u.getLength(),
72 / v.getLength()
);
},
2011-05-23 10:09:04 -04:00
/**
* The Canvas 2d drawing context of the raster.
*
* @type Context
* @bean
*/
2011-03-04 21:40:38 -05:00
getContext: function() {
if (!this._context) {
this._context = this.getCanvas().getContext('2d');
}
return this._context;
},
setContext: function(context) {
this._context = context;
},
getCanvas: function() {
if (!this._canvas) {
this._canvas = CanvasProvider.getCanvas(this._size);
if (this._image)
this.getContext().drawImage(this._image, 0, 0);
}
return this._canvas;
},
setCanvas: function(canvas) {
if (this._canvas)
CanvasProvider.returnCanvas(this._canvas);
this._canvas = canvas;
this._size = new Size(canvas.width, canvas.height);
this._image = null;
this._context = null;
this._bounds = null;
},
2011-05-23 10:09:04 -04:00
/**
* The HTMLImageElement or Canvas of the raster.
*
* @type HTMLImageElement|Canvas
* @bean
*/
getImage: function() {
return this._image || this.getCanvas();
},
2011-05-23 10:09:04 -04:00
// TODO: support string id of image element
setImage: function(image) {
if (this._canvas)
CanvasProvider.returnCanvas(this._canvas);
this._image = image;
// TODO: cross browser compatible?
this._size = new Size(image.naturalWidth, image.naturalHeight);
this._canvas = null;
this._context = null;
this._bounds = null;
},
2011-05-23 10:09:04 -04:00
// DOCS: document Raster#getSubImage
/**
2011-05-23 10:19:37 -04:00
* @param {Rectangle} rect the boundaries of the sub image in pixel
* coordinates
*
2011-05-23 10:09:04 -04:00
* @return {Canvas}
*/
getSubImage: function(rect) {
rect = Rectangle.read(arguments);
var canvas = CanvasProvider.getCanvas(rect.getSize());
canvas.getContext('2d').drawImage(this.getCanvas(), rect.x, rect.y,
canvas.width, canvas.height, 0, 0, canvas.width, canvas.height);
return canvas;
},
2011-05-23 10:09:04 -04:00
/**
2011-05-23 10:19:37 -04:00
* Draws an image on the raster.
*
2011-05-23 10:09:04 -04:00
* @param {HTMLImageELement|Canvas} image
2011-05-23 10:19:37 -04:00
* @param {Point} point the offset of the image as a point in pixel
* coordinates
2011-05-23 10:09:04 -04:00
*/
drawImage: function(image, point) {
point = Point.read(arguments, 1);
this.getContext().drawImage(image, point.x, point.y);
},
/**
* Gets the color of a pixel in the raster.
2011-05-23 10:09:04 -04:00
*
* @name Raster#getPixel^2
* @function
* @param x the x offset of the pixel in pixel coordinates
* @param y the y offset of the pixel in pixel coordinates
* @return {RGBColor} the color of the pixel
*/
/**
* Gets the color of a pixel in the raster.
* @param point the offset of the pixel as a point in pixel coordinates
* @return {RGBColor} the color of the pixel
*/
getPixel: function(point) {
point = Point.read(arguments);
var pixels = this.getContext().getImageData(point.x, point.y, 1, 1).data,
channels = new Array(4);
for (var i = 0; i < 4; i++)
channels[i] = pixels[i] / 255;
return RGBColor.read(channels);
2011-02-21 12:43:56 -05:00
},
2011-05-23 10:09:04 -04:00
/**
* Sets the color of the specified pixel to the specified color.
*
* @name Raster#setPixel^2
* @function
* @param x the x offset of the pixel in pixel coordinates
* @param y the y offset of the pixel in pixel coordinates
* @param color the color that the pixel will be set to
*/
/**
* Sets the color of the specified pixel to the specified color.
* @param point the offset of the pixel as a point in pixel coordinates
* @param color the color that the pixel will be set to
*/
setPixel: function(point, color) {
var hasPoint = arguments.length == 2;
point = Point.read(arguments, 0, hasPoint ? 1 : 2);
color = Color.read(arguments, hasPoint ? 1 : 2);
var ctx = this.getContext(),
imageData = ctx.createImageData(1, 1),
alpha = color.getAlpha();
imageData.data[0] = color.getRed() * 255;
imageData.data[1] = color.getGreen() * 255;
imageData.data[2] = color.getBlue() * 255;
imageData.data[3] = alpha != null ? alpha * 255 : 255;
ctx.putImageData(imageData, point.x, point.y);
},
2011-05-19 09:43:23 -04:00
/**
* Calculates the average color of the image within the given path,
* rectangle or point. This can be used for creating raster image
* effects.
*
2011-05-23 10:09:04 -04:00
* @param {Path|Rectangle|Point} object
* @return {RGBColor} the average color contained in the area covered by the
2011-05-19 09:43:23 -04:00
* specified path, rectangle or point.
*/
getAverageColor: function(object) {
if (!object)
object = this.getBounds();
var bounds, path;
if (object instanceof PathItem) {
// TODO: what if the path is smaller than 1 px?
// TODO: how about rounding of bounds.size?
path = object;
bounds = object.getBounds();
} else if (object.width) {
bounds = new Rectangle(object);
} else if (object.x) {
bounds = Rectangle.create(object.x - 0.5, object.y - 0.5, 1, 1);
}
// Use a sample size of max 32 x 32 pixels, into which the path is
// scaled as a clipping path, and then the actual image is drawn in and
// sampled.
2011-05-19 13:47:49 -04:00
var sampleSize = 32,
width = Math.min(bounds.width, sampleSize),
height = Math.min(bounds.height, sampleSize);
// Reuse the same sample context for speed. Memory consumption is low
// since it's only 32 x 32 pixels.
2011-05-19 13:47:49 -04:00
var ctx = Raster._sampleContext;
if (!ctx) {
2011-05-19 09:43:23 -04:00
ctx = Raster._sampleContext = CanvasProvider.getCanvas(
new Size(sampleSize)).getContext('2d');
} else {
// Clear the sample canvas:
ctx.clearRect(0, 0, sampleSize, sampleSize);
}
ctx.save();
// Scale the context so that the bounds ends up at the given sample size
2011-05-19 13:47:49 -04:00
ctx.scale(width / bounds.width, height / bounds.height);
ctx.translate(-bounds.x, -bounds.y);
2011-05-19 09:43:23 -04:00
// If a path was passed, draw it as a clipping mask:
if (path) {
path.draw(ctx, { ignoreStyle: true });
ctx.clip();
}
// Now draw the image clipped into it.
2011-05-19 09:43:23 -04:00
this.matrix.applyToContext(ctx);
ctx.drawImage(this._canvas || this._image,
-this._size.width / 2, -this._size.height / 2);
ctx.restore();
// Get pixel data from the context and calculate the average color value
// from it, taking alpha into account.
2011-05-19 09:43:23 -04:00
var pixels = ctx.getImageData(0.5, 0.5, Math.ceil(width),
Math.ceil(height)).data,
channels = [0, 0, 0],
total = 0;
for (var i = 0, l = pixels.length; i < l; i += 4) {
var alpha = pixels[i + 3];
total += alpha;
alpha /= 255;
channels[0] += pixels[i] * alpha;
channels[1] += pixels[i + 1] * alpha;
channels[2] += pixels[i + 2] * alpha;
}
for (var i = 0; i < 3; i++)
channels[i] /= total;
return total ? Color.read(channels) : null;
},
2011-05-23 10:19:37 -04:00
// DOCS: document Raster#createData
/**
* @param {Size} size
* @return {ImageData}
*/
2011-03-09 13:17:12 -05:00
createData: function(size) {
size = Size.read(arguments);
return this.getContext().createImageData(size.width, size.height);
},
2011-05-23 10:19:37 -04:00
// DOCS: document Raster#getData
/**
* @param {Rectangle} rect
* @return {ImageData}
*/
getData: function(rect) {
rect = Rectangle.read(arguments);
if (rect.isEmpty())
rect = new Rectangle(this.getSize());
return this.getContext().getImageData(rect.x, rect.y,
rect.width, rect.height);
2011-03-09 13:17:12 -05:00
},
2011-05-23 10:19:37 -04:00
// DOCS: document Raster#setData
/**
* @param {ImageData} data
* @param {Point} point
* @return {ImageData}
*/
setData: function(data, point) {
point = Point.read(arguments, 1);
this.getContext().putImageData(data, point.x, point.y);
2011-03-09 13:17:12 -05:00
},
_transform: function(matrix, flags) {
// In order to set the right context transformation when drawing the
// raster, simply preconcatenate the internal matrix with the provided
// one.
this.matrix.preConcatenate(matrix);
this._bounds = null;
2011-02-21 12:43:56 -05:00
},
2011-02-21 12:43:56 -05:00
getBounds: function() {
if (!this._bounds) {
this._bounds = this.matrix._transformBounds(
new Rectangle(this._size).setCenter(0, 0));
}
2011-02-21 12:43:56 -05:00
return this._bounds;
},
2011-04-28 06:56:08 -04:00
getStrokeBounds: function() {
return this.getBounds();
},
draw: function(ctx, param) {
2011-04-21 09:48:21 -04:00
if (param.selection) {
var bounds = new Rectangle(this._size).setCenter(0, 0);
2011-04-21 09:48:21 -04:00
Item.drawSelectedBounds(bounds, ctx, this.matrix);
} else {
ctx.save();
this.matrix.applyToContext(ctx);
ctx.drawImage(this._canvas || this._image,
-this._size.width / 2, -this._size.height / 2);
ctx.restore();
}
2011-02-21 12:43:56 -05:00
}
2011-03-03 11:32:55 -05:00
});