-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2DTextExample04.htm
executable file
·46 lines (38 loc) · 1.6 KB
/
2DTextExample04.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<!DOCTYPE html>
<html>
<head>
<title>Canvas Text Example</title>
</head>
<body>
<canvas id="drawing" width="200" height="200">Your browser doesn't support the canvas tag.</canvas>
<script type="text/javascript">
window.onload = function(){
var drawing = document.getElementById("drawing");
//make sure <canvas> is completely supported
if (drawing.getContext){
var context = drawing.getContext("2d"),
fontSize = 50,
i, len;
//draw a red rectangle
context.strokeWidth = 1;
context.strokeStyle="#000000";
context.strokeRect(10, 10, 150, 30);
//default font setting
context.font = fontSize + "px Arial";
context.textBaseline = "top";
if (context.measureText){
while(context.measureText("Hello world!").width > 140){
fontSize--;
context.font = fontSize + "px Arial";
}
//known to work in Firefox 4, not Chrome
context.fillText("Hello world!", 10, 10, 50);
context.fillText("Font size is " + fontSize + "px", 10, 50);
} else {
alert("Your browser doesn't support the canvas text API.");
}
}
};
</script>
</body>
</html>