キャンバスにdrawImage()を使用して画像をトリミングする方法は数多くあります。クロップキャンバス/特定の幅と高さを持つhtml5キャンバスをエクスポート
context.drawImage(imageObj, sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight);
しかし、私はユーザーのブラウザを埋めるキャンバスを持っています。キャンバスをイメージとしてエクスポートすると、640px * 480pxの領域のみを(0 | 0)からエクスポートしたいと考えています。
問題:javascriptにtoDataURL()のキャンバスのみを使用するように指示するにはどうすればよいですか?ここで
は、私がこれまで持っているものです。
$("#submitGraphic").click(function(){
var canvas = document.getElementsByTagName("canvas");
// canvas context
var context = canvas[0].getContext("2d");
// get the current ImageData for the canvas
var data = context.getImageData(0, 0, canvas[0].width, canvas[0].height);
// store the current globalCompositeOperation
var compositeOperation = context.globalCompositeOperation;
// set to draw behind current content
context.globalCompositeOperation = "destination-over";
//set background color
context.fillStyle = "#FFFFFF";
// draw background/rectangle on entire canvas
context.fillRect(0,0,canvas[0].width,canvas[0].height);
// not working, seems to clear the canvas? browser hangs?
// seems that I can click a white image in the background
/*canvas[0].width = 640;
canvas[0].height = 480;*/
// not working either
/*canvas[0].style.width = '640px';
canvas[0].style.height = '480px';*/
// not working at all
/*context.canvas.width = 640;
context.canvas.height = 480;*/
// write on screen
var img = canvas[0].toDataURL("image/png");
document.write('<a href="'+img+'"><img src="'+img+'"/></a>');
})
PS:私はちょうど、固定窓にトリミング/クリッピング、サイズ変更や規模にしたくありません。 Here canvas.widthとcanvas.heightを指定するだけですが、キャンバスはクリアされます。
素晴らしい、うまくいきます! /私を苛立たせるのは、画像が既に表示されているときに、Firefoxがページの読み込みを指示する(タブが円を描く)ことだけです。 ESCをクリックすると、URLにwyciwyg://が表示されます。 ... "wyciwygは、document.write result pagesを表すために使用される内部URIスキームです。"とにかく、あなたが私に実用的なソリューションをくれたことをうれしく思います! –
"data"変数は割り当てられていますが使用されていません... –
もcompositeOperation変数 –