Improve CanvasView constructor.

Support canvas / size arguments, but not both together, and throw exception if both fail.
This commit is contained in:
Jürg Lehni 2013-10-14 16:06:30 +02:00
parent a3d0e0d462
commit 7285ec636a

View file

@ -21,16 +21,25 @@ var CanvasView = View.extend(/** @lends CanvasView# */{
/**
* Creates a view object that wraps a canvas element.
*
* @param {HTMLCanvasElement} canvas The canvas object that this view should
* @name CanvasView#initialize
* @param {HTMLCanvasElement} canvas the canvas object that this view should
* wrap
*/
/**
* Creates a view object that wraps a newly created canvas element.
*
* @name CanvasView#initialize
* @param {Size} size the size of the canvas to be created
*/
initialize: function CanvasView(canvas) {
// Handle canvas argument
if (!(canvas instanceof HTMLCanvasElement)) {
// 2nd argument onwards could be view size, otherwise use default:
var size = Size.read(arguments, 1);
// See if the arguments describe the view size:
var size = Size.read(arguments);
if (size.isZero())
size = new Size(1024, 768);
throw new Error(
'Cannot create CanvasView with the provided arguments: '
+ arguments);
canvas = CanvasProvider.getCanvas(size);
}
var ctx = this._context = canvas.getContext('2d');