diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/ja/web/api/offscreencanvas | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/ja/web/api/offscreencanvas')
-rw-r--r-- | files/ja/web/api/offscreencanvas/getcontext/index.html | 121 | ||||
-rw-r--r-- | files/ja/web/api/offscreencanvas/index.html | 155 |
2 files changed, 276 insertions, 0 deletions
diff --git a/files/ja/web/api/offscreencanvas/getcontext/index.html b/files/ja/web/api/offscreencanvas/getcontext/index.html new file mode 100644 index 0000000000..ce60c9f633 --- /dev/null +++ b/files/ja/web/api/offscreencanvas/getcontext/index.html @@ -0,0 +1,121 @@ +--- +title: OffscreenCanvas.getContext() +slug: Web/API/OffscreenCanvas/getContext +translation_of: Web/API/OffscreenCanvas/getContext +--- +<div>{{APIRef("Canvas API")}} {{SeeCompatTable}}</div> + +<p>The <strong><code>OffscreenCanvas.getContext()</code></strong> method returns a drawing context for an offscreen canvas, or {{jsxref("null")}} if the context identifier is not supported.</p> + +<div class="note"> +<p><strong>Note</strong>: This API is currently implemented for <a href="/en-US/docs/Web/API/WebGLRenderingContext">WebGL1</a> and <a href="/en-US/docs/Web/API/WebGL2RenderingContext">WebGL2</a> contexts only. See {{bug(801176)}} for <a href="/en-US/docs/Web/API/Canvas_API">Canvas 2D API</a> support from workers.</p> +</div> + +<p><font face="x-locale-heading-primary, zillaslab, Palatino, Palatino Linotype, x-locale-heading-secondary, serif"><span style="font-size: 37.33327865600586px;"><strong>構文</strong></span></font></p> + + + +<pre class="syntaxbox notranslate"><em>offscreen</em>.getContext(<em>contextType</em>, <em>contextAttributes</em>); +</pre> + +<h3 id="引数">引数</h3> + +<dl> + <dt><code>contextType</code></dt> + <dd>Is a {{domxref("DOMString")}} containing the context identifier defining the drawing context associated to the canvas. Possible values are: + <ul> + <li><strong><code>"2d"</code></strong> creates a {{domxref("CanvasRenderingContext2D")}} object representing a two-dimensional rendering context.</li> + <li><strong><code>"webgl"</code></strong> creates a {{domxref("WebGLRenderingContext")}} object representing a three-dimensional rendering context. This context is only available on browsers that implement <a href="https://developer.mozilla.org/en-US/docs/Web/WebGL">WebGL</a> version 1 (OpenGL ES 2.0).</li> + <li><strong><code>"webgl2"</code></strong> creates a {{domxref("WebGL2RenderingContext")}} object representing a three-dimensional rendering context. This context is only available on browsers that implement <a href="https://developer.mozilla.org/en-US/docs/Web/WebGL">WebGL</a> version 2 (OpenGL ES 3.0). {{experimental_inline}}</li> + <li> + <p><strong><code>"bitmaprenderer"</code></strong> creates a {{domxref("ImageBitmapRenderingContext")}} which only provides functionality to replace the content of the canvas with a given {{domxref("ImageBitmap")}}.</p> + </li> + </ul> + + <p>Note: The identifiers <strong><code>"experimental-webgl"</code></strong> or <strong><code>"experimental-webgl2"</code></strong> are also used in implementations of WebGL. These implementations have not reached test suite conformance, or the graphic drivers situation on the platform is not yet stable. The <a href="https://www.khronos.org/">Khronos Group</a> certifies WebGL implementations under certain <a href="https://www.khronos.org/registry/webgl/sdk/tests/CONFORMANCE_RULES.txt">conformance rules</a>.</p> + </dd> + <dt><code>contextAttributes</code></dt> + <dd> + <p>You can use several context attributes when creating your rendering context, for example:</p> + + <pre class="brush: js notranslate">offscreen.getContext("webgl", + { antialias: false, + depth: false });</pre> + 2d context attributes: + + <ul> + <li><strong><code>alpha</code></strong>: Boolean that indicates if the canvas contains an alpha channel. If set to <code>false</code>, the browser now knows that the backdrop is always opaque, which can speed up drawing of transparent content and images then.</li> + <li>{{non-standard_inline}} (Gecko only) <strong><code>willReadFrequently</code></strong>: Boolean that indicates whether or not a lot of read-back operations are planned. This will force the use of a software (instead of hardware accelerated) 2D canvas and can save memory when calling {{domxref("CanvasRenderingContext2D.getImageData", "getImageData()")}} frequently. This option is only available, if the flag <code>gfx.canvas.willReadFrequently.enable</code> is set to <code>true</code> (which, by default, is only the case for B2G/Firefox OS).</li> + <li>{{non-standard_inline}} (Blink only) <strong><code>storage</code></strong>: String that indicates which storage is used ("persistent" by default).</li> + </ul> + WebGL context attributes: + + <ul> + <li><strong><code>alpha</code></strong>: Boolean that indicates if the canvas contains an alpha buffer.</li> + <li><strong><code>depth</code></strong>: Boolean that indicates that the drawing buffer has a depth buffer of at least 16 bits.</li> + <li><strong><code>stencil</code></strong>: Boolean that indicates that the drawing buffer has a stencil buffer of at least 8 bits.</li> + <li><strong><code>antialias</code></strong>: Boolean that indicates whether or not to perform anti-aliasing.</li> + <li><strong><code>premultipliedAlpha</code></strong>: Boolean that indicates that the page compositor will assume the drawing buffer contains colors with pre-multiplied alpha.</li> + <li><strong><code>preserveDrawingBuffer</code></strong>: If the value is true the buffers will not be cleared and will preserve their values until cleared or overwritten by the author.</li> + <li> + <p><code><strong>failIfMajorPerformanceCaveat</strong></code>: Boolean that indicates if a context will be created if the system performance is low.</p> + </li> + </ul> + </dd> +</dl> + +<h3 id="返値"> 返値</h3> + +<p>A {{domxref("RenderingContext")}} which is either a</p> + +<ul> + <li>{{domxref("CanvasRenderingContext2D")}} for <code>"2d"</code>,</li> + <li>{{domxref("WebGLRenderingContext")}} for <code>"webgl"</code> and <code>"experimental-webgl"</code>,</li> + <li>{{domxref("WebGL2RenderingContext")}} for <code>"webgl2"</code> and <code>"experimental-webgl2"</code> {{experimental_inline}}, or</li> + <li>{{domxref("ImageBitmapRenderingContext")}} for <code>"bitmaprenderer"</code>.</li> +</ul> + +<p>If the <code>contextType</code> doesn't match a possible drawing context, <code>null</code> is returned.</p> + +<h2 id="例"><font face="x-locale-heading-primary, zillaslab, Palatino, Palatino Linotype, x-locale-heading-secondary, serif"><span style="font-size: 37.33327865600586px;"><strong>例</strong></span></font></h2> + + + +<pre class="brush: js notranslate">var offscreen = new OffscreenCanvas(256, 256); +var gl = offscreen.getContext("webgl"); + +gl; // WebGLRenderingContext +gl.canvas; // OffscreenCanvas</pre> + +<h2 id="仕様書"><font face="x-locale-heading-primary, zillaslab, Palatino, Palatino Linotype, x-locale-heading-secondary, serif"><span style="font-size: 37.33327865600586px;"><strong>仕様書</strong></span></font></h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('HTML WHATWG', "#dom-offscreencanvas-getcontext", "OffscreenCanvas.getContext()")}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="ブラウザの互換性"><font face="x-locale-heading-primary, zillaslab, Palatino, Palatino Linotype, x-locale-heading-secondary, serif"><span style="font-size: 37.33327865600586px;"><strong>ブラウザの互換性</strong></span></font></h2> + +<div> + + +<p>{{Compat("api.OffscreenCanvas.getContext")}}</p> +</div> + +<h2 id="See_also">See also</h2> + +<ul> + <li>The interface defining this method: {{domxref("OffscreenCanvas")}}</li> + <li>{{domxref("HTMLCanvasElement.getContext()")}}</li> + <li>Available rendering contexts: {{domxref("CanvasRenderingContext2D")}}, {{domxref("WebGLRenderingContext")}}, {{domxref("WebGL2RenderingContext")}}, and {{domxref("ImageBitmapRenderingContext")}}</li> +</ul> diff --git a/files/ja/web/api/offscreencanvas/index.html b/files/ja/web/api/offscreencanvas/index.html new file mode 100644 index 0000000000..38919748b7 --- /dev/null +++ b/files/ja/web/api/offscreencanvas/index.html @@ -0,0 +1,155 @@ +--- +title: OffscreenCanvas +slug: Web/API/OffscreenCanvas +tags: + - API + - Canvas + - Experimental + - Interface + - NeedsTranslation + - Reference + - TopicStub +translation_of: Web/API/OffscreenCanvas +--- +<div>{{APIRef("Canvas API")}} {{SeeCompatTable}}</div> + +<p>The <strong><code>OffscreenCanvas</code></strong> interface provides a canvas that can be rendered off screen. It is available in both the window and <a href="/en-US/docs/Web/API/Web_Workers_API">worker</a> contexts.</p> + +<h2 id="Constructors">Constructors</h2> + +<dl> + <dt>{{domxref("OffscreenCanvas.OffscreenCanvas", "OffscreenCanvas()")}}</dt> + <dd><code>OffscreenCanvas</code> constructor. Creates a new <code>OffscreenCanvas</code> object.</dd> +</dl> + +<h2 id="Properties">Properties</h2> + +<dl> + <dt>{{domxref("OffscreenCanvas.height")}}</dt> + <dd>The height of the offscreen canvas.</dd> + <dt>{{domxref("OffscreenCanvas.width")}}</dt> + <dd>The width of the offscreen canvas.</dd> +</dl> + +<h2 id="Methods">Methods</h2> + +<dl> + <dt>{{domxref("OffscreenCanvas.getContext()")}}</dt> + <dd>Returns a rendering context for the offscreen canvas.</dd> +</dl> + +<dl> + <dt>{{domxref("OffscreenCanvas.convertToBlob()")}}</dt> + <dd>Creates a {{domxref("Blob")}} object representing the image contained in the canvas.</dd> +</dl> + +<dl> + <dt>{{domxref("OffscreenCanvas.transferToImageBitmap()")}}</dt> + <dd>Creates an {{domxref("ImageBitmap")}} object from the most recently rendered image of the <code>OffscreenCanvas</code>.</dd> +</dl> + +<h2 id="Examples">Examples</h2> + +<h3 id="Synchronous_display_of_frames_produced_by_an_OffscreenCanvas">Synchronous display of frames produced by an <code>OffscreenCanvas</code></h3> + +<p>One way to use the <code>OffscreenCanvas</code> API, is to use a {{domxref("RenderingContext")}} that has been obtained from an <code>OffscreenCanvas</code> object to generate new frames. Once a new frame has finished rendering in this context, the {{domxref("OffscreenCanvas.transferToImageBitmap", "transferToImageBitmap()")}} method can be called to save the most recent rendered image. This method returns an {{domxref("ImageBitmap")}} object, which can be used in a variety of Web APIs and also in a second canvas without creating a transfer copy.</p> + +<p>To display the <code>ImageBitmap</code>, you can use a {{domxref("ImageBitmapRenderingContext")}} context, which can be created by calling <code>canvas.getContext("bitmaprenderer")</code> on a (visible) canvas element. This context only provides functionality to replace the canvas's contents with the given <code>ImageBitmap</code>. A call to {{domxref("ImageBitmapRenderingContext.transferFromImageBitmap()")}} with the previously rendered and saved <code>ImageBitmap</code> from the OffscreenCanvas, will display the <code>ImageBitmap</code> on the canvas and transfer its ownership to the canvas. A single <code>OffscreenCanvas</code> may transfer frames into an arbitrary number of other <code>ImageBitmapRenderingContext</code> objects.</p> + +<p>Given these two {{HTMLElement("canvas")}} elements</p> + +<pre class="brush: html notranslate"><canvas id="one"></canvas> +<canvas id="two"></canvas></pre> + +<p>the following code will provide the rendering using an <code>OffscreenCanvas</code> as described above.</p> + +<pre class="brush: js notranslate">var one = document.getElementById("one").getContext("bitmaprenderer"); +var two = document.getElementById("two").getContext("bitmaprenderer"); + +var offscreen = new OffscreenCanvas(256, 256); +var gl = offscreen.getContext('webgl'); + +// ... some drawing for the first canvas using the gl context ... + +// Commit rendering to the first canvas +var bitmapOne = offscreen.transferToImageBitmap(); +one.transferFromImageBitmap(bitmapOne); + +// ... some more drawing for the second canvas using the gl context ... + +// Commit rendering to the second canvas +var bitmapTwo = offscreen.transferToImageBitmap(); +two.transferFromImageBitmap(bitmapTwo); +</pre> + +<h3 id="Asynchronous_display_of_frames_produced_by_an_OffscreenCanvas">Asynchronous display of frames produced by an <code>OffscreenCanvas</code></h3> + +<p>Another way to use the <code>OffscreenCanvas</code> API, is to call {{domxref("HTMLCanvasElement.transferControlToOffscreen", "transferControlToOffscreen()")}} on a {{HTMLElement("canvas")}} element, either on a <a href="/en-US/docs/Web/API/Web_Workers_API">worker</a> or the main thread, which will return an <code>OffscreenCanvas</code> object from an {{domxref("HTMLCanvasElement")}} object from the main thread. Calling {{domxref("OffscreenCanvas.getContext", "getContext()")}} will then obtain a <code>RenderingContext</code> from that <code>OffscreenCanvas</code>.</p> + +<p>main.js (main thread code):</p> + +<pre class="brush: js notranslate">var htmlCanvas = document.getElementById("canvas"); +var offscreen = htmlCanvas.transferControlToOffscreen(); + +var worker = new Worker("offscreencanvas.js"); +worker.postMessage({canvas: offscreen}, [offscreen]); +</pre> + +<p>offscreencanvas.js (worker code):</p> + +<pre class="brush: js notranslate">onmessage = function(evt) { + var canvas = evt.data.canvas; + var gl = canvas.getContext("webgl"); + + // ... some drawing using the gl context ... +}; +</pre> + +<p>You can also use requestAnimationFrame in workers</p> + +<pre class="brush: js notranslate">onmessage = function(evt) { + const canvas = evt.data.canvas; + const gl = canvas.getContext("webgl"); + + function render(time) { + // ... some drawing using the gl context ... + requestAnimationFrame(render); + } + requestAnimationFrame(render); +};</pre> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('HTML WHATWG', "scripting.html#the-offscreencanvas-interface", "OffscreenCanvas")}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + +<div> + + +<p>{{Compat("api.OffscreenCanvas")}}</p> +</div> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="https://hacks.mozilla.org/2016/01/webgl-off-the-main-thread/">WebGL Off the Main Thread – Mozilla Hacks</a></li> + <li>{{domxref("CanvasRenderingContext2D")}}</li> + <li>{{domxref("ImageBitmap")}}</li> + <li>{{domxref("ImageBitmapRenderingContext")}}</li> + <li>{{domxref("HTMLCanvasElement.transferControlToOffscreen()")}}</li> + <li>{{domxref("WebGLRenderingContext.commit()")}}</li> +</ul> |