画像をキャンバスに描く
すでにある画像を直接キャンバスに描くこともできます。このセクションでは、前のステップで作成したお絵描きアプリに画像挿入機能を追加します。
ファイルの用意
Section titled “ファイルの用意”次のように index.html、style.css、main.js、そして画像ファイル(dog.png、bird.png、fish.png)を用意してください。
index.html、style.css、main.js は前の節の draw-line フォルダからコピーします。
./works└── put-image ├── index.html ├── style.css ├── main.js └── assets ├── dog.png ├── bird.png └── fish.png前の節を終えていない人は、ここから始められます。解凍してできる draw-line フォルダの名前を put-image に変えてください。
画像ファイルは、次のボタンからダウンロードするか、自分で用意した画像を使用してください。
前のステップ(マウスで線を描く)のコードを拡張して、以下の機能を追加します:
- 画像をクリックするとキャンバスに描画される機能
1. index.htmlを修正する
Section titled “1. index.htmlを修正する”前のステップの index.html に、画像要素を追加します。
変更点:
- 画像要素(犬、鳥、魚)を追加
- クリック可能なサムネイル画像として表示
index.html を変更する
Section titled “index.html を変更する”33 <link rel="stylesheet" href="./style.css" />44</head>55<body>6 <button id="clearButton">消す</button>7 <input type="color" id="colorInput" value="#000000" />6 <div>7 <button id="clearButton">消す</button>8 <input type="color" id="colorInput" value="#000000" />9 </div>10 <div>11 <img id="dogImage" src="./assets/dog.png" width="100" height="100" />12 <img id="birdImage" src="./assets/bird.png" width="100" height="100" />13 <img id="fishImage" src="./assets/fish.png" width="100" height="100" />14 </div>815 <canvas id="canvas" width="400" height="300"></canvas>916 <script src="./main.js"></script>1017</body>2. style.cssを修正する
Section titled “2. style.cssを修正する”前のステップの style.css に、画像のスタイルを追加します。
変更点:
img要素のスタイルを追加して、画像のサイズを調整
style.css を変更する
Section titled “style.css を変更する”11canvas {22 border: 1px solid #CCC;33}4
5img {6 width: 200px;7 height: auto;8}3. main.jsを修正する
Section titled “3. main.jsを修正する”前のステップの main.js に、画像描画機能を追加します。
変更点:
dogImage、birdImage、fishImage要素を取得- 各画像のクリックイベントで
ctx.drawImage()を使用してキャンバスに描画
main.js を変更する
Section titled “main.js を変更する”11const canvas = document.getElementById('canvas');22const clearButton = document.getElementById('clearButton');33const colorInput = document.getElementById('colorInput');4const dogImage = document.getElementById('dogImage');5const birdImage = document.getElementById('birdImage');6const fishImage = document.getElementById('fishImage');47
58let drawing = false;3538 const ctx = canvas.getContext('2d');3639 ctx.clearRect(0, 0, canvas.width, canvas.height);3740});41
42birdImage.addEventListener('click', () => {43 const ctx = canvas.getContext('2d');44 ctx.drawImage(birdImage, 0, 0, 400, 300);45});46
47dogImage.addEventListener('click', () => {48 const ctx = canvas.getContext('2d');49 ctx.drawImage(dogImage, 0, 0, 400, 300);50});51
52fishImage.addEventListener('click', () => {53 const ctx = canvas.getContext('2d');54 ctx.drawImage(fishImage, 0, 0, 400, 300);55});- ブラウザでHTMLファイルを開く
- 画像(犬、鳥、魚)をクリックするとキャンバスに描画されることを確認
- 描画された画像の上から線を描けることを確認
- 消すボタンでキャンバスがクリアされることを確認

ctx.drawImage() について
Section titled “ctx.drawImage() について”ctx.drawImage() メソッドは、画像をキャンバスに描画するためのメソッドです。
ctx.drawImage(image, x, y, width, height);image: 描画する画像要素x,y: キャンバス上の描画開始位置width,height: 描画する画像のサイズ
ペンの太さを変更する機能を追加してください。
- https://developer.mozilla.org/ja/docs/Web/HTML/Reference/Elements/input/range
- https://developer.mozilla.org/ja/docs/Web/API/CanvasRenderingContext2D/lineWidth
クリックした場所に絵文字を配置する機能を追加してください。
- https://developer.mozilla.org/ja/docs/Web/HTML/Reference/Elements/select
- https://developer.mozilla.org/ja/docs/Web/API/CanvasRenderingContext2D/fillText