こんにちは。KOUKIです。
本記事は、Udemyの「50 Projects In 50 Days – HTML, CSS & JavaScript」で学習したことを載せています。
<目次>
実装するもの
今回は、CSSでDrawingアプリケーションのスタイリング方法を学びました。
demoは「こちら」で確認できます。
ワークスペース
必要なファイルは、以下の通りです。
1 2 3 4 5 6 |
$ tree . ├── index.html ├── script.js └── style.css |
JavaScript版
HTML & CSS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" href="style.css" /> <title>Drawing App</title> </head> <body> <canvas id="canvas" width="800" height="700"></canvas> <div class="toolbox"> <button id="decrease">-</button> <span id="size">10</span> <button id="increase">+</button> <input type="color" id="color"> <button id="clear">X</button> </div> <script src="script.js"></script> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
// 要素を取得する const canvas = document.getElementById('canvas'); const increaseBtn = document.getElementById('increase'); const decreaseBtn = document.getElementById('decrease'); const sizeEL = document.getElementById('size'); const colorEl = document.getElementById('color'); const clearEl = document.getElementById('clear'); const ctx = canvas.getContext('2d'); // 描画条件 let size = 20 let color = 'balck' let isPressed = false let x let y function drawCircle(x, y) { // 描画開始 ctx.beginPath() // 円を描く ctx.arc(x, y, size, 0, Math.PI * 2) // 色を指定 ctx.fillStyle = color // 描画 ctx.fill() } function drawLine(x1, y1, x2, y2) { ctx.beginPath() ctx.moveTo(x1, y1) ctx.lineTo(x2, y2) ctx.strokeStyle = color ctx.lineWidth = size * 2 ctx.stroke() } // mouesdownの登録 canvas.addEventListener('mousedown', (e) => { // ポイントを当てていることを示す isPressed = true // マウスがポイントされた位置を取得 x = e.offsetX y = e.offsetY }) // mouseupの登録 canvas.addEventListener('mouseup', (e) => { // ポイントを外したことを示す isPressed = false // マウスのポインタ位置を初期化 x = undefined y = undefined }) // mousemoveの登録 canvas.addEventListener('mousemove', (e) => { if(isPressed) { // mousedown時に描画開始位置を取得するので // 最後にカーソルを当てたところを終了位置として // 格納する const x2 = e.offsetX const y2 = e.offsetY // 円と線を組み合わせるのがミソ drawCircle(x2, y2) drawLine(x, y, x2, y2) x = x2 y = y2 } }) // 色の変更 colorEl.addEventListener('change', (e) => color = e.target.value) // 描画サイズの変更 increaseBtn.addEventListener('click', () => { size += 5 // 最大50まで if (size > 50) { size = 50 } updateSizeOnScreen() }) decreaseBtn.addEventListener('click', () => { size -= 5 // 最大50まで if (size < 0) { size = 5 } updateSizeOnScreen() }) function updateSizeOnScreen() { sizeEL.innerHTML = size } // 描画をクリアする clearEl.addEventListener('click', () => ctx.clearRect(0,0, canvas.width, canvas.height)) |

スタイリング
これからCSSでスタイリングをしていきます。項目のbodyやcanvasは、HTML要素です。
全体の設定
1 2 3 4 5 6 7 8 |
/* フォント */ @import url("https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap"); * { /* ボックスサイズを算出 paddingとborderをwidthとheightに含める */ box-sizing: border-box; } |
bodyの設定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
body { background-color: #f5f5f5; font-family: "Roboto", sans-serif; /* flexアイテムにする */ display: flex; /* 積み重ねるように配置 */ flex-direction: column; /* flex重点にアイテムを配置 */ align-items: center; /* flex横軸中央にアイテムを配置 */ justify-content: center; height: 100vh; margin: 0; } |

canvasの設定
1 2 3 |
canvas { border: 2px solid steelblue; } |

toolboxの設定
1 2 3 4 5 6 7 |
.toolbox { background-color: steelblue; border: 1px solid slateblue; display: flex; width: 804px; padding: 1rem; } |

toolbox子要素の設定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/* toolbox子要素 */ .toolbox > * { background: #fff; border: none; width: 50px; height: 50px; margin: 0.25rem; padding: 0.25rem; cursor: pointer; font-size: 2rem; /* 所属する要素依存の幅にする */ display: inline-flex; align-items: center; justify-content: center; } |

toolbox末要素の設定
1 2 3 |
.toolbox > *:last-child { margin-left: auto; } |

これで、完成です。
おわりに
CSSは、デザイン力が命だと思います。しかし、デザイン力はいかにして高められるのか?日々葛藤していますね。
仕事で管理画面の開発もしているので、少しずつ学んだ知識を活用していこうと思っています。
それでは、また!
CSSまとめ
CSSソースコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
/* フォント */ @import url("https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap"); * { /* ボックスサイズを算出 paddingとborderをwidthとheightに含める */ box-sizing: border-box; } body { background-color: #f5f5f5; font-family: "Roboto", sans-serif; /* flexアイテムにする */ display: flex; /* 積み重ねるように配置 */ flex-direction: column; /* flex重点にアイテムを配置 */ align-items: center; /* flex横軸中央にアイテムを配置 */ justify-content: center; height: 100vh; margin: 0; } canvas { border: 2px solid steelblue; } .toolbox { background-color: steelblue; border: 1px solid slateblue; display: flex; width: 804px; padding: 1rem; } /* toolbox子要素 */ .toolbox > * { background: #fff; border: none; width: 50px; height: 50px; margin: 0.25rem; padding: 0.25rem; cursor: pointer; font-size: 2rem; /* 所属する要素依存の幅にする */ display: inline-flex; align-items: center; justify-content: center; } .toolbox > *:last-child { margin-left: auto; } |
コメントを残す
コメントを投稿するにはログインしてください。