Class enchant.Surface
Class that wraps canvas elements. Can be used to set the enchant.Sprite and enchant.Map's image properties to be displayed. If you wish to access Canvas API use the enchant.Surface#context property.
- Defined in: enchant.js
- Extends enchant.EventTarget
Constructor Attributes | Constructor Name and Description |
---|---|
enchant.Surface(width, height)
|
Field Attributes | Field Name and Description |
---|---|
Surface drawing context.
|
|
Surface height.
|
|
Surface width.
|
Method Summary
Method Attributes | Method Name and Description |
---|---|
clear()
Clears all Surface pixels and makes the pixels transparent.
|
|
clone()
Copies Surface.
|
|
draw(image)
Draws the content of the given Surface onto this surface.
|
|
getPixel(x, y)
Returns 1 pixel from the Surface.
|
|
<static> |
enchant.Surface.load(src, callback, onerror)
Loads an image and creates a Surface object out of it.
|
setPixel(x, y, r, g, b, a)
Sets one pixel within the surface.
|
|
Creates a data URI scheme from this Surface.
|
- Methods borrowed from class enchant.EventTarget:
- addEventListener
- clearEventListener
- dispatchEvent
- on
- removeEventListener
Class Detail
enchant.Surface(width, height)
// Creates Sprite that displays a circle. var ball = new Sprite(50, 50); var surface = new Surface(50, 50); surface.context.beginPath(); surface.context.arc(25, 25, 25, 0, Math.PI*2, true); surface.context.fill(); ball.image = surface;
- Parameters:
- {Number} width
- Surface width.
- {Number} height
- Surface height.
Field Detail
{CanvasRenderingContext2D}
context
Surface drawing context.
{Number}
height
Surface height.
{Number}
width
Surface width.
Method Detail
-
clear()Clears all Surface pixels and makes the pixels transparent.
-
{enchant.Surface} clone()Copies Surface.
- Returns:
- {enchant.Surface} The copied Surface.
-
draw(image)Draws the content of the given Surface onto this surface. Wraps Canvas API drawImage and if multiple arguments are given, these are getting applied to the Canvas drawImage method.
var src = core.assets['src.gif']; var dst = new Surface(100, 100); dst.draw(src); // Draws source at (0, 0) dst.draw(src, 50, 50); // Draws source at (50, 50) // Draws just 30 horizontal and vertical pixels of source at (50, 50) dst.draw(src, 50, 50, 30, 30); // Takes the image content in src starting at (10,10) with a (Width, Height) of (40,40), // scales it and draws it in this surface at (50, 50) with a (Width, Height) of (30,30). dst.draw(src, 10, 10, 40, 40, 50, 50, 30, 30);
- Parameters:
- {enchant.Surface} image
- Surface used in drawing.
-
{Number[]} getPixel(x, y)Returns 1 pixel from the Surface.
- Parameters:
- {Number} x
- The pixel's x coordinates.
- {Number} y
- The pixel's y coordinates.
- Returns:
- {Number[]} An array that holds pixel information in [r, g, b, a] format.
-
Loads an image and creates a Surface object out of it. It is not possible to access properties or methods of the enchant.Surface#context, or to call methods using the Canvas API - like enchant.Surface#draw, enchant.Surface#clear, enchant.Surface#getPixel, enchant.Surface#setPixel.. - of the wrapped image created with this method. However, it is possible to use this surface to draw it to another surface using the enchant.Surface#draw method. The resulting surface can then be manipulated. (when loading images in a cross-origin resource sharing environment, pixel acquisition and other image manipulation might be limited).
- Parameters:
- {String} src
- The file path of the image to be loaded.
- {Function} callback
- on load callback.
- {Function} onerror Optional
- on error callback.
- Returns:
- {enchant.Surface} Surface
-
setPixel(x, y, r, g, b, a)Sets one pixel within the surface.
- Parameters:
- {Number} x
- The pixel's x coordinates.
- {Number} y
- The pixel's y coordinates.
- {Number} r
- The pixel's red level.
- {Number} g
- The pixel's green level.
- {Number} b
- The pixel's blue level.
- {Number} a
- The pixel's transparency.
-
{String} toDataURL()Creates a data URI scheme from this Surface.
- Returns:
- {String} The data URI scheme that identifies this Surface and can be used to include this Surface into a dom tree.