aboutsummaryrefslogtreecommitdiff
path: root/files/it/web/api/htmlcanvaselement/toblob
diff options
context:
space:
mode:
Diffstat (limited to 'files/it/web/api/htmlcanvaselement/toblob')
-rw-r--r--files/it/web/api/htmlcanvaselement/toblob/index.html262
1 files changed, 262 insertions, 0 deletions
diff --git a/files/it/web/api/htmlcanvaselement/toblob/index.html b/files/it/web/api/htmlcanvaselement/toblob/index.html
new file mode 100644
index 0000000000..f0793eb393
--- /dev/null
+++ b/files/it/web/api/htmlcanvaselement/toblob/index.html
@@ -0,0 +1,262 @@
+---
+title: HTMLCanvasElement.toBlob()
+slug: Web/API/HTMLCanvasElement/toBlob
+translation_of: Web/API/HTMLCanvasElement/toBlob
+---
+<div>
+<div>
+<div>{{APIRef("Canvas API")}}</div>
+</div>
+</div>
+
+<p>Il metodo<strong><code>HTMLCanvasElement.toBlob()</code></strong> crea un oggetto {{domxref("Blob")}} rappresentante l'immagine contenuta nel canvas;  questo file potrebbe essere immagazzinato per usi futuri su disco o in memoria a discrezione dell'user agent. Se <code>type</code> non è specificato, il tipo immagine è impostato a<code>image/png</code>. L'immagine creata ha una risoluzione di 96dpi.<br>
+ Il terzo argomento è usato con immagini di tipo<code>image/jpeg</code> per specificare la qualità dell'output.</p>
+
+<h2 id="Sintassi">Sintassi</h2>
+
+<pre class="syntaxbox">void <var>canvas</var>.toBlob(<var>callback</var>, <var>mimeType</var>, <var>qualityArgument</var>);
+</pre>
+
+<h3 id="Parametri">Parametri</h3>
+
+<dl>
+ <dt>callback</dt>
+ <dd>Funzione di callback con l'oggetto {{domxref("Blob")}} risultante come singolo argomento.</dd>
+ <dt><code>mimeType</code> {{optional_inline}}</dt>
+ <dd>Un oggetto {{domxref("DOMString")}} indicante il tipo immagine. Quella di default è<code>image/png</code>.</dd>
+ <dt><code>qualityArgument</code> {{optional_inline}}</dt>
+ <dd>Un oggetto {{jsxref("Number")}} tra<code>0</code> e<code>1</code> indicante la qualità immagine se il tipo richiesto è <code>image/jpeg </code>o<code>image/webp</code>. Se questo argomento è diverso dai due precedenti è utilizzato il valore di qualità immagine di default. Altri argomenti verranno ignorati.</dd>
+</dl>
+
+<h3 id="Valore_ritornato">Valore ritornato</h3>
+
+<p>Nessuno.</p>
+
+<h2 id="Esempi">Esempi</h2>
+
+<h3 id="Ottenere_un_file_rappresentante_il_canvas">Ottenere un file rappresentante il canvas</h3>
+
+<p>Una volta aver disegnato il contenuto in un canvas, è possibile convertirlo in un file di un qualsiasi. Lo snippet di codice in basso, ad esempio, prende l'immagine contenuta nell'elemento {{HTMLElement("canvas")}} avente ID "canvas", ottenendo una sua copia in formato immagine PNG, quindi aggiunge un nuovo elemento {{HTMLElement("img")}} al documento, la cui immagine di origine è quella creata utilizzando il canvas.</p>
+
+<pre class="brush: js">var canvas = document.getElementById('canvas');
+
+canvas.toBlob(function(blob) {
+ var newImg = document.createElement('img'),
+ url = URL.createObjectURL(blob);
+
+ newImg.onload = function() {
+ // no longer need to read the blob so it's revoked
+ URL.revokeObjectURL(url);
+ };
+
+ newImg.src = url;
+ document.body.appendChild(newImg);
+});
+</pre>
+
+<p>Si noti che si sta creando un'immagine PNG; se si specifica un secondo parametro nella chiamata a <code>toBlob()</code>,  si può specificare il formato immagine. Ad esempio, per ottenere l'immagine in formato JPEG:</p>
+
+<pre class="brush: js"> canvas.toBlob(function(blob){...}, 'image/jpeg', 0.95); // JPEG at 95% quality</pre>
+
+<div>
+<h3 id="Convertire_un_canvas_in_icona_(solo_su_Mozilla)">Convertire un canvas in icona (solo su Mozilla)</h3>
+
+<p>Ora si userà <code>-moz-parse</code> per convertire un canvas in icona. Windows XP non supporta la conversione dal formato PNG ad ICO, quindi usa quello bitmap (BMP) al suo posto. Un link di download è creato impostando l'attributo download. Il valore dello stesso è il nome che verrà utilizzato come nome del file.</p>
+
+<pre class="brush: js">var canvas = document.getElementById('canvas');
+var d = canvas.width;
+ctx = canvas.getContext('2d');
+ctx.beginPath();
+ctx.moveTo(d / 2, 0);
+ctx.lineTo(d, d);
+ctx.lineTo(0, d);
+ctx.closePath();
+ctx.fillStyle = 'yellow';
+ctx.fill();
+
+function blobCallback(iconName) {
+ return function(b) {
+ var a = document.createElement('a');
+ a.textContent = 'Download';
+ document.body.appendChild(a);
+ a.style.display = 'block';
+ a.download = iconName + '.ico';
+ a.href = window.URL.createObjectURL(b);
+ }
+}
+canvas.toBlob(blobCallback('passThisString'), 'image/vnd.microsoft.icon',
+ '-moz-parse-options:format=bmp;bpp=32');</pre>
+</div>
+
+<h3 id="Salvare_toBlob_su_disco_con_OS.File_(valido_solo_per_chromeadd-on)">Salvare toBlob su disco con OS.File (valido solo per chrome/add-on)</h3>
+
+<div class="note">
+<p>This technique saves it to the desktop and is only useful in Firefox chrome context or add-on code as OS APIs are not present on web sites.</p>
+</div>
+
+<pre class="brush: js">var canvas = document.getElementById('canvas');
+var d = canvas.width;
+ctx = canvas.getContext('2d');
+ctx.beginPath();
+ctx.moveTo(d / 2, 0);
+ctx.lineTo(d, d);
+ctx.lineTo(0, d);
+ctx.closePath();
+ctx.fillStyle = 'yellow';
+ctx.fill();
+
+function blobCallback(iconName) {
+ return function(b) {
+ var r = new FileReader();
+ r.onloadend = function () {
+ // r.result contains the ArrayBuffer.
+ Cu.import('resource://gre/modules/osfile.jsm');
+ var writePath = OS.Path.join(OS.Constants.Path.desktopDir,
+ iconName + '.ico');
+ var promise = OS.File.writeAtomic(writePath, new Uint8Array(r.result),
+ {tmpPath:writePath + '.tmp'});
+ promise.then(
+ function() {
+ console.log('successfully wrote file');
+ },
+ function() {
+ console.log('failure writing file')
+ }
+ );
+ };
+ r.readAsArrayBuffer(b);
+ }
+}
+
+canvas.toBlob(blobCallback('passThisString'), 'image/vnd.microsoft.icon',
+ '-moz-parse-options:format=bmp;bpp=32');</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#dom-canvas-toblob", "HTMLCanvasElement.toBlob")}}</td>
+ <td>{{Spec2('HTML WHATWG')}}</td>
+ <td>No change since the latest snapshot, {{SpecName('HTML5 W3C')}}</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('HTML5.1', "scripting-1.html#dom-canvas-toblob", "HTMLCanvasElement.toBlob")}}</td>
+ <td>{{Spec2('HTML5.1')}}</td>
+ <td>No change</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('HTML5 W3C', "scripting-1.html#dom-canvas-toblob", "HTMLCanvasElement.toBlob")}}</td>
+ <td>{{Spec2('HTML5 W3C')}}</td>
+ <td>Snapshot of the {{SpecName('HTML WHATWG')}} containing the initial definition.</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_compatibility">Browser compatibility</h2>
+
+<p>{{CompatibilityTable}}</p>
+
+<div id="compat-desktop">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Chrome</th>
+ <th>Firefox (Gecko)</th>
+ <th>Internet Explorer</th>
+ <th>Opera</th>
+ <th>Safari</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>{{CompatChrome("50")}}</td>
+ <td>{{CompatGeckoDesktop('19')}}</td>
+ <td>{{CompatIE(10)}}{{property_prefix("ms")}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}<sup>[1]</sup></td>
+ </tr>
+ <tr>
+ <td>Image quality parameter</td>
+ <td>{{CompatChrome("50")}}</td>
+ <td>{{CompatGeckoDesktop('25')}}</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatNo}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<div id="compat-mobile">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Android</th>
+ <th>Android Webview</th>
+ <th>Firefox Mobile (Gecko)</th>
+ <th>IE Mobile</th>
+ <th>Opera Mobile</th>
+ <th>Safari Mobile</th>
+ <th>Chrome for Android</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatChrome("50")}}</td>
+ <td>{{CompatGeckoMobile("19")}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatChrome("50")}}</td>
+ </tr>
+ <tr>
+ <td>Image quality parameter</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatChrome("50")}}</td>
+ <td>{{CompatGeckoMobile("25")}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatChrome("50")}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<p>[1] See {{WebKitBug("71270")}}.</p>
+
+<h2 id="Polyfill">Polyfill</h2>
+
+<p>A low performance polyfill based on toDataURL.</p>
+
+<pre class="brush: js">if (!HTMLCanvasElement.prototype.toBlob) {
+ Object.defineProperty(HTMLCanvasElement.prototype, 'toBlob', {
+ value: function (callback, type, quality) {
+
+ var binStr = atob( this.toDataURL(type, quality).split(',')[1] ),
+ len = binStr.length,
+ arr = new Uint8Array(len);
+
+ for (var i = 0; i &lt; len; i++ ) {
+ arr[i] = binStr.charCodeAt(i);
+ }
+
+ callback( new Blob( [arr], {type: type || 'image/png'} ) );
+ }
+ });
+}
+</pre>
+
+<h2 id="See_also">See also</h2>
+
+<ul>
+ <li>The interface defining it, {{domxref("HTMLCanvasElement")}}.</li>
+ <li>{{domxref("Blob")}}</li>
+</ul>