javascript - [js] Mengapa gambar tidak muncul dalam kanvas? Menunggu dalam talian
欧阳克
欧阳克 2017-07-05 10:46:45
0
2
671
<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

membalas semua(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>

Kod di atas adalah perkara biasa, kerana imej anda tidak mempunyai imej dalam kanvas di dalam load_source的时候,是通过img.onload异步画到load_source里面的canvas上的,然而,在那个时间之前,img上是没有图像的,所以load_source.

Walau bagaimanapun, sebelum masa itu, kanvas dalam DOM sudah sedia, dan perkara yang dilukis di atasnya selepas melaksanakan ctxs.drawImage(source,110,110)。由于此时的load_source里的canvas还是空的(里面的图还没加载完毕,里面的画布也就没有内容),所以source也就是空的,所以ctx.drawImage(source,110,110)juga kosong.

为情所困

Cuba kod berikut, rasa macam 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>


Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan