--- 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).