简体   繁体   中英

JavaScript: Canvas.drawImage Not showing any image

I wrote this simple html code...

<!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>

After running, it don't show any image!! (Path is correct)

I made ​​an example look here: 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";

};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM