以下のようにコードを書いていますが、画像がうまく反映されません。
エラーは発生していません。
画像をcanvasを使って表示したいのですが、一部分しか表示されません。
例えば、600 x 600の画像を読み込んだ場合、
表示が300 x 150になって頭の部分しか見れません。
drawImageが効いていないように見受けられます。
こちらはなぜでしょうか。
ご回答していただければ幸いです。
jQuery
1const ctx = resizePic[0].getContext("2d"); 2 3 $("#pic").on("change", function (event) { 4 const file = event.target.files[0]; 5 if (!file) return; 6 const reader = new FileReader(); 7 8 reader.onload = (event) => { 9 const image = new Image(); 10 image.src = event.target.result; 11 12 image.onload = function () { 13 $("#resize-pic").width(this.width).height(this.height); 14 15 let w = Number(this.width); 16 let h = Number(this.height); 17 18 ctx.drawImage(image, 0, 0, w, h); 19 }; 20 }; 21 22 reader.readAsDataURL(file);
HTML
1<div class="wrapper"> 2 <div class="get-image"> 3 <p>画像読み込み</p> 4 <input type="file" id="pic" /> 5 </div> 6 <div class="image-resize" id="image-resize"> 7 <p>リサイズ画像</p> 8 <canvas class="canvas" id="resize-pic"></canvas> 9 </div> 10 </div>

回答1件
あなたの回答
tips
プレビュー