JavaScript:Canvas.drawImage不显示任何图像

时间:2013-10-29 18:38:59

标签: javascript image canvas

我写了这个简单的HTML代码......

<!DOCTYPE html>
<html>
<head>
    <title>Doc</title>
</head>
<body>
<canvas id = 'myCanvas' width = '800' height = '560'>Ah Snap!</canvas>
<script>
    var canvas = document.getElementById('myCanvas');
    var context = canvas.getContext('2d');

    var img = new Image();
    img.src = 'E:\HTMLPaint1\image123.jpg';

    img.onload = function() {
        context.drawImage(img, 278, 117, 200, 200);
    }

</script>
</body>
</html>

跑步后,它不会显示任何图像!! (路径是正确的)

1 个答案:

答案 0 :(得分:0)

我做了一个例子 看这里: examples

window.onload = function(){

       var canvas = document.getElementById("myCanvas");
       var context = canvas.getContext("2d"); 
       var imageObj = new Image();

       imageObj.onload = function(){
                  var destX = canvas.width / 2 - this.width / 2;
                  var destY = canvas.height / 2 - this.height / 2;
                  context.drawImage(this, destX, destY); 
        };
     imageObj.src = "http://lh3.ggpht.com/YOsk5uW00GWrypKD_2PmGz1sGjsLZs4TMtUWUgX5RCTufXhO_HbpCjJz23K-m0KJX8QQv3ihghMhBFuB2I6nO6GXslhfR3GtTw2HQMYvYw";

};