--- id: html-canvas title: "HTML Canvas" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["canvas element", "HTML5 Canvas", "", "2d context", "getContext"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.90 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["html", "web", "frontend", "canvas", "graphics", "html5", "w3schools"] raw_sources: ["https://www.w3schools.com/html/html5_canvas.asp"] applied_in: [] github_commit: "" --- # [[HTML Canvas]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) The HTML `` element is an empty container for graphics; all actual drawing β€” lines, shapes, text, gradients, images β€” is done at runtime with JavaScript via a 2D drawing context. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **`` is just a container** β€” it has no drawing ability of its own; JavaScript draws into it. [S1] - **It can draw** paths, boxes, circles, text, and images, and is supported by all major browsers. [S1] - **Always set `id`, `width`, and `height`** β€” the `id` lets scripts reference it; `width`/`height` set its drawable size. A border style makes the otherwise invisible canvas visible. [S1] - **The 2D context is the drawing surface** β€” obtain it with `canvas.getContext("2d")`; all drawing methods (`moveTo`, `lineTo`, `arc`, `fillText`, etc.) are called on that context object. [S1] - **Gradients** β€” created with `createLinearGradient(...)` or `createRadialGradient(...)`, then color stops are added and the gradient is assigned to `fillStyle`. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Setup pattern** β€” `getElementById` β†’ `getContext("2d")` β†’ draw. [S1] - **Path/line pattern** β€” `moveTo(x,y)` then `lineTo(x,y)` then `stroke()`. [S1] - **Circle pattern** β€” `beginPath()` β†’ `arc(x,y,r,0,2*Math.PI)` β†’ `stroke()`. [S1] - **Text pattern** β€” set `font`, then `fillText(...)` (filled) or `strokeText(...)` (outline). [S1] - **Gradient fill pattern** β€” create gradient β†’ `addColorStop()` β†’ assign to `fillStyle` β†’ `fillRect(...)`. [S1] - **Image pattern** β€” `drawImage(img, x, y)` paints an existing image element. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **What is HTML Canvas?** The HTML `` element is used to draw graphics, on the fly, via JavaScript. The `` element is only a container for graphics β€” you must use JavaScript to actually draw. Canvas can draw paths, boxes, circles, text, and add images, and it works in all major browsers. [S1] **The Canvas element** β€” a canvas is a rectangular area on an HTML page, specified with the `` element. Always specify an `id` (to refer to it in a script) and a `width` and `height` (to define its size). To add a border, use the `style` attribute: [S1] ```html ``` **Draw a Line** β€” [S1] ```html ``` **Draw a Circle** β€” [S1] ```html ``` **Draw Text** β€” set the font and use `fillText(text, x, y)`: [S1] ```html ``` **Stroke Text** β€” set the font and use `strokeText(text, x, y)` for outlined text: [S1] ```html ``` **Draw Linear Gradient** β€” gradients can fill rectangles, circles, lines, text, etc. Create a linear gradient, add color stops, set it as the fill style, and fill a rectangle: [S1] ```html ``` **Draw Radial Gradient** β€” [S1] ```html ``` **Draw Image** β€” use `drawImage(image, x, y)` to paint an existing image element onto the canvas: [S1] ```html ``` ## πŸ› οΈ 적용 사둀 (Applied in summary) The examples above are the canonical applied uses: drawing a diagonal line, a circle, filled and outlined text, linear and radial gradient-filled rectangles, and painting an existing image into the canvas. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Canvas setup + draw a line (HTML + JavaScript): ```html ``` Circle (JavaScript): ```javascript ctx.beginPath(); ctx.arc(95, 50, 40, 0, 2 * Math.PI); ctx.stroke(); ``` Linear gradient fill (JavaScript): ```javascript var grd = ctx.createLinearGradient(0, 0, 200, 0); grd.addColorStop(0, "red"); grd.addColorStop(1, "white"); ctx.fillStyle = grd; ctx.fillRect(10, 10, 150, 80); ``` ## βš–οΈ 비ꡐ 및 선택 κΈ°μ€€ (Comparison & decision criteria) The source frames Canvas in contrast to SVG: SVG describes 2D graphics in XML, while Canvas draws 2D graphics on the fly with JavaScript. In Canvas, once a graphic is drawn it is "forgotten" by the browser (immediate-mode, pixel/bitmap output), whereas in SVG each drawn shape is remembered as an object and re-rendered on change (retained-mode). Canvas is resolution dependent, has no built-in event handlers, has poor text rendering, but can save the result as a .png/.jpg image and is well suited to graphic-intensive games. See [[HTML SVG]] for the complementary trade-offs. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) No contradictions found in the source. Note the immediate-mode nature: Canvas does not retain a scene graph, so anything that must be interactive or re-rendered on data change has to be redrawn manually by your script. [S1] ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.90 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[HTML Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[HTML SVG]], [[HTML Graphics]], [[HTML Media]], [[HTML5]] - **μ°Έμ‘° λ§₯락:** Referenced when rendering dynamic, script-driven, pixel-based graphics such as charts, visualizations, or games. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” HTML Canvas β€” https://www.w3schools.com/html/html5_canvas.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "HTML Canvas" page (Astra wiki-curation, P-Reinforce v3.1 format).