Skip to content

Commit 297a628

Browse files
committed
Update canvas-print-text
1 parent abd6804 commit 297a628

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed
63.2 KB
Loading

canvas-print-text/index.html

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,36 @@
1414
overflow: hidden;
1515
background-color: grey;
1616
}
17+
h4 {
18+
margin: 0;
19+
text-align: center;
20+
}
21+
div {
22+
display: inline-block;
23+
}
1724
#main-canvas {
25+
background-color: white;
1826
display: inline-block;
1927
margin: 0 auto;
2028
padding: 0;
2129
}
30+
#second-canvas,
31+
#html-text {
32+
background-color: white;
33+
}
2234
</style>
2335
</head>
2436
<body>
25-
<canvas id="main-canvas"></canvas>
37+
<div>
38+
<h4>Empty canvas element</h4>
39+
<canvas id="main-canvas"></canvas>
40+
</div>
41+
<div id="canvas-text">
42+
<h4>Print text on canvas</h4>
43+
</div>
44+
<div id="html-text">
45+
<h4>Print text on html</h4>
46+
</div>
2647
<script src="script.js"></script>
2748
</body>
2849
</html>

canvas-print-text/script.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
// Print text on canvas element using fillText.
2+
var canvasText = document.getElementById("canvas-text");
23
var canvas = document.createElement("canvas");
34
var context = canvas.getContext("2d");
4-
document.body.appendChild(canvas);
5+
6+
canvas.setAttribute("id", "second-canvas");
7+
canvasText.appendChild(canvas);
58

69
context.font = "20px arial";
710
context.fillText("xyz", 50, 50);
811

912
// Print text directly to html document using write method.
13+
var htmlText = document.getElementById("html-text");
1014
document.body.style.fontFamily = "arial";
1115
document.body.style.fontSize = "20px";
1216
document.write("xyz");

0 commit comments

Comments
 (0)