javascript - Can't Draw Image on HTML5 Canvas in Phonegap - Stack Overflow

I am trying to draw an image to an Html5 Canvas to be used withing Phonegap, this is the code i used.th

I am trying to draw an image to an Html5 Canvas to be used withing Phonegap, this is the code i used.

the problem is nothing happens i tried almost everything, added the image.onload, tried clearrect still nothing is drawn.

I am using Phonegap 2.7, testing on Android 2.3.7 (GingerBread).

Fiddle

var canvas = document.getElementById('map');
var ctx = canvas.getContext("2d");
var image = new Image();
image.src="img/map-04.png";
image.onload=function(){            
    ctx.drawImage(image,0,0);
};              
function fitToContainer(canvas){
    var div = $(mapcontext);    
    canvas.style.width=div.css("width");
    canvas.style.height=div.css("height");
    canvas.width  = canvas.offsetWidth;
    canvas.height = canvas.offsetHeight;                
}

window.onload = fitToContainer(canvas);
window.onresize = fitToContainer(canvas);

Thanks in Advance.

I am trying to draw an image to an Html5 Canvas to be used withing Phonegap, this is the code i used.

the problem is nothing happens i tried almost everything, added the image.onload, tried clearrect still nothing is drawn.

I am using Phonegap 2.7, testing on Android 2.3.7 (GingerBread).

Fiddle

var canvas = document.getElementById('map');
var ctx = canvas.getContext("2d");
var image = new Image();
image.src="img/map-04.png";
image.onload=function(){            
    ctx.drawImage(image,0,0);
};              
function fitToContainer(canvas){
    var div = $(mapcontext);    
    canvas.style.width=div.css("width");
    canvas.style.height=div.css("height");
    canvas.width  = canvas.offsetWidth;
    canvas.height = canvas.offsetHeight;                
}

window.onload = fitToContainer(canvas);
window.onresize = fitToContainer(canvas);

Thanks in Advance.

Share Improve this question edited Apr 5, 2014 at 11:19 user1693593 asked Apr 4, 2014 at 15:52 Ahmed SaadAhmed Saad 1772 silver badges12 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

Update

Based on the provided fiddle:

You will be able to solve the problem by reorganizing the code a bit by doing:

  • Make sure the image has loaded before doing anything
  • When image is loaded call the draw function and attach the onresize handler for window
  • Remove the draw function parameters so you can set it directly as a callback and also to use the already allocated canvas variable globally
  • Inside the draw function use the window's dimension directly for the canvas element's width and height, don't use CSS as that will stretch the image (it's better to redraw the image stretched onto canvas so you can read mouse positions etc. properly).
  • Redraw image every time the canvas changes size

The updated code looks like this (online live version here):

var canvas = document.getElementById('map');
var ctx = canvas.getContext("2d");

var image = new Image();
image.onload = function() {
    window.onresize = fitToContainer;
    fitToContainer();
};
image.src = "http://i.imgur./oPt1Nrr.gif";

function fitToContainer() {

    var div = $('#mapcontext'),
        w = $(window).width(),
        h = $(window).height();

    canvas.width = w;
    canvas.height = h;

    ctx.drawImage(image, 0, 0, w, h);
}

Hope this helps!


Old answer

The usage of window.onload or window.onresize are wrong. You need to pass them references:

window.onload = window.onresize = function() {fitToContainer(canvas)};

You could optionally obtain the canvas element within the function callback and simply call a parameterless function

window.onload = window.onresize = fitToContainer;

function fitToContainer() { ... } // use global var for canvas

Also note that resizing canvas will cause it to be cleared so you'll need to redraw the image when this happens:

function fitToContainer(canvas){
    var div = $(mapcontext);    
    canvas.style.width=div.css("width");
    canvas.style.height=div.css("height");
    canvas.width  = canvas.offsetWidth;     // this will clear canvas
    canvas.height = canvas.offsetHeight;

    if (image && image.plete)
        ctx.drawImage(image, 0, 0);         // redraw image if loaded
}

You should also add an onerror handler (and optimally onabort) for the image in case the error occur during the loading and decoding process. Also add those callbacks before setting src.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745386433a4625459.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信