The second argument is accepting attributes of the HTML element.  It
isn't just assigning values to the object like Object.extend style.

I'd recommend using Element.addMethods, and scope it to only canvas
elements, that way each time you create a canvas you'll have this
method available, but it won't pollute other elements with this
special functionality.
Element.addMethods('canvas', ...);

http://prototypejs.org/api/element/addMethods

You could also assign the function value after creation, but unless
you're using a good factory pattern, you could easily get instances of
canvas without your specialized method.  You can't send methods to the
constructor like that, just won't work.  The declaration for Element
is a bit bizarre so it can be hard to find, so here it is...

(function() {
  var element = this.Element;
  this.Element = function(tagName, attributes) {
    attributes = attributes || { };
    tagName = tagName.toLowerCase();
    var cache = Element.cache;
    if (Prototype.Browser.IE && attributes.name) {
      tagName = '<' + tagName + ' name="' + attributes.name + '">';
      delete attributes.name;
      return Element.writeAttribute(document.createElement(tagName),
attributes);
    }
    if (!cache[tagName]) cache[tagName] = Element.extend
(document.createElement(tagName));
    return Element.writeAttribute(cache[tagName].cloneNode(false),
attributes);
  };
  Object.extend(this.Element, element || { });
  if (element) this.Element.prototype = element.prototype;
}).call(window);



--

http://positionabsolute.net





On Jul 13, 2:40 pm, ronman <ron.new...@gmail.com> wrote:
> Why doesn't 'getCoordinates' show up in the following code?
> canvas.getCoordinates() throws an error of "getCoordinates is not a
> function"
>
> I'm making a newbie error somewhere.
>
>     var canvas = new Element('canvas', {
>       width:  this.xform(data.size[0]) + 16 + 'px',
>       height: this.xform(data.size[1]) + 16 + 'px',
>
>       // mixin to Element for canvases
>       getCoordinates: function(element){
>         var position = this.getPosition(element), size = this.getSize
> ();
>         var obj = {left: position.x, top: position.y, width: size.x,
> height: size.y};
>         obj.right = obj.left + obj.width;
>         obj.bottom = obj.top + obj.height;
>         return obj;
>       },
>     }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to