Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Window: createImageBitmap() method

BaselineWidely available *

ThecreateImageBitmap() method of theWindow 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 aPromise which resolves to anImageBitmap.

Syntax

js
createImageBitmap(image)createImageBitmap(image, options)createImageBitmap(image, sx, sy, sw, sh)createImageBitmap(image, sx, sy, sw, sh, options)

Parameters

image

An image source, which can be any one of the following:

sx

The x coordinate of the reference point of the rectangle from which theImageBitmap will be extracted.

sy

The y coordinate of the reference point of the rectangle from which theImageBitmap will be extracted.

sw

The width of the rectangle from which theImageBitmap will be extracted.This value can be negative.

sh

The height of the rectangle from which theImageBitmap will be extracted. This value can be negative.

optionsOptional

An object that sets options for the image's extraction.The available options are:

imageOrientation

Specifies how the bitmap image should be oriented.

from-image

Image oriented according to EXIF orientation metadata, if present (default).

flipY

Image oriented according to EXIF orientation metadata, if present, and then flipped vertically.

none

Image oriented according to image encoding, ignoring any metadata about the orientation (such as EXIF metadata, that might be added to an image to indicate that the camera was turned sideways to capture the image in portrait mode).

premultiplyAlpha

Specifies whether the bitmap's color channels should be premultiplied by the alpha channel.One ofnone,premultiply, ordefault (default).

colorSpaceConversion

Specifies whether the image should be decoded using color space conversion.Eithernone ordefault (default).The valuedefault indicates that implementation-specific behavior is used.

resizeWidth

A long integer that indicates the output width.

resizeHeight

A long integer that indicates the output height.

resizeQuality

Specifies the algorithm to be used for resizing the input to match the output dimensions.One ofpixelated,low (default),medium, orhigh.

Return value

APromise which resolves to anImageBitmap object containing bitmap data from the given rectangle.

Examples

Creating sprites from a sprite sheet

This example loads a sprite sheet, extracts individual sprites, and then renders eachsprite 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:<img src="50x50.jpg" /><hr /><canvas></canvas>
canvas {  border: 2px solid green;}
js
const canvas = document.getElementById("myCanvas"),  ctx = canvas.getContext("2d"),  image = new Image();// Wait for the sprite sheet to loadimage.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 fileimage.src = "50x50.jpg";

Specifications

Specification
HTML
# dom-createimagebitmap-dev

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp