files/en-us/web/api/window/createimagebitmap/index.md
{{APIRef("Canvas API")}}
The createImageBitmap() method of the {{domxref("Window")}} interface creates a bitmap from a given source, optionally cropped to contain only a portion of that source.
It accepts a variety of different image sources, and returns a {{jsxref("Promise")}} which resolves to an {{domxref("ImageBitmap")}}.
createImageBitmap(image)
createImageBitmap(image, options)
createImageBitmap(image, sx, sy, sw, sh)
createImageBitmap(image, sx, sy, sw, sh, options)
image
sx
ImageBitmap will be extracted.sy
ImageBitmap will be extracted.sw
ImageBitmap will be extracted.
This value can be negative.sh
ImageBitmap will be extracted. This value can be negative.options {{optional_inline}}
imageOrientation
from-image
flipY
none
premultiplyAlpha
none, premultiply, or default (default).colorSpaceConversion
none or default (default).
The value default indicates that implementation-specific behavior is used.resizeWidth
resizeHeight
resizeQuality
pixelated, low (default), medium, or high.A {{jsxref("Promise")}} which resolves to an {{domxref("ImageBitmap")}} object containing bitmap data from the given rectangle.
This example loads a sprite sheet, extracts individual sprites, and then renders each sprite to the canvas. A sprite sheet is an image containing multiple smaller images, each of which you want to be able to render separately.
Original image:
<hr />
<canvas id="myCanvas"></canvas>
canvas {
border: 2px solid green;
}
const canvas = document.getElementById("myCanvas"),
ctx = canvas.getContext("2d"),
image = new Image();
// Wait for the sprite sheet to load
image.onload = () => {
Promise.all([
// Cut out two sprites from the sprite sheet
createImageBitmap(image, 0, 0, 32, 32),
createImageBitmap(image, 32, 0, 32, 32),
createImageBitmap(image, 0, 0, 50, 50, { imageOrientation: "flipY" }),
]).then((sprites) => {
// Draw each sprite onto the canvas
ctx.drawImage(sprites[0], 0, 0);
ctx.drawImage(sprites[1], 32, 32);
ctx.drawImage(sprites[2], 64, 64);
});
};
// Load the sprite sheet from an image file
image.src = "50x50.jpg";
{{EmbedLiveSample("Creating sprites from a sprite sheet", "100%", "250")}}
{{Specifications}}
{{Compat}}