我正在使用此代码创建带有blob网址的img标记:
blob = canvas.toBlob(function (blob) {
var newImg = window.parent.document.createElement('img'),
url = window.URL.createObjectURL(blob);
newImg.onload = function () {
// no longer need to read the blob so it's revoked
window.URL.revokeObjectURL(url);
};
newImg.src = url;
console.log(url); // prints blob url of blank/white image
window.parent.document.body.appendChild(newImg);
}, 'image/png', 1.0);
console.log(blob); // returns undefined
是的我得到blob网址,但是我在网址中打开网址时的图片只显示空白/白色图片。
我做错了什么?
我甚至尝试过toDataURL():
var canvasblobapp = (function (jq) {'use strict';
return {
pageReady: function () {
var img = new Image(),
canvas = document.getElementById('myCanvas'),
ctx = canvas.getContext('2d'),
blob = null,
base64String = "",
newImg = new Image(),
imgTag = document.createElement("img");
img.onload = function () {
canvas.width = this.width;
canvas.height = this.height;
//ctx.drawImage(img, 0, 0);
ctx.beginPath();
ctx.moveTo(170, 80);
ctx.bezierCurveTo(130, 100, 130, 150, 230, 150);
ctx.bezierCurveTo(250, 180, 320, 180, 340, 150);
ctx.bezierCurveTo(420, 150, 420, 120, 390, 100);
ctx.bezierCurveTo(430, 40, 370, 30, 340, 50);
ctx.bezierCurveTo(320, 5, 250, 20, 250, 50);
ctx.bezierCurveTo(200, 5, 150, 20, 170, 80);
ctx.closePath();
ctx.lineWidth = 5;
ctx.fillStyle = '#8ED6FF';
ctx.fill();
ctx.strokeStyle = '#0000ff';
ctx.stroke();
};
img.src = 'img/jabolche.png';
imgTag.src = canvas.toDataURL("image/png", 1.0);
imgTag.id = "novaSlika";
document.getElementById('result').appendChild(imgTag);
}
};
}($));
$(document).ready(canvasblobapp.pageReady);
但同样,获得带有SCR填充有 “A” 的信空图像 “数据:图像/ PNG; BASE64,iVBORw0KGgoAAAANSUhEUgAAAeAAAAIQCAYAAABZrr0RAAAD7klEQVR4nO3BMQEAAADCoPVPbQo / oAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAlwF68QABHxjrnwAAAABJRU5ErkJggg ==”
好像“A”表示白色像素。不确定吗?
答案 0 :(得分:1)
查看文档:
void canvas.toBlob(callback,mimeType,qualityArgument);
该函数不返回任何内容(void
),因此blob = canvas.toBlob(...
可能不是您想要使用它的方式。
您是否尝试过使用canvas.toDataUrl()
?
(在iframe
中不允许这样做,这就是为什么它被替换为此示例的返回值):
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.arc(95,50,40,0,2*Math.PI);
ctx.stroke();
var newImg = document.createElement('img')
//Result from: canvas.toDataUrl('image/jpeg', 1.0);
newImg.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAABkCAYAAADDhn8LAAAIsUlEQVR4Xu2deYxeUxjGf40ttjSorYqIXShNqJ0WrSqtXQnVPxAaaUQstURICKklIiISNIglIa2tFJVSaiuiSVtb7X9oiV1iTSx5OmfMdOab+e693/2+ud85z0luZpI56/O+z5ztfd8zCCcjYAT6RGCQsTECRqBvBEwQa4cR6AcBE8TqYQRMEOuAESiGgGeQYri5VCIImCCJCNrDLIaACVIMN5dKBAETJBFBe5jFEDBBiuFWpNRGwBbAluHT70pfAyvDp99/LFK5yzQHAROkObgeBEwEdgRGBGL8VoMMar0nadYLZFkMfAw8BbzWnG661noImCD1EMr297WACYEUIsZ7QbHnA98HYvyZrSrWCTPMxsARoc5dQ30ii76/M9blbA0iY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kg3EHYLiDQtEWZCtWMty6VBBxFAMYhHZhoclQW+C5ANyZFBERY2XImqfMpBJbrIihiK2qD/25yhZGiZIMUDHBcWUe6q8FJ8uVk3hUjqVGhWOo0UMu8kWhrL/giZIY8BqzT8l+LlrM69LOH16W6PMpHCgCsWjT+RQW3L6ml1mI66rNwImSDlaIRyluJ1KLCckEWV+iA0sa+HfMzalx2v0fLWOmuXIpDq360Y+RzzMCGQZ2UyQMlDsXYd836XY8l7UPmEo8Fd4d3xlt58qKTJ0fsqn/YTyLAaWB2Isak43XWs9BEyQegiV9/fBPcggUih1J4xmmqTfBCwP7nJqMkHKwdG1RIqACRKpYD2schAwQcrB0bVEioAJEqlgPaxyEDBBysHRtUSKgAkSqWA9rHIQMEHKwdG1RIqACRKpYD2schAwQcrB0bVEisB/jEcudDTljn4AAAAASUVORK5CYII=";
console.log(newImg.src);
document.getElementById('result').appendChild(newImg);
<canvas id="myCanvas" width="200" height="100" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>
<div id="result"></div>
答案 1 :(得分:0)
我发现了什么问题。所有这些都应该发生在img.onload()处理程序中:
var canvasblobapp = (function (win, jq) {'use strict';
return {
pageReady: function () {
var img = new Image(),
canvas = document.getElementById('image'),
canvas1 = document.getElementById('illustration'),
ctx = canvas.getContext('2d'),
ctx1 = canvas1.getContext('2d'),
blob = null,
base64String = "",
imgTag = document.createElement("img"),
imgTag1 = document.createElement("img");
// draw the image
img.src = 'img/jabolche.png';
img.onload = function () {
canvas.width = this.width;
canvas.height = this.height;
ctx.drawImage(img, 0, 0);
base64String = canvas.toDataURL("image/png", 0.92);
imgTag.src = base64String;
imgTag.id = "newImage";
window.document.getElementById('result').appendChild(imgTag);
};
// draw the cloud illustration
ctx1.beginPath();
ctx1.moveTo(170, 80);
ctx1.bezierCurveTo(130, 100, 130, 150, 230, 150);
ctx1.bezierCurveTo(250, 180, 320, 180, 340, 150);
ctx1.bezierCurveTo(420, 150, 420, 120, 390, 100);
ctx1.bezierCurveTo(430, 40, 370, 30, 340, 50);
ctx1.bezierCurveTo(320, 5, 250, 20, 250, 50);
ctx1.bezierCurveTo(200, 5, 150, 20, 170, 80);
ctx1.closePath();
ctx1.lineWidth = 5;
ctx1.fillStyle = '#8ED6FF';
ctx1.fill();
ctx1.strokeStyle = '#0000ff';
ctx1.stroke();
imgTag1.src = canvas1.toDataURL("image/png", 0.92);
imgTag1.id = "newIllustration";
document.getElementById('result').appendChild(imgTag1);
}
};
}(window, $));
$(document).ready(canvasblobapp.pageReady);
以下是截图: