イメージblobをキャンバス要素にレンダリングする方法は?キャンバス要素にblobをレンダリングする方法は?
これまでのところ、私は in this codepen, it just returns the default black image.
var canvas = document.getElementById('canvas');
var input = document.getElementById('input');
var ctx = canvas.getContext('2d');
var photo;
function picToBlob() {
var file = input.files[0];
canvas.toBlob(function(blob) {
var newImg = document.createElement("img"),
url = URL.createObjectURL(blob);
newImg.onload = function() {
ctx.drawImage(this, 0, 0);
photo = blob;
URL.revokeObjectURL(url);
};
newImg.src = url;
}, file.type, 0.5);
canvas.renderImage(photo);
}
HTMLCanvasElement.prototype.renderImage = function(blob) {
var canvas = this;
var ctx = canvas.getContext('2d');
var img = new Image();
img.onload = function() {
ctx.drawImage(img, 0, 0)
}
img.src = URL.createObjectURL(blob);
}
input.addEventListener('change', picToBlob, false);
の単純化された作業バージョンは、あなたが非同期であるあなたの 'canvas.renderImage'に(あなたはそれを描く' canvas.toBlob'beforeを呼んでいるのですbtw)なぜあなたは透明な画像を出力するのだろうか?私はそれをエクスポートしようとしたときにあなたのキャンバスに何も描かなかったからだと言いたい。 Ps:あなたの 'renderImage'では、URLオブジェクトも取り消すことを忘れないでください。おそらくキャンバスのサイズを変更する必要があります。 – Kaiido