質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
canvas

HTML5の<canvas>要素用のタグです。CanvasはHTML5から導入された、二次元の図形描写が可能な要素です。

HTML5

HTML5 (Hyper Text Markup Language、バージョン 5)は、マークアップ言語であるHTMLの第5版です。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

Q&A

解決済

2回答

5083閲覧

JavaScript の Unexpected end of input について

退会済みユーザー

退会済みユーザー

総合スコア0

canvas

HTML5の<canvas>要素用のタグです。CanvasはHTML5から導入された、二次元の図形描写が可能な要素です。

HTML5

HTML5 (Hyper Text Markup Language、バージョン 5)は、マークアップ言語であるHTMLの第5版です。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

0グッド

0クリップ

投稿2018/04/04 10:16

JavaScript初心者です。
以下のコードが、Uncaught SyntaxError: Unexpected end of input をはいて動きません。(最終行の animate(); です)
色々試したのですが、原因が分からなくなりハマっています。初歩的な質問で申し訳ないのですが、どこに問題があるのかご指摘いただけないでしょうか。

以下にコードを載せます。
一応全コードを載せますが、おそらく問題がある個所は最後の数行だと思われます。

canvas 上で複数の円を動かして、カーソルが近い円を大きくするアニメーションを実装しようとしています。

javascript

1const canvas = document.querySelector("canvas"); 2const mouse = { 3 x : undefined, 4 y : undefined, 5}; 6const number = 30 7canvas.width = window.innerWidth; 8canvas.height = window.innerHeight; 9 10const c = canvas.getContext("2d"); 11const r = 8; 12 13function cirlce(x, y, radius, dx_veloOFx, dy_veloOFy) { 14 this.x = x; 15 this.y = y; 16 this.r = radius; 17 this.dx = dx_veloOFx; 18 this.dy = dy_veloOFy; 19 20 this.draw = function(){ 21 c.beginPath(); 22 c.arc(this.x, this.y, r, 0, Math.PI*2, false); 23 c.fill(); 24 }; 25 26 this.motion = function(){ 27 if (this.x + this.r >= innerWidth || this.x - this.r <= 0){ 28 this.dx = -this.dx; }; 29 if (this.y + this.r >= innerHeight || this.y - this.r <= 0){ 30 this.dy = -this.dy; }; 31 this.x += this.dx; 32 this.y += this.dy; 33 }; 34 35 this.judge = function(){ 36 if( (this.x - mouse.x)^2 + (this.y - mouse.y)^2 <= 1000){ 37 this.r = 40; 38 }; 39 if( (this.x - mouse.x)^2 + (this.y - mouse.y)^2 > 1000){ 40 this.r = 8; 41 }; 42}; 43 44function animate() { 45 requestAnimationFrame(animate); 46 c.clearRect(0, 0, innerWidth, innerHeight); 47 48 for(let i = 0; i<number; i++){ 49 cirArray[i].draw(); 50 cirArray[i].motion(); 51 cirArray[i].judge(); 52 }; 53}; 54 55const cirArray = []; 56 57for (let i = 0; i< number; i++){ 58 let x = Math.random()*innerWidth, 59 y = Math.random()*innerHeight, 60 dx = (Math.random()-0.5)*8, 61 dy = (Math.random()-0.5)*8, 62 63 hako = new cirlce(x, y, r, dx, dy); 64 cirArray.push(hako); 65}; 66 67window.addEevntLisnter('mousemove', 68 function(event){ 69 mouse.x = event.x; 70 mouse.y = event.y; 71 }) 72 73animate();

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

どこからコードを写経したのかは知りませんが, アルゴリズムそのものに問題はなく, 単にあなたのコードの写し間違えのようです.

  • {}の対応に一箇所記述漏れがあります.
  • メソッド名に間違いがあります. addEevntLisnterでなく正しくはaddEventListenerです.

これで一応アニメーションし始めますが、意図した動作となっているかはこちらでは確認のしようがありません(多分まだマウス位置の判定処理に問題があるはず)ので、今一度コードの内容を目を皿のようにして確認して下さい.

JavaScript

1const canvas = document.querySelector("canvas"); 2const mouse = { 3 x : undefined, 4 y : undefined, 5}; 6const number = 30 7canvas.width = window.innerWidth; 8canvas.height = window.innerHeight; 9 10const c = canvas.getContext("2d"); 11const r = 8; 12 13function cirlce(x, y, radius, dx_veloOFx, dy_veloOFy) { 14 this.x = x; 15 this.y = y; 16 this.r = radius; 17 this.dx = dx_veloOFx; 18 this.dy = dy_veloOFy; 19 20 this.draw = function(){ 21 c.beginPath(); 22 c.arc(this.x, this.y, r, 0, Math.PI*2, false); 23 c.fill(); 24 }; 25 26 this.motion = function(){ 27 if (this.x + this.r >= innerWidth || this.x - this.r <= 0){ 28 this.dx = -this.dx; }; 29 if (this.y + this.r >= innerHeight || this.y - this.r <= 0){ 30 this.dy = -this.dy; }; 31 this.x += this.dx; 32 this.y += this.dy; 33 }; 34 35 this.judge = function(){ 36 if( (this.x - mouse.x)^2 + (this.y - mouse.y)^2 <= 1000){ 37 this.r = 40; 38 }; 39 if( (this.x - mouse.x)^2 + (this.y - mouse.y)^2 > 1000){ 40 this.r = 8; 41 }; 42 }//←ここにカッコが足りない! 43}; 44 45function animate() { 46 requestAnimationFrame(animate); 47 c.clearRect(0, 0, innerWidth, innerHeight); 48 49 for(let i = 0; i<number; i++){ 50 cirArray[i].draw(); 51 cirArray[i].motion(); 52 cirArray[i].judge(); 53 }; 54}; 55 56const cirArray = []; 57 58for (let i = 0; i< number; i++){ 59 let x = Math.random()*innerWidth, 60 y = Math.random()*innerHeight, 61 dx = (Math.random()-0.5)*8, 62 dy = (Math.random()-0.5)*8, 63 64 hako = new cirlce(x, y, r, dx, dy); 65 cirArray.push(hako); 66}; 67 68//window.addEevntLisnter('mousemove', 69window.addEventListener('mousemove', 70 function(event){ 71 mouse.x = event.x; 72 mouse.y = event.y; 73 }) 74 75animate();

投稿2018/04/04 10:31

defghi1977

総合スコア4756

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2018/04/04 10:57

ありがとうございます。解決しました。 まさに指摘していただいた箇所の "}"漏れと、メソッド名のタイポでした。 動かしてみたのですが意図した挙動ではないので改善が必要ですね… もう少し取り組んでみます。 このような質問に答えてくださりありがとうございました!
guest

0

addEevntLisnterではなくaddEventListenerです。

あと、this.judge = function(){ の閉じかっこがないですね。

投稿2018/04/04 10:28

kszk311

総合スコア3404

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2018/04/04 10:58

ありがとうございます。 一つの名前で二か所もタイポしてますね…
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問