我正在开发一个网站,加载后,它会显示HTML画布元素的编译图像。我的问题是,我无法分辨何时加载图像以将其绘制到画布或如何在我继续下一步之前强制加载图像。我一直在浏览以前关于这个主题的帖子,并尝试了很多不同的解决方案,但没有任何对我有用。
我正在使用web2py所以我确实使用了一些在服务器端运行的Python助手。
当我使用它时:
canvas = document.getElementById('canvas');
context = canvas.getContext('2d');
image1 = new Image();
image1.src = '{{=URL('static', 'images/image-wrap-border-land.png')}}'; //resolves to: '/SNotes/static/images/image-wrap-border-land.png'
image1.onload = function() {
context.drawImage(image1, 0, 0, canvas.width, canvas.height);
};
由于未加载图像,因此onload函数不会执行任何操作并跳过它。我在尝试设置源之前尝试放置onload函数,但仍未绘制图像。
我试过了:
//code from above
if(image1.completed) {
context.drawImage(image1, 0, 0, canvas.width, canvas.height);
}
但是面临同样的问题。
我还研究了图像在加载时产生错误的可能性。我写道:
//code from above
image1.onerror = function() {
console.log('error');
}
但是图像源很好,图像不会产生错误,只需要很长时间才能加载。
有没有办法等待图像在绘制到画布之前加载?
编辑以增加特异性:
我的HTML看起来像这样:
<div id="profile-container" class="container-fluid">
<h1>My Notes</h1>
<div id="empty-orders" class="column-wrapper disable" style="padding-top: 5px; margin: 0 38%;">
<h3>Create a Note and it will appear here</h3>
<button class="btn btn-lg btn-outline">{{=A('GET STARTED', _href=URL('default', 'canvas_board_print'))}}</button>
</div>
<div id="loading" class="column-wrapper">
<i class="fa fa-cog fa-spin fa-3x fa-fw"></i>
</div>
<div id="row1" class="row">
</div>
</div>
我的javascript看起来像这样:
function showOrders(note_orders) {
var orders = note_orders;
//console.log(orders);
var row_num = 1;
var node_num = 1;
if(orders.length > 0) {
for (var i = 0; i != orders.length; i++) {
orders[i].border = JSON.parse(orders[i].border);
orders[i].image_retouching = JSON.parse(orders[i].image_retouching);
orders[i].shipping_info = JSON.parse(orders[i].shipping_info);
var new_node = $("<div>", {id: "node" + node_num, "class": "col-xs-3 node-wrapper"});
var new_canvas = $('<canvas>', {id: "canvas" + node_num, style: 'display: none'});
var new_image = $('<img>', {id: "note_prev" + node_num, 'class': 'img-responsive'});
$('#row' + row_num).append(new_node);
$('#node'+ node_num).append(new_canvas).append(new_image).append(processOrders(orders[i], node_num));
node_num++;
if (i != 0 && (i + 1) % 4 == 0) {
row_num++;
var new_row = $("<div>", {id: "row" + row_num, "class": "row"});
$(' #profile-container ').append(new_row);
}
}
$(' #loading ').addClass('disable');
} else {
$(' #loading ').addClass('disable');
$(' #empty-orders ').removeClass('disable');
}
}
function processOrders(curr_order, node_num) {
var canvas = document.getElementById('canvas' + node_num);
var context = canvas.getContext('2d');
var image1 = new Image();
image1.src = curr_order.image_url;
canvas.width = image1.naturalWidth;
canvas.height = image1.naturalHeight;
if(image1.complete) {
context.drawImage(image1, 0, 0, canvas.width, canvas.height);
if(curr_order.border.style == 'color_wrap') {
document.getElementById('note_prev' + node_num).style.border = "10px solid " + curr_order.border.color;
} else if(curr_order.border.style == 'image_wrap') {
var image_wrap = new Image();
if(canvas.width > canvas.height) {
image_wrap.src = '{{=URL('static', 'images/image-wrap-border-land.png')}}';
} else {
image_wrap.src = '{{=URL('static', 'images/image-wrap-border-port.png')}}';
}
console.log(image_wrap);
image_wrap.onload = function() {
context.drawImage(image_wrap, 0, 0, canvas.width, canvas.height);
};
image_wrap.onerror = function() {
console.log('errors');
}
}
}
document.getElementById('note_prev' + node_num).src = canvas.toDataURL('image/png');
var node_div = document.createElement('div');
//Other non-image informantion is appended to node_div
return node_div;
}
我的问题是在上面的例子中绘制image_wrap而不是image1。我的代码中的Image1绘制得很好,但是当绘制到画布时,image_wrap不会出现。
我固定了它!!!
我通过创建image1变量来移动image_Wrap变量的创建。
function processOrders(curr_order, node_num) {
var canvas = document.getElementById('canvas' + node_num);
var context = canvas.getContext('2d');
var image1 = new Image();
var image_wrap = new Image();
image1.src = curr_order.image_url;
canvas.width = image1.naturalWidth;
canvas.height = image1.naturalHeight;
if(image1.complete) {
context.drawImage(image
我认为这不是一个好主意,因为我不想创建额外的变量,即使它们不会被使用,但我想它已修复它。
答案 0 :(得分:3)
我可以使用您的代码加载图片。
您可以确认加载图片时没有收到错误吗?
(寻求澄清: 发布作为演示正在运行的代码段的答案)
更新:添加了一种hackish方式,在加载图片之后等待一秒钟并将其绘制到画布之前
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var image1 = new Image();
image1.src = 'http://lorempixel.com/400/200/sports/1/';
image1.onload = function() {
// you could draw the image right away
// ctx.drawImage(image1, 0, 0, canvas.width, canvas.height);
// OR
// You could wait for an extra second before drawing the image (hackish, I know)
setTimeout(function() {
ctx.drawImage(image1, 0, 0, canvas.width, canvas.height);
}, 1 * 1000); // 1 second (in milliseconds)
};
&#13;
<h2>canvas</h2>
<canvas id='canvas'>
</canvas>
<hr/>
&#13;
答案 1 :(得分:2)
在设置src之前设置onload函数。在设置onload函数之前,图像可能正在加载。
canvas = document.getElementById('canvas');
context = canvas.getContext('2d');
image1 = new Image();
image1.onload = function() {
context.drawImage(image1, 0, 0, canvas.width, canvas.height);
};
image1.src = '{{=URL('static', 'images/image-wrap-border-land.png')}}'; //resolves to: '/SNotes/static/images/image-wrap-border-land.png'
答案 2 :(得分:1)
context.drawImage的第一个参数是你想要绘制的图像,后两个是上下文中的位置....
context.drawImage(img,sx,sy,swidth,sheight,x,y,width,height);
其余参数可让您缩放图像。
建议你试试..
image1.onload = function() { context.drawImage(image1,0,0); }