Files
2nd/10_Wiki/Topic_HTML/HTML_Canvas.md
T
koriweb 9609c04755 docs(10_Wiki): W3Schools 위키화 — HTML/CSS/JavaScript(core)
W3Schools 튜토리얼을 P-Reinforce v3.1 포맷으로 위키화(영어 본문, 한/영 섹션 헤더).
- Topic_HTML: 59문서 (튜토리얼+예제, 레퍼런스/메타 제외)
- Topic_CSS: 190문서 (메인 + Advanced/Flexbox/Grid/RWD 전체)
- Topic_JavaScript: 120문서 (코어 언어; Temporal/DOM상세/BOM/WebAPI/AJAX/jQuery/Graphics 등은 후속)
각 폴더 00_INDEX.md(MOC) 포함. 코드 verbatim, 미확인분은 "Not found in source" 표기.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 19:21:18 +09:00

7.6 KiB

id, title, category, status, verification_status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, created_at, updated_at, review_reason, merge_history, tags, raw_sources, applied_in, github_commit
id title category status verification_status canonical_id aliases duplicate_of source_trust_level confidence_score created_at updated_at review_reason merge_history tags raw_sources applied_in github_commit
html-canvas HTML Canvas Frontend draft conceptual
canvas element
HTML5 Canvas
<canvas>
2d context
getContext
B 0.90 2026-06-23 2026-06-23
html
web
frontend
canvas
graphics
html5
w3schools
https://www.w3schools.com/html/html5_canvas.asp

HTML Canvas

🎯 한 줄 통찰 (One-line insight)

The HTML <canvas> 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)

  • <canvas> 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 patterngetElementByIdgetContext("2d") → draw. [S1]
  • Path/line patternmoveTo(x,y) then lineTo(x,y) then stroke(). [S1]
  • Circle patternbeginPath()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 fillStylefillRect(...). [S1]
  • Image patterndrawImage(img, x, y) paints an existing image element. [S1]

📖 세부 내용 (Details)

What is HTML Canvas? The HTML <canvas> element is used to draw graphics, on the fly, via JavaScript. The <canvas> 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 <canvas> 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]

<canvas id="myCanvas" width="200" height="100" style="border:1px solid #000000;">
</canvas>

Draw a Line — [S1]

<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.moveTo(0, 0);
ctx.lineTo(200, 100);
ctx.stroke();
</script>

Draw a Circle — [S1]

<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.arc(95, 50, 40, 0, 2 * Math.PI);
ctx.stroke();
</script>

Draw Text — set the font and use fillText(text, x, y): [S1]

<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.font = "30px Arial";
ctx.fillText("Hello World", 10, 50);
</script>

Stroke Text — set the font and use strokeText(text, x, y) for outlined text: [S1]

<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.font = "30px Arial";
ctx.strokeText("Hello World", 10, 50);
</script>

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]

<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

// Create gradient
var grd = ctx.createLinearGradient(0, 0, 200, 0);
grd.addColorStop(0, "red");
grd.addColorStop(1, "white");

// Fill with gradient
ctx.fillStyle = grd;
ctx.fillRect(10, 10, 150, 80);
</script>

Draw Radial Gradient — [S1]

<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

// Create gradient
var grd = ctx.createRadialGradient(75, 50, 5, 90, 60, 100);
grd.addColorStop(0, "red");
grd.addColorStop(1, "white");

// Fill with gradient
ctx.fillStyle = grd;
ctx.fillRect(10, 10, 150, 80);
</script>

Draw Image — use drawImage(image, x, y) to paint an existing image element onto the canvas: [S1]

<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var img = document.getElementById("scream");
ctx.drawImage(img, 10, 10);
</script>

🛠️ 적용 사례 (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):

<canvas id="myCanvas" width="200" height="100" style="border:1px solid #000000;"></canvas>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.moveTo(0, 0);
ctx.lineTo(200, 100);
ctx.stroke();
</script>

Circle (JavaScript):

ctx.beginPath();
ctx.arc(95, 50, 40, 0, 2 * Math.PI);
ctx.stroke();

Linear gradient fill (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)

📚 출처 (Sources)

📝 변경 이력 (Change history)

  • 2026-06-23: Initial draft synthesized from the W3Schools "HTML Canvas" page (Astra wiki-curation, P-Reinforce v3.1 format).