File tree 3 files changed +27
-2
lines changed 3 files changed +27
-2
lines changed Original file line number Diff line number Diff line change 14
14
overflow : hidden;
15
15
background-color : grey;
16
16
}
17
+ h4 {
18
+ margin : 0 ;
19
+ text-align : center;
20
+ }
21
+ div {
22
+ display : inline-block;
23
+ }
17
24
# main-canvas {
25
+ background-color : white;
18
26
display : inline-block;
19
27
margin : 0 auto;
20
28
padding : 0 ;
21
29
}
30
+ # second-canvas ,
31
+ # html-text {
32
+ background-color : white;
33
+ }
22
34
</ style >
23
35
</ head >
24
36
< 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 >
26
47
< script src ="script.js "> </ script >
27
48
</ body >
28
49
</ html >
Original file line number Diff line number Diff line change 1
1
// Print text on canvas element using fillText.
2
+ var canvasText = document . getElementById ( "canvas-text" ) ;
2
3
var canvas = document . createElement ( "canvas" ) ;
3
4
var context = canvas . getContext ( "2d" ) ;
4
- document . body . appendChild ( canvas ) ;
5
+
6
+ canvas . setAttribute ( "id" , "second-canvas" ) ;
7
+ canvasText . appendChild ( canvas ) ;
5
8
6
9
context . font = "20px arial" ;
7
10
context . fillText ( "xyz" , 50 , 50 ) ;
8
11
9
12
// Print text directly to html document using write method.
13
+ var htmlText = document . getElementById ( "html-text" ) ;
10
14
document . body . style . fontFamily = "arial" ;
11
15
document . body . style . fontSize = "20px" ;
12
16
document . write ( "xyz" ) ;
You can’t perform that action at this time.
0 commit comments