teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

コードを書き忘れてました

2018/09/06 23:19

投稿

Kaede0902
Kaede0902

スコア32

title CHANGED
File without changes
body CHANGED
@@ -12,4 +12,102 @@
12
12
 
13
13
  自分としてはMDNの書き方の方がかなり見やすく感じるのですが、
14
14
 
15
- windowLoadのイベント無しでbodyにfunctionも無しでscript直打ちしちゃって大丈夫なんでしょうか??
15
+ windowLoadのイベント無しでbodyにfunctionも無しでscript直打ちしちゃって大丈夫なんでしょうか??
16
+
17
+ MDNのサイトのコードです
18
+ ```javascript
19
+ <!DOCTYPE html>
20
+ <html>
21
+ <head>
22
+ <meta charset="utf-8" />
23
+ <title>Sample of MDN</title>
24
+ <style>* { padding: 0; margin: 0; }
25
+ canvas { background: #eee; display:
26
+ block; margin: 0 auto; }
27
+ </style>
28
+ </head>
29
+ <body>
30
+
31
+ <canvas id="myCanvas" width="480" height="320"></canvas>
32
+
33
+ <script>
34
+ var canvas = document.getElementById("myCanvas");
35
+ var ctx = canvas.getContext("2d");
36
+ ctx.beginPath();
37
+ ctx.rect(20, 40, 50, 50);
38
+ ctx.fillStyle = "#FF0000";
39
+ ctx.fill();
40
+ ctx.closePath();
41
+ ctx.beginPath();
42
+ ctx.arc(240, 160, 20, 0, Math.PI*2, false);
43
+ ctx.fillStyle = "green";
44
+ ctx.fill();
45
+ ctx.closePath();
46
+ ctx.beginPath();
47
+ ctx.rect(160, 10, 100, 40);
48
+ ctx.strokeStyle = "rgba(0, 0, 255, 0.5)";
49
+ ctx.stroke();
50
+ ctx.closePath();
51
+ </script>
52
+
53
+ </body>
54
+ </html>
55
+
56
+ ```
57
+
58
+ Oreillyを参考にしたwindowLoadを使う書き方です
59
+
60
+ ```javascript
61
+ <!DOCTYPE html>
62
+ <html lang="en">
63
+ <head>
64
+ <meta charset="UTF-8">
65
+
66
+ <title>EX2 seriesMDN</title>
67
+
68
+ <style>* { padding: 0; margin: 0; }
69
+ canvas { background: #eee; display:
70
+ block; margin: 0 auto; }
71
+ </style>
72
+
73
+ <script >
74
+ window.addEventListener('load', eventWindowLoaded, false);
75
+
76
+ function eventWindowLoaded() {
77
+ canvasApp();
78
+ }
79
+ function canvasApp() {
80
+ var theCanvas = document.getElementById("canvas");
81
+ var ctx = theCanvas.getContext("2d");
82
+ drawScreen();
83
+ function drawScreen() {
84
+ ctx.beginPath();
85
+ ctx.rect(20, 40, 50, 50);
86
+ ctx.fillStyle = "#FF0000";
87
+ ctx.fill();
88
+ ctx.closePath();
89
+
90
+ ctx.beginPath();
91
+ ctx.arc(240, 160, 20, 0, Math.PI*2, false);
92
+ ctx.fillStyle = "green";
93
+ ctx.fill();
94
+ ctx.closePath();
95
+
96
+ ctx.beginPath();
97
+ ctx.rect(160, 10, 100, 40);
98
+ ctx.strokeStyle = "rgba(0, 0, 255, 0.5)";
99
+ ctx.stroke();
100
+ ctx.closePath();
101
+
102
+ }
103
+ }
104
+
105
+
106
+
107
+ </script>
108
+ </head>
109
+ <body>
110
+ <canvas id="canvas" width="480" height="320">No canvas sup</canvas>
111
+ </body>
112
+ </html>
113
+ ```