実現したいこと
デバッグあるいはエラーの原因の特定方法を教えて頂けないでしょうか。
https://upload0605.web.fc2.com/sample_nov292024/idx.html
発生している問題・分からないこと
オセロゲームでの画像が一部表示されない。
エラーメッセージ
error
1 Uncaught (in promise) TypeError: CanvasRenderingContext2D.drawImage: Argument 1 could not be converted to any of: HTMLImageElement, SVGImageElement, HTMLCanvasElement, HTMLVideoElement, OffscreenCanvas, ImageBitmap, VideoFrame. 2 draw https://upload0605.web.fc2.com/sample_nov292024/js/resource-img.js:47 3 drawToken https://upload0605.web.fc2.com/sample_nov292024/js/app-view.js:107 4 scanBoard https://upload0605.web.fc2.com/sample_nov292024/js/reversi-low.js:24 5 drawToken https://upload0605.web.fc2.com/sample_nov292024/js/app-view.js:88 6 <anonymous> https://upload0605.web.fc2.com/sample_nov292024/js/main.js:11 7resource-img.js:47:6
該当のソースコード
// main.js 'use strict'; document.addEventListener( 'DOMContentLoaded', async () => { reversiCore.init(); await appResource.load(); appView.init(); appView.drawToken(); appView.drawCanPut(); } );
// resource-img.js 'use strict'; /* * variable : * 1. resourceImg: { Object } リソース 画像の名前空間 * 2. holder : { Object } img要素オブジェクトを格納するオブジェクト */ const resourceImg = { holder: {} }; /* * description: 画像ファイルを読み込む関数 * id : { String } id名 * url : { String } 画像のurl * returns : { Promise } プロミスオブジェクト */ resourceImg.load = function({ id, url }) { return new Promise( resolve => { // img: { Object } img要素を表すオブジェクト const img = new Image(); img.onload = resolve; // 読み込み後の処理 img.src = url; // URLを指定 this.holder[ id ] = img; } ); }; // resourceImg.load = function({ id, url }) {} /* * description: 画像を描画する関数 * id : { String } id名 * cobj : { Object } キャンバスのオブジェクト * x : { Number } 画像の水平位置 * y : { Number } 画像の鉛直位置 * w : { Number } 画像の横幅 * h : { Number } 画像の高さ * returns : no return values */ resourceImg.draw = function({ id, cobj, x, y, w, h }) { cobj .ctx .drawImage(this.holder[ id ], x, y, w, h); // エラーが発生する箇所 @@@@@@@@@@@@@@@@ };
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
(検証1)
JavaScript
1document.addEventListener( 2 'DOMContentLoaded', 3 async () => { 4 try { 5 reversiCore.init(); 6 await appResource.load(); 7 appView.init(); 8 appView.drawToken(); 9 appView.drawCanPut(); 10 } catch (err) { 11 console.log(`エラー発生: ${ err.message }`); 12 } 13 } 14);
21:54:09.916 エラー発生: CanvasRenderingContext2D.drawImage:
Argument 1 could not be converted to any of: HTMLImageElement,
SVGImageElement, HTMLCanvasElement, HTMLVideoElement,
OffscreenCanvas, ImageBitmap, VideoFrame. main.js:25:15
(検証2)
JavaScript
1appResource.load = async function() { 2 3 // promises: { Array } プロミスインスタンスを格納する配列 4 const promises = []; 5 6 7 promises.push( resourceImg.load({ id: 'token0', url: 'img/token_0.png' }) ); 8 promises.push( resourceImg.load({ id: 'token1', url: 'img/token_1.png' }) ); 9 promises.push( resourceImg.load({ id: 'square', url: 'img/square.png' }) ); 10 promises.push( resourceImg.load({ id: 'active', url: 'img/active.png' }) ); 11 await Promise 12 .all(promises) 13 .then( res => console.log(res) ) 14 .catch( err => console.log(`エラー: ${ err }`) ); 15};
エラーは検出されず。
補足
特になし
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2024/12/17 09:20