javascript - [js]為什么畫(huà)布里不出現(xiàn)圖片呢?在線等
問(wèn)題描述
<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>
問(wèn)題解答
回答1:<!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>
上面這段代碼是正常的,因?yàn)槟愕膱D片在load_source的時(shí)候,是通過(guò)img.onload異步畫(huà)到load_source里面的canvas上的,然而,在那個(gè)時(shí)間之前,img上是沒(méi)有圖像的,所以load_source里面的canvas也是沒(méi)圖像的。
但是,在那個(gè)時(shí)間之前,DOM里的canvas已經(jīng)就緒,而且執(zhí)行了ctxs.drawImage(source,110,110)。由于此時(shí)的load_source里的canvas還是空的(里面的圖還沒(méi)加載完畢,里面的畫(huà)布也就沒(méi)有內(nèi)容),所以source也就是空的,所以ctx.drawImage(source,110,110)畫(huà)上去的東西也是空的。
回答2:試一哈下面這段代碼,感覺(jué)是在做課程設(shè)計(jì)的樣子
<!DOCTYPE html><html><head></head><body><p id='main'> <canvas style='border: 1px solid red;'> </canvas></p><script type='text/javascript'> var canvas = document.getElementById('myCanvas'); if(canvas.getContext){ //獲取對(duì)應(yīng)的CanvasRenderingContext2D對(duì)象(畫(huà)筆)var ctx = canvas.getContext('2d');//創(chuàng)建新的圖片對(duì)象var img = new Image();//指定圖片的URLimg.src = 'http://htmljs.b0.upaiyun.com/uploads/1382542991440-2angles.png';//瀏覽器加載圖片完畢后再繪制圖片img.onload = function(){ //以Canvas畫(huà)布上的坐標(biāo)(10,10)為起始點(diǎn),繪制圖像 ctx.drawImage(img, 10, 10); }; }</script></html>
相關(guān)文章:
1. android - rxjava merge 返回Object對(duì)象數(shù)據(jù)如何緩存2. docker 下面創(chuàng)建的IMAGE 他們的 ID 一樣?這個(gè)是怎么回事????3. angular.js - ionic2 瀏覽器跨域問(wèn)題4. python - Scrapy存在內(nèi)存泄漏的問(wèn)題。5. javascript - 螞蟻金服里的react Modal方法,是怎么把元素插入到頁(yè)面最后的6. mysql 一個(gè)sql 返回多個(gè)總數(shù)7. 如何用筆記本上的apache做微信開(kāi)發(fā)的服務(wù)器8. docker-compose中volumes的問(wèn)題9. CSS3 畫(huà)如下圖形10. java - 三位二進(jìn)制表示8進(jìn)制,四位二進(jìn)制表示16進(jìn)制,那么多少二進(jìn)制表示10進(jìn)制呢?
