| 情報学部 | 菅沼ホーム | JavaScript 目次 | 索引 |
文字を<B>太く</B>表示する
文字の背景を<SPAN STYLE="background-color: pink">ピンク</SPAN>にする
01 <!DOCTYPE HTML> 02 <HTML> 03 <HEAD> 04 <TITLE>HTML を理解しよう!</TITLE> 05 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8"> 06 <META NAME=viewport CONTENT="width=device-width, initial-scale=1"> 07 </HEAD> 08 <BODY> 09 本文の記述 10 </BODY> 11 </HTML>
<SCRIPT TYPE="text/javascript"> <!-- ここに,JavaScript のプログラム //--> </SCRIPT>
01 <!DOCTYPE HTML>
02 <HTML>
03 <HEAD>
04 <TITLE>文書内記述</TITLE>
05 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
06 <META NAME=viewport CONTENT="width=device-width, initial-scale=1">
07 <LINK REL="stylesheet" TYPE="text/css" HREF="../../master.css">
08 </HEAD>
09 <BODY CLASS="white">
10 <H1 CLASS="center">文書内記述</H1>
11 <SCRIPT TYPE="text/javascript">
12 document.write("JavaScript によって出力<BR>\n");
13 alert("文書内記述");
14 let w = window.open("", "", "width=300, height=100");
15 w.document.open(); // 以下,生成された Window 内に記述される
16 w.document.write('<HTML>\n');
17 w.document.write(' <HEAD>\n');
18 w.document.write(' <TITLE>新しい Window</TITLE>\n');
19 w.document.write(' </HEAD>\n');
20 w.document.write(' <BODY>\n');
21 w.document.write(' 新しい Window の生成も可能<BR>\n');
22 w.document.write(' </BODY>\n');
23 w.document.write('</HTML>\n');
24 w.document.close();
25 </SCRIPT>
26 </BODY>
27 </HTML>
body.white { background-color: white; color: black; font-size: 130% }
H1.center { font-style: italic; text-align: center }
let x1 = 12345; let x2; var y1 = 12.5; var y1; z1 = 12; z1;
01 <!DOCTYPE HTML>
02 <HTML>
03 <HEAD>
04 <TITLE>文書内記述(関数)</TITLE>
05 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
06 <META NAME=viewport CONTENT="width=device-width, initial-scale=1">
07 <LINK REL="stylesheet" TYPE="text/css" HREF="../../master.css">
08 <SCRIPT TYPE="text/javascript">
09 function func()
10 {
11 alert("文書内記述(関数)");
12 }
13 </SCRIPT>
14 </HEAD>
15 <BODY CLASS="white">
16 <H1 CLASS="center">文書内記述(関数)(<SPAN onClick="func()">ここをクリック</SPAN>)</H1>
17 </BODY>
18 </HTML>
function func()
{
alert("外部ファイル");
}
01 <!DOCTYPE HTML> 02 <HTML> 03 <HEAD> 04 <TITLE>外部ファイル</TITLE> 05 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8"> 06 <META NAME=viewport CONTENT="width=device-width, initial-scale=1"> 07 <LINK REL="stylesheet" TYPE="text/css" HREF="../../master.css"> 08 <SCRIPT TYPE="text/javascript" SRC="control.js"></SCRIPT> 09 </HEAD> 10 <BODY CLASS="white"> 11 <H1 CLASS="center">外部ファイル(<SPAN onClick="func()">ここをクリック</SPAN>)</H1> 12 </BODY> 13 </HTML>
01 <!DOCTYPE HTML>
02 <HTML>
03 <HEAD>
04 <TITLE>要素内記述</TITLE>
05 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
06 <META NAME=viewport CONTENT="width=device-width, initial-scale=1">
07 <LINK REL="stylesheet" TYPE="text/css" HREF="../../master.css">
08 </HEAD>
09 <BODY CLASS="white">
10 <H1 CLASS="center">要素内記述</H1>
11 SPAN 要素 <SPAN onClick="JavaScript: alert('alert 1');alert('alert 2')">ここをクリック</SPAN><BR><BR>
12 A 要素 <A HREF="JavaScript: alert('alert 1');alert('alert 2')">ここをクリック</A>
13 </BODY>
14 </HTML>
01 <!DOCTYPE HTML> 02 <HTML> 03 <HEAD> 04 <TITLE>アニメーション1</TITLE> 05 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8"> 06 <META NAME=viewport CONTENT="width=device-width, initial-scale=1"> 07 <LINK REL="stylesheet" TYPE="text/css" HREF="../../../master.css"> 08 <SCRIPT TYPE="text/javascript" SRC="animation1.js"></SCRIPT> 09 </HEAD> 10 <BODY CLASS="eeffee" onLoad="start()"> 11 <H1>アニメーション( setInterval,CANVAS )</H1> 12 <CANVAS ID="canvas_e" STYLE="background-color: #ffffff;" WIDTH="250" HEIGHT="150"></CANVAS> 13 </BODY> 14 </HTML>
01 count1 = 0; // 何個の円を描いたかを示す
02 count2 = 0; // 10 個の円の描画を何回行ったかを示す
03 r = 10; // 円の半径の初期値
04 canvas = null; // キャンバス
05 ctx = null; // コンテキスト
06 timerID = -1; // タイマー
07 //
08 // 開始
09 //
10 function start()
11 {
12 canvas = document.getElementById('canvas_e'); // キャンバス要素の取得
13 ctx = canvas.getContext('2d'); // キャンバスからコンテキストを取得
14 timerID = setInterval('draw()',100);
15 }
16 //
17 // 描画
18 //
19 function draw() {
20 count1++;
21 if (count1 > 10) {
22 count2++;
23 if (count2 > 4)
24 stop();
25 else {
26 r = 10;
27 count1 = 1;
28 ctx.clearRect(0, 0, canvas.width, canvas.height);
29 }
30 }
31 ctx.beginPath();
32 ctx.arc(0, 0, r, Math.PI*1.5, Math.PI*2, true);
33 ctx.stroke();
34 r = 1.5 * r; // r *= 1.5; でも可
35 }
36 //
37 // 停止
38 //
39 function stop()
40 {
41 clearInterval(timerID);
42 ctx.clearRect(0, 0, canvas.width, canvas.height);
43 timerID = -1;
44 }
function 関数名 (引数, ・・・) { 処理 }
count1 += 1; count1 = count1 + 1;
if (論理式) { 文1(複数の文も可) } else { 文2(複数の文も可) } ・・・・・・
> より大きい a > b 式 a の値が式 b の値より大きいとき真 < より小さい a < b 式 a の値が式 b の値より小さいとき真 >= 以上 a >= b 式 a の値が式 b の値以上のとき真 <= 以下 a <= b 式 a の値が式 b の値以下のとき真
== 等しい a == b 式 a の値と式 b の値が等しいとき真 === 等しい a == b 式 a と式 b の型と値が等しいとき真 != 等しくない a != b 式 a の値と式 b の値が等しくないとき真 !== 等しくない a != b 式 a と式 b の型か値が等しくないとき真
|| 論理和 x || y 式 x が真か,または,式 y が真のとき真 && 論理積 x && y 式 x が真で,かつ,式 y が真のとき真 ! 否定 ! x 式 x が偽のとき真
01 <!DOCTYPE HTML> 02 <HTML> 03 <HEAD> 04 <TITLE>アニメーション2</TITLE> 05 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8"> 06 <META NAME=viewport CONTENT="width=device-width, initial-scale=1"> 07 <LINK REL="stylesheet" TYPE="text/css" HREF="../../../master.css"> 08 <SCRIPT TYPE="text/javascript" SRC="animation2.js"></SCRIPT> 09 </HEAD> 10 <BODY CLASS="eeffee" onLoad="start()"> 11 <H1>アニメーション( setInterval,DIV )</H1> 12 <DIV ID="canvas_e"></DIV> 13 </BODY> 14 </HTML>
01 count1 = 0; // 何個の円を描いたかを示す
02 count2 = 0; // 10 個の円の描画を何回行ったかを示す
03 r = 10; // 円の半径の初期値
04 canvas = null; // キャンバス
05 ctx = null; // コンテキスト
06 timerID = -1; // タイマー
07 //
08 // 開始
09 //
10 function start()
11 {
12 let area = document.getElementById("canvas_e"); // キャンバスを挿入する場所
13 canvas = document.createElement("canvas"); // キャンバス要素を生成
14 canvas.style.backgroundColor = "#ffffff"; // キャンバスの背景色
15 canvas.width = 250; // キャンバス要素の幅
16 canvas.height = 150; // キャンバス要素の高さ
17 area.appendChild(canvas); // キャンバス要素を追加
18 ctx = canvas.getContext('2d'); // キャンバスからコンテキストを取得
19 timerID = setInterval('draw()',100);
20 }
21 //
22 // 描画
23 //
24 function draw() {
25 count1++;
26 if (count1 > 10) {
27 count2++;
28 if (count2 > 4)
29 stop();
30 else {
31 r = 10;
32 count1 = 1;
33 ctx.clearRect(0, 0, canvas.width, canvas.height);
34 }
35 }
36 ctx.beginPath();
37 ctx.arc(0, 0, r, Math.PI*1.5, Math.PI*2, true);
38 ctx.stroke();
39 r = 1.5 * r;
40 }
41 //
42 // 停止
43 //
44 function stop()
45 {
46 clearInterval(timerID);
47 ctx.clearRect(0, 0, canvas.width, canvas.height);
48 timerID = -1;
49 }
01 count1 = 0; // 何個の円を描いたかを示す
02 count2 = 0; // 10 個の円の描画を何回行ったかを示す
03 r = 10; // 円の半径の初期値
04 canvas = null; // キャンバス
05 ctx = null; // コンテキスト
06 timerID = -1; // タイマー
07 //
08 // 開始
09 //
10 function start()
11 {
12 canvas = document.getElementById('canvas_e'); // キャンバス要素の取得
13 ctx = canvas.getContext('2d'); // キャンバスからコンテキストを取得
14 timerID = setTimeout("draw()", 100);
15 }
16 //
17 // 描画
18 //
19 function draw() {
20 count1++;
21 if (count1 > 10) {
22 count2++;
23 if (count2 > 4)
24 stop();
25 else {
26 r = 10;
27 count1 = 1;
28 ctx.clearRect(0, 0, canvas.width, canvas.height);
29 }
30 }
31 ctx.beginPath();
32 ctx.arc(0, 0, r, Math.PI*1.5, Math.PI*2, true);
33 ctx.stroke();
34 r = 1.5 * r;
35 clearTimeout(timerID);
36 timerID = setTimeout("draw()", 100);
37 }
38 //
39 // 停止
40 //
41 function stop()
42 {
43 clearTimeout(timerID);
44 ctx.clearRect(0, 0, canvas.width, canvas.height);
45 timerID = -1;
46 }
01 <!DOCTYPE HTML> 02 <HTML> 03 <HEAD> 04 <TITLE>イベント1</TITLE> 05 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8"> 06 <META NAME=viewport CONTENT="width=device-width, initial-scale=1"> 07 <LINK REL="stylesheet" TYPE="text/css" HREF="../../../master.css"> 08 <SCRIPT TYPE="text/javascript" SRC="event1.js"></SCRIPT> 09 </HEAD> 10 <BODY CLASS="eeffee" onLoad="animation()"> 11 <H1>イベント処理(その1)</H1> 12 <BUTTON STYLE="font-size:90%" onClick="start()">開始</BUTTON> <BUTTON STYLE="font-size:90%" onClick="stop()">停止</BUTTON><BR><BR> 13 <CANVAS ID="canvas_e" STYLE="background-color: #ffffff;" WIDTH="400" HEIGHT="300"></CANVAS> 14 </BODY> 15 </HTML>
01 x = 0; // 画像(円の中心)の位置( x 軸)
02 y = 0; // 画像(円の中心)の位置( y 軸)
03 v = 0; // 画像の移動速度
04 canvas = null; // キャンバス
05 ctx = null; // コンテキスト
06 img = null; // 画像
07 timerID = -1; // タイマー
08 //
09 // 開始
10 //
11 function animation()
12 {
13 canvas = document.getElementById('canvas_e'); // キャンバス要素の取得
14 ctx = canvas.getContext('2d'); // キャンバスからコンテキストを取得
15 img = new Image();
16 img.src = "ball.gif";
17 x = 0; // 円の中心の座標( x )
18 y = canvas.height / 2; // 円の中心の座標( y )
19 timerID = setInterval('draw()',100);
20 }
21 //
22 // 描画
23 //
24 function draw() {
25 x += v;
26 ctx.clearRect(0, 0, canvas.width, canvas.height);
27 ctx.drawImage(img, x-40, y-40);
28 }
29 //
30 // 開始
31 //
32 function start()
33 {
34 v = 5;
35 }
36 //
37 // 停止
38 //
39 function stop()
40 {
41 v = 0;
42 }
01 x = 0; // 画像(円の中心)の位置( x 軸)
02 y = 0; // 画像(円の中心)の位置( y 軸)
03 v = 0; // 画像の移動速度
04 canvas = null; // キャンバス
05 ctx = null; // コンテキスト
06 img = null; // 画像
07 timerID = -1; // タイマー
08 x_base = -1; // Window上に置けるキャンバスの位置(左上のx座標)
09 y_base = -1; // Window上に置けるキャンバスの位置(左上のy座標)
10 //
11 // 開始
12 //
13 function animation()
14 {
15 canvas = document.getElementById('canvas_e'); // キャンバス要素の取得
16 ctx = canvas.getContext('2d'); // キャンバスからコンテキストを取得
17 img = new Image();
18 img.src = "ball.gif";
19 x_base = canvas.offsetLeft; // キャンバスの左上のx座標
20 y_base = canvas.offsetTop; // キャンバスの左上のy座標
21 x = 0; // 円の中心の座標( x )
22 y = canvas.height / 2; // 円の中心の座標( y )
23 canvas.addEventListener("click", Click, false); // クリックイベント
24 timerID = setInterval('draw()',100);
25 }
26 //
27 // 描画
28 //
29 function draw() {
30 x += v;
31 ctx.clearRect(0, 0, canvas.width, canvas.height);
32 ctx.drawImage(img, x-40, y-40);
33 }
34 //
35 // 円がクリックされた時の処理
36 //
37 function Click(event)
38 {
39 let x_now = event.pageX - x_base;
40 let y_now = event.pageY - y_base;
41 let x1 = x - x_now;
42 let y1 = y - y_now;
43 let r = Math.sqrt(x1 * x1 + y1 * y1);
44 if (r < 40) {
45 if (v > 0)
46 v = 0;
47 else
48 v = 5;
49 }
50 }
x = 0; // 画像(円の中心)の位置( x 軸)
y = 0; // 画像(円の中心)の位置( y 軸)
v = 0; // 画像の移動速度
canvas = null; // キャンバス
ctx = null; // コンテキスト
img = null; // 画像
timerID = -1; // タイマー
x_base = -1; // Window上に置けるキャンバスの位置(左上のx座標)
y_base = -1; // Window上に置けるキャンバスの位置(左上のy座標)
//
// 開始
//
function animation()
{
canvas = document.getElementById('canvas_e'); // キャンバス要素の取得
ctx = canvas.getContext('2d'); // キャンバスからコンテキストを取得
img = new Image();
img.src = "ball.gif";
x_base = canvas.offsetLeft; // キャンバスの左上のx座標
y_base = canvas.offsetTop; // キャンバスの左上のy座標
x = 0; // 円の中心の座標( x )
y = canvas.height / 2; // 円の中心の座標( y )
// クリックイベント
canvas.addEventListener("click", function(event) {
let x_now = event.pageX - x_base;
let y_now = event.pageY - y_base;
let x1 = x - x_now;
let y1 = y - y_now;
let r = Math.sqrt(x1 * x1 + y1 * y1);
if (r < 40) {
if (v > 0)
v = 0;
else
v = 5;
}
}, false);
timerID = setInterval('draw()',100);
}
//
// 描画
//
function draw() {
x += v;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, x-40, y-40);
}
| 情報学部 | 菅沼ホーム | JavaScript 目次 | 索引 |