Google Apps Script

Class Image

A widget that displays the image at a given URL.

The image can be in 'unclipped' mode (the default) or 'clipped' mode. In clipped mode, a viewport is overlaid on top of the image so that a subset of the image will be displayed. In unclipped mode, there is no viewport - the entire image will be visible. Whether an image is in clipped or unclipped mode depends on how the image is constructed, and how it is transformed after construction. Methods will operate differently depending on the mode that the image is in. These differences are detailed in the documentation for each method.

Here is an example of how to use this widget:

 
function doGet() {
   var app = UiApp.createApplication();
   // The very first Google Doodle!
   app.add(app.createImage("http://www.google.com/logos/googleburn.jpg"));
   // Just the man in the middle
   app.add(app.createImage("http://www.google.com/logos/googleburn.jpg", 118, 0, 50, 106));
   return app;
 }
 

Due to browser-specific HTML constructions needed to achieve the clipping effect, certain CSS attributes, such as padding and background, may not work as expected when an image is in clipped mode. These limitations can usually be easily worked around by encapsulating the image in a container widget that can itself be styled.

Internally, UiApp widgets are built on top of the Google Web Toolkit, and it can sometimes be helpful to look at the GWT documentation directly. You can find the Image documentation here.

Methods

MethodReturn typeBrief description
addClickHandler(handler)ImageAdd a handler for click events.
addErrorHandler(handler)ImageAdd a handler for error events (failed loads).
addLoadHandler(handler)ImageAdd a handler for load events.
addMouseDownHandler(handler)ImageAdd a handler for mouse down events.
addMouseMoveHandler(handler)ImageAdd a handler for mouse move events.
addMouseOutHandler(handler)ImageAdd a handler for mouse out events.
addMouseOverHandler(handler)ImageAdd a handler for mouse move events.
addMouseUpHandler(handler)ImageAdd a handler for mouse up events.
addMouseWheelHandler(handler)ImageAdd a handler for mouse wheel events.
addStyleDependentName(styleName)ImageSets the dependent style name of this Image.
addStyleName(styleName)ImageAdds a style name to this Image.
getId()StringReturns the id that has been assigned to this object.
getTag()StringGets the text tag of this Image.
getType()StringGets the type of this object.
setHeight(height)ImageSets the height of this Image.
setId(id)ImageSets the id of this Image.
setPixelSize(width, height)ImageSets the size of this Image in pixels.
setSize(width, height)ImageSets the size of this Image.
setStyleAttribute(attribute, value)ImageSets one of this Image's style attributes to a new value.
setStyleAttributes(attributes)ImageSets this Image's style attributes.
setStyleName(styleName)ImageSets the style name of this Image.
setStylePrimaryName(styleName)ImageSets the primary style name of this Image.
setTag(tag)ImageSets the text tag of this Image.
setTitle(title)ImageSets the hover title of this Image.
setUrl(url)ImageSets the URL of the image to be displayed.
setUrlAndVisibleRect(url, left, top, width, height)ImageSets the url and the visibility rectangle for the image at the same time.
setVisible(visible)ImageSets whether this Image is visible.
setVisibleRect(left, top, width, height)ImageSets the visibility rectangle of an image.
setWidth(width)ImageSets the width of this Image.

Detailed documentation

addClickHandler(handler)

Add a handler for click events.

Note that you can have multiple handlers for the same event on the same widget. They will be called in the order that they were added to the widget, although ServerHandlers may appear to happen simultaneously.

The handler passes back some information to the server about what happened. This information can be accessed as follows:

 
function doGet() {
   var app = UiApp.createApplication();
   var button = app.createButton("a button");
   var handler = app.createServerHandler("handlerFunction");
   button.addClickHandler(handler);
   app.add(button);
   return app;
 }

 function handlerFunction(eventInfo) {
   var parameter = eventInfo.parameter;
   // the type of event, in this case "click".
   var eventType = parameter.eventType;
   // the id of the widget that fired this event.
   var source = parameter.source;
   // mouse x and y position relative to the widget that fired the event.
   var x = parameter.x;
   var y = parameter.y;
   // mouse x and y position within the browser window's client area.
   var clientX = parameter.clientX;
   var clientY = parameter.clientY;
   // mouse x and y position within the user's display.
   var screenX = parameter.screenX;
   var screenY = parameter.screenY;
   // the mouse button used. Left is 1, right is 2, and middle is 4.
   var button = parameter.button;
   // whether the various modifier keys were also pressed (true or false)
   var shift = parameter.shift;
   var alt = parameter.alt;
   var ctrl = parameter.ctrl;
   var meta = parameter.meta;
 }
 
In addition, the values of certain widgets can be sent up with the event as well, as "callback elements." See the documentation of ServerHandler for more information.

Parameters

NameTypeDescription
handlerHandlerthe handler to execute when the event occurs. This can be a ClientHandler or a ServerHandler.

Return

Image — the Image itself, useful for chaining.


addErrorHandler(handler)

Add a handler for error events (failed loads).

The handler passes back some information to the server about what happened. This information can be accessed as follows:

 
function doGet() {
   var app = UiApp.createApplication();
   var image = app.createImage("some image url");
   var handler = app.createServerHandler("handlerFunction");
   image.addErrorHandler(handler);
   app.add(image);
   return app;
 }

 function handlerFunction(eventInfo) {
   var parameter = eventInfo.parameter;
   // the type of event, in this case "error".
   var eventType = parameter.eventType;
   // the id of the widget that fired this event.
   var source = parameter.source;
 }
 
In addition, the values of certain widgets can be sent up with the event as well, as "callback elements." See the documentation of ServerHandler for more information.

Parameters

NameTypeDescription
handlerHandlerthe handler to execute when the event occurs. This can be a ClientHandler or a ServerHandler.

Return

Image — the Image itself, useful for chaining.


addLoadHandler(handler)

Add a handler for load events.

The handler passes back some information to the server about what happened. This information can be accessed as follows:

 
function doGet() {
   var app = UiApp.createApplication();
   var image = app.createImage("some image url");
   var handler = app.createServerHandler("handlerFunction");
   image.addLoadHandler(handler);
   app.add(image);
   return app;
 }

 function handlerFunction(eventInfo) {
   var parameter = eventInfo.parameter;
   // the type of event, in this case "load".
   var eventType = parameter.eventType;
   // the id of the widget that fired this event.
   var source = parameter.source;
 }
 
In addition, the values of certain widgets can be sent up with the event as well, as "callback elements." See the documentation of ServerHandler for more information.

Parameters

NameTypeDescription
handlerHandlerthe handler to execute when the event occurs. This can be a ClientHandler or a ServerHandler.

Return

Image — the Image itself, useful for chaining.


addMouseDownHandler(handler)

Add a handler for mouse down events.

Note that you can have multiple handlers for the same event on the same widget. They will be called in the order that they were added to the widget, although ServerHandlers may appear to happen simultaneously.

The handler passes back some information to the server about what happened. This information can be accessed as follows:

 
function doGet() {
   var app = UiApp.createApplication();
   var button = app.createButton("a button");
   var handler = app.createServerHandler("handlerFunction");
   button.addMouseDownHandler(handler);
   app.add(button);
   return app;
 }

 function handlerFunction(eventInfo) {
   var parameter = eventInfo.parameter;
   // the type of event, in this case "mousedown".
   var eventType = parameter.eventType;
   // the id of the widget that fired this event.
   var source = parameter.source;
   // mouse x and y position relative to the widget that fired the event.
   var x = parameter.x;
   var y = parameter.y;
   // mouse x and y position within the browser window's client area.
   var clientX = parameter.clientX;
   var clientY = parameter.clientY;
   // mouse x and y position within the user's display.
   var screenX = parameter.screenX;
   var screenY = parameter.screenY;
   // the mouse button used. Left is 1, right is 2, and middle is 4.
   var button = parameter.button;
   // whether the various modifier keys were also pressed (true or false)
   var shift = parameter.shift;
   var alt = parameter.alt;
   var ctrl = parameter.ctrl;
   var meta = parameter.meta;
 }
 
In addition, the values of certain widgets can be sent up with the event as well, as "callback elements." See the documentation of ServerHandler for more information.

Parameters

NameTypeDescription
handlerHandlerthe handler to execute when the event occurs. This can be a ClientHandler or a ServerHandler.

Return

Image — the Image itself, useful for chaining.


addMouseMoveHandler(handler)

Add a handler for mouse move events.

Note that you can have multiple handlers for the same event on the same widget. They will be called in the order that they were added to the widget, although ServerHandlers may appear to happen simultaneously.

The handler passes back some information to the server about what happened. This information can be accessed as follows:

 
function doGet() {
   var app = UiApp.createApplication();
   var button = app.createButton("a button");
   var handler = app.createServerHandler("handlerFunction");
   button.addMouseMoveHandler(handler);
   app.add(button);
   return app;
 }

 function handlerFunction(eventInfo) {
   var parameter = eventInfo.parameter;
   // the type of event, in this case "mousemove".
   var eventType = parameter.eventType;
   // the id of the widget that fired this event.
   var source = parameter.source;
   // mouse x and y position relative to the widget that fired the event.
   var x = parameter.x;
   var y = parameter.y;
   // mouse x and y position within the browser window's client area.
   var clientX = parameter.clientX;
   var clientY = parameter.clientY;
   // mouse x and y position within the user's display.
   var screenX = parameter.screenX;
   var screenY = parameter.screenY;
   // the mouse button used. Left is 1, right is 2, and middle is 4.
   var button = parameter.button;
   // whether the various modifier keys were also pressed (true or false)
   var shift = parameter.shift;
   var alt = parameter.alt;
   var ctrl = parameter.ctrl;
   var meta = parameter.meta;
 }
 
In addition, the values of certain widgets can be sent up with the event as well, as "callback elements." See the documentation of ServerHandler for more information.

Parameters

NameTypeDescription
handlerHandlerthe handler to execute when the event occurs. This can be a ClientHandler or a ServerHandler.

Return

Image — the Image itself, useful for chaining.


addMouseOutHandler(handler)

Add a handler for mouse out events.

Note that you can have multiple handlers for the same event on the same widget. They will be called in the order that they were added to the widget, although ServerHandlers may appear to happen simultaneously.

The handler passes back some information to the server about what happened. This information can be accessed as follows:

 
function doGet() {
   var app = UiApp.createApplication();
   var button = app.createButton("a button");
   var handler = app.createServerHandler("handlerFunction");
   button.addMouseOutHandler(handler);
   app.add(button);
   return app;
 }

 function handlerFunction(eventInfo) {
   var parameter = eventInfo.parameter;
   // the type of event, in this case "mouseout".
   var eventType = parameter.eventType;
   // the id of the widget that fired this event.
   var source = parameter.source;
   // mouse x and y position relative to the widget that fired the event.
   var x = parameter.x;
   var y = parameter.y;
   // mouse x and y position within the browser window's client area.
   var clientX = parameter.clientX;
   var clientY = parameter.clientY;
   // mouse x and y position within the user's display.
   var screenX = parameter.screenX;
   var screenY = parameter.screenY;
   // the mouse button used. Left is 1, right is 2, and middle is 4.
   var button = parameter.button;
   // whether the various modifier keys were also pressed (true or false)
   var shift = parameter.shift;
   var alt = parameter.alt;
   var ctrl = parameter.ctrl;
   var meta = parameter.meta;
 }
 
In addition, the values of certain widgets can be sent up with the event as well, as "callback elements." See the documentation of ServerHandler for more information.

Parameters

NameTypeDescription
handlerHandlerthe handler to execute when the event occurs. This can be a ClientHandler or a ServerHandler.

Return

Image — the Image itself, useful for chaining.


addMouseOverHandler(handler)

Add a handler for mouse move events.

Note that you can have multiple handlers for the same event on the same widget. They will be called in the order that they were added to the widget, although ServerHandlers may appear to happen simultaneously.

The handler passes back some information to the server about what happened. This information can be accessed as follows:

 
function doGet() {
   var app = UiApp.createApplication();
   var button = app.createButton("a button");
   var handler = app.createServerHandler("handlerFunction");
   button.addMouseOverHandler(handler);
   app.add(button);
   return app;
 }

 function handlerFunction(eventInfo) {
   var parameter = eventInfo.parameter;
   // the type of event, in this case "mousover".
   var eventType = parameter.eventType;
   // the id of the widget that fired this event.
   var source = parameter.source;
   // mouse x and y position relative to the widget that fired the event.
   var x = parameter.x;
   var y = parameter.y;
   // mouse x and y position within the browser window's client area.
   var clientX = parameter.clientX;
   var clientY = parameter.clientY;
   // mouse x and y position within the user's display.
   var screenX = parameter.screenX;
   var screenY = parameter.screenY;
   // the mouse button used. Left is 1, right is 2, and middle is 4.
   var button = parameter.button;
   // whether the various modifier keys were also pressed (true or false)
   var shift = parameter.shift;
   var alt = parameter.alt;
   var ctrl = parameter.ctrl;
   var meta = parameter.meta;
 }
 
In addition, the values of certain widgets can be sent up with the event as well, as "callback elements." See the documentation of ServerHandler for more information.

Parameters

NameTypeDescription
handlerHandlerthe handler to execute when the event occurs. This can be a ClientHandler or a ServerHandler.

Return

Image — the Image itself, useful for chaining.


addMouseUpHandler(handler)

Add a handler for mouse up events.

Note that you can have multiple handlers for the same event on the same widget. They will be called in the order that they were added to the widget, although ServerHandlers may appear to happen simultaneously.

The handler passes back some information to the server about what happened. This information can be accessed as follows:

 
function doGet() {
   var app = UiApp.createApplication();
   var button = app.createButton("a button");
   var handler = app.createServerHandler("handlerFunction");
   button.addMouseUpHandler(handler);
   app.add(button);
   return app;
 }

 function handlerFunction(eventInfo) {
   var parameter = eventInfo.parameter;
   // the type of event, in this case "mouseup".
   var eventType = parameter.eventType;
   // the id of the widget that fired this event.
   var source = parameter.source;
   // mouse x and y position relative to the widget that fired the event.
   var x = parameter.x;
   var y = parameter.y;
   // mouse x and y position within the browser window's client area.
   var clientX = parameter.clientX;
   var clientY = parameter.clientY;
   // mouse x and y position within the user's display.
   var screenX = parameter.screenX;
   var screenY = parameter.screenY;
   // the mouse button used. Left is 1, right is 2, and middle is 4.
   var button = parameter.button;
   // whether the various modifier keys were also pressed (true or false)
   var shift = parameter.shift;
   var alt = parameter.alt;
   var ctrl = parameter.ctrl;
   var meta = parameter.meta;
 }
 
In addition, the values of certain widgets can be sent up with the event as well, as "callback elements." See the documentation of ServerHandler for more information.

Parameters

NameTypeDescription
handlerHandlerthe handler to execute when the event occurs. This can be a ClientHandler or a ServerHandler.

Return

Image — the Image itself, useful for chaining.


addMouseWheelHandler(handler)

Add a handler for mouse wheel events.

Note that you can have multiple handlers for the same event on the same widget. They will be called in the order that they were added to the widget, although ServerHandlers may appear to happen simultaneously.

The handler passes back some information to the server about what happened. This information can be accessed as follows:

 
function doGet() {
   var app = UiApp.createApplication();
   var button = app.createButton("a button");
   var handler = app.createServerHandler("handlerFunction");
   button.addMouseWheelHandler(handler);
   app.add(button);
   return app;
 }

 function handlerFunction(eventInfo) {
   var parameter = eventInfo.parameter;
   // the type of event, in this case "mousewheel".
   var eventType = parameter.eventType;
   // the id of the widget that fired this event.
   var source = parameter.source;
   // mouse x and y position relative to the widget that fired the event.
   var x = parameter.x;
   var y = parameter.y;
   // mouse x and y position within the browser window's client area.
   var clientX = parameter.clientX;
   var clientY = parameter.clientY;
   // mouse x and y position within the user's display.
   var screenX = parameter.screenX;
   var screenY = parameter.screenY;
   // the mouse button used. Left is 1, right is 2, and middle is 4.
   var button = parameter.button;
   // whether the various modifier keys were also pressed (true or false)
   var shift = parameter.shift;
   var alt = parameter.alt;
   var ctrl = parameter.ctrl;
   var meta = parameter.meta;
 }
 
In addition, the values of certain widgets can be sent up with the event as well, as "callback elements." See the documentation of ServerHandler for more information.

Parameters

NameTypeDescription
handlerHandlerthe handler to execute when the event occurs. This can be a ClientHandler or a ServerHandler.

Return

Image — the Image itself, useful for chaining.


addStyleDependentName(styleName)

Sets the dependent style name of this Image.

This is useful for debugging but is otherwise of minimal use since there is no way to use custom stylesheets in UiApp.

Parameters

NameTypeDescription
styleNameStringthe new style name.

Return

Image — the Image itself, useful for chaining.


addStyleName(styleName)

Adds a style name to this Image.

This is useful for debugging but is otherwise of minimal use since there is no way to use custom stylesheets in UiApp.

Parameters

NameTypeDescription
styleNameStringthe new style name.

Return

Image — the Image itself, useful for chaining.


getId()

Returns the id that has been assigned to this object.

This can be used in conjunction with app.getElementById() to retrieve a reference to this object.

Return

String — the id that has been assigned to this object


getTag()

Gets the text tag of this Image.

Return

String — the text tag.


getType()

Gets the type of this object.

Return

String — the object type


setHeight(height)

Sets the height of this Image.

Parameters

NameTypeDescription
heightStringthe new height in any CSS unit such as "10px" or "50%".

Return

Image — the Image itself, useful for chaining.


setId(id)

Sets the id of this Image.

Parameters

NameTypeDescription
idStringthe new id, which can be used to retrieve the Image from app.getElementById(id).

Return

Image — the Image itself, useful for chaining.


setPixelSize(width, height)

Sets the size of this Image in pixels.

Parameters

NameTypeDescription
widthIntegerthe new width in pixels.
heightIntegerthe new height in pixels.

Return

Image — the Image itself, useful for chaining.


setSize(width, height)

Sets the size of this Image.

Parameters

NameTypeDescription
widthStringthe new width in any CSS unit such as "10px" or "50%".
heightStringthe new height in any CSS unit such as "10px" or "50%".

Return

Image — the Image itself, useful for chaining.


setStyleAttribute(attribute, value)

Sets one of this Image's style attributes to a new value. Valid attributes are listed here; the values for each attribute are the same as those available in CSS style sheets.

 
// Change the widget's background to black and text color to green.
 widget.setStyleAttribute("background", "black")
     .setStyleAttribute("color", "green");
 

Parameters

NameTypeDescription
attributeStringthe CSS attribute, in camel-case ("fontSize", not "font-size"), as listed here
valueStringthe CSS value

Return

Image — the Image itself, useful for chaining.


setStyleAttributes(attributes)

Sets this Image's style attributes. This is a convenience method that is equivalent to calling setStyleAttribute with every key/value pair in the attributes object. Valid attributes are listed here; the values for each attribute are the same as those available in CSS style sheets.

 
// Change the widget's background to black and text color to green.
 widget.setStyleAttributes({background: "black", color: "green"});
 

Parameters

NameTypeDescription
attributesObjectan object of key/value pairs for the CSS attributes and values to set; valid attributes are listed here

Return

Image — the Image itself, useful for chaining.


setStyleName(styleName)

Sets the style name of this Image.

This is useful for debugging but is otherwise of minimal use since there is no way to use custom stylesheets in UiApp.

Parameters

NameTypeDescription
styleNameStringthe new style name.

Return

Image — the Image itself, useful for chaining.


setStylePrimaryName(styleName)

Sets the primary style name of this Image.

This is useful for debugging but is otherwise of minimal use since there is no way to use custom stylesheets in UiApp.

Parameters

NameTypeDescription
styleNameStringthe new style name.

Return

Image — the Image itself, useful for chaining.


setTag(tag)

Sets the text tag of this Image.

Parameters

NameTypeDescription
tagStringthe new text tag, which can be anything you wish to store with the widget.

Return

Image — the Image itself, useful for chaining.


setTitle(title)

Sets the hover title of this Image.

Not all browsers will show this.

Parameters

NameTypeDescription
titleStringthe hover title.

Return

Image — the Image itself, useful for chaining.


setUrl(url)

Sets the URL of the image to be displayed.

If the image is in the clipped state, a call to this method will cause a transition of the image to the unclipped state. Regardless of whether or not the image is in the clipped or unclipped state, a load event will be fired.

Parameters

NameTypeDescription
urlStringthe new image url.

Return

Image — the Image itself, useful for chaining.


setUrlAndVisibleRect(url, left, top, width, height)

Sets the url and the visibility rectangle for the image at the same time.

A single load event will be fired if either the incoming url or visiblity rectangle coordinates differ from the image's current url or current visibility rectangle coordinates. If the image is currently in the unclipped state, a call to this method will cause a transition to the clipped state.

Parameters

NameTypeDescription
urlStringthe new image url.
leftIntegerthe the horizontal coordinate of the upper-left vertex of the visibility rectangle, in pixels.
topIntegerthe the vertical coordinate of the upper-left vertex of the visibility rectangle, in pixels.
widthIntegerthe width of the visibility rectangle.
heightIntegerthe height of the visibility rectangle.

Return

Image — the Image itself, useful for chaining.


setVisible(visible)

Sets whether this Image is visible.

Parameters

NameTypeDescription
visibleBooleanwhether this Image should be visible or not.

Return

Image — the Image itself, useful for chaining.


setVisibleRect(left, top, width, height)

Sets the visibility rectangle of an image.

The visibility rectangle is declared relative to the the rectangle which encompasses the entire image, which has an upper-left vertex of (0,0). Provided that any of the left, top, width, and height parameters are different than the those values that are currently set for the image, a load event will be fired. If the image is in the unclipped state, a call to this method will cause a transition of the image to the clipped state. This transition will cause a load event to fire.

Parameters

NameTypeDescription
leftIntegerthe the horizontal coordinate of the upper-left vertex of the visibility rectangle, in pixels.
topIntegerthe the vertical coordinate of the upper-left vertex of the visibility rectangle, in pixels.
widthIntegerthe width of the visibility rectangle.
heightIntegerthe height of the visibility rectangle.

Return

Image — the Image itself, useful for chaining.


setWidth(width)

Sets the width of this Image.

Parameters

NameTypeDescription
widthStringthe new width in any CSS unit such as "10px" or "50%".

Return

Image — the Image itself, useful for chaining.

Deprecated methods


Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.