亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁/技術文章
文章詳情頁

javascript - [js]為什么畫布里不出現圖片呢?在線等

瀏覽:76日期:2023-03-25 08:21:31

問題描述

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

問題解答

回答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>

上面這段代碼是正常的,因為你的圖片在load_source的時候,是通過img.onload異步畫到load_source里面的canvas上的,然而,在那個時間之前,img上是沒有圖像的,所以load_source里面的canvas也是沒圖像的。

但是,在那個時間之前,DOM里的canvas已經就緒,而且執行了ctxs.drawImage(source,110,110)。由于此時的load_source里的canvas還是空的(里面的圖還沒加載完畢,里面的畫布也就沒有內容),所以source也就是空的,所以ctx.drawImage(source,110,110)畫上去的東西也是空的。

回答2:

試一哈下面這段代碼,感覺是在做課程設計的樣子

javascript - [js]為什么畫布里不出現圖片呢?在線等

<!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){ //獲取對應的CanvasRenderingContext2D對象(畫筆)var ctx = canvas.getContext('2d');//創建新的圖片對象var img = new Image();//指定圖片的URLimg.src = 'http://htmljs.b0.upaiyun.com/uploads/1382542991440-2angles.png';//瀏覽器加載圖片完畢后再繪制圖片img.onload = function(){ //以Canvas畫布上的坐標(10,10)為起始點,繪制圖像 ctx.drawImage(img, 10, 10); }; }</script></html>

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 久久精品中文字幕一区 | 日本第一次处毛片 | 国产精品麻豆入口 | 国产美女精品自拍 | 精品人成| 久久国产精品久久国产精品 | 久久中文字幕网 | 国产精品日日摸夜夜添夜夜添1 | 黄 色 成 年人在线 黄 色 成 年人网站 | 久久aaa| 国产在线观看入口 | 亚洲欧美一区二区三区综合 | 世界一级毛片 | 免费一级a毛片免费观看欧美大片 | 在线婷婷 | 365深夜福利在线观看 | 国产99视频精品免视看7 | 欧美亚洲色图视频 | 手机在线看片国产日韩生活片 | 网站在线你懂的 | 国产精品自拍一区 | 日韩一级黄色大片 | 一区二区三区高清在线观看 | 一级女性全黄生活片免费看 | 亚洲精品国产拍拍拍拍拍 | 国产凹凸一区在线观看视频 | 久久福利在线 | 国产成人综合久久亚洲精品 | 色噜噜狠狠狠综合曰曰曰88av | 最新亚洲手机在线人成网站 | 久久99精品久久久久久青青日本 | 亚洲福利视频一区二区三区 | zljzlj日本妈妈| 自拍 亚洲| 免费黄色国产视频 | 国产亚洲精品精品国产亚洲综合 | 国产福利在线观看精品 | 色婷婷亚洲精品综合影院 | 色片在线免费观看 | 国产在线观看91精品2021 | 国产精品入口麻豆 |