javascript - [js] Why don't pictures appear in the canvas? Waiting online
欧阳克
欧阳克 2017-07-05 10:46:45
0
2
670
<body>
    <p id="main"></p>
</p>
<script type="text/javascript">
function GED(ele) {return document.getElementById(ele);}
function load_source(url, w, h) {
    this.canvas = document.createElement('canvas');
    this.canvas.width = w;
    this.canvas.height = h;
    this.ctx = this.canvas.getContext('2d');
    this.img = new Image();
    this.img.src = url;
    this.img.onload = function () {
        this.ctx.drawImage(this.img, 0, 0);
    }.bind(this);
    return this.canvas;
}
source = new load_source('http://htmljs.b0.upaiyun.com/uploads/1382542991440-2angles.png', 300, 100);
canvas = document.createElement('canvas')
canvas.id = 'ff'
canvas.width = 300;
canvas.height = 100;
GED('main').appendChild(canvas);
ctxs = GED('ff').getContext('2d'); 
ctxs.drawImage(source, 110, 110);
</script>
欧阳克
欧阳克

温故而知新,可以为师矣。 博客:www.ouyangke.com

reply all(2)
淡淡烟草味
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <p id="main"></p>
<script type="text/javascript">
function GED(ele){return document.getElementById(ele);}
function load_source(url,w,h){
    this.canvas = document.createElement('canvas');
    this.canvas.width = w;
    this.canvas.height = h;  
    this.ctx = this.canvas.getContext('2d');
    this.img = new Image();
    this.img.src = url;
    this.img.onload = function () {
        this.ctx.drawImage(this.img,0,0);
        window.ctxs.drawImage(source,110,110);
    }.bind(this);
    return this.canvas;
}
source = new load_source('http://htmljs.b0.upaiyun.com/uploads/1382542991440-2angles.png',300,100);
GED('main').appendChild(source);
canvas = document.createElement('canvas')
canvas.id='ff'
canvas.width = 300;
canvas.height = 100;
GED('main').appendChild(canvas);
ctxs= GED('ff').getContext('2d'); 
</script>
</body>
</html>

The above code is normal, because when your image is in load_source, it is drawn asynchronously to the canvas in load_source through img.onload. However, before that time, img There is no image on , so the canvas in load_source also has no image.

However, before that time, the canvas in the DOM is ready, and ctxs.drawImage(source,110,110) is executed. Since the canvas in load_source is still empty at this time (the image inside has not been loaded yet, the canvas inside has no content), so source is also empty, so ctx.drawImage(source,110,110) The things painted on are also empty.

为情所困

Try the following code, it feels like course design


<!DOCTYPE html>
<html>
<head>
</head>
<body>

<p id="main">
    <canvas id="myCanvas" width="400px" height="300px" style="border: 1px solid red;">
    </canvas>
</p>
<script type="text/javascript">

   var canvas = document.getElementById("myCanvas");

   if(canvas.getContext){  
    //获取对应的CanvasRenderingContext2D对象(画笔)
        var ctx = canvas.getContext("2d");
        
        //创建新的图片对象
        var img = new Image();
        //指定图片的URL
        img.src = "http://htmljs.b0.upaiyun.com/uploads/1382542991440-2angles.png";
        //浏览器加载图片完毕后再绘制图片
        img.onload = function(){
            //以Canvas画布上的坐标(10,10)为起始点,绘制图像
            ctx.drawImage(img, 10, 10);             
        };
    }
</script>
</html>


Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template