More Related Content
数学的基礎から学ぶ Deep Learning (with Python) Vol. 12 Python で画像処理をやってみよう! -SIFT 第7回- 数学的基礎から学ぶ Deep Learning (with Python) Vol. 9 Python で画像処理をやってみよう! -SIFT 第6回- 数学的基礎から学ぶ Deep Learning (with Python) Vol. 8 数学的基礎から学ぶ Deep Learning (with Python) Vol. 7 Python で画像処理をやってみよう! -SIFT 第5回- More from Project Samurai (20)
数学的基礎から学ぶ Deep Learning (with Python) Vol. 6 Make your Artificial Intelligence 数学的基礎から学ぶ Deep Learning (with Python) Vol. 4 数学的基礎から学ぶ Deep Learning (with Python) Vol. 3 数学的基礎から学ぶ Deep Learning (with Python) Vol. 2 数学的基礎から学ぶ Deep Learning (with Python) Vol. 1 Python で画像処理をやってみよう!第11回 - SIFT Vol.1 キーポイント候補 - Instagram API を使ってウェブアプリを作ろう! Pythonで画像処理をやってみよう!第8回 - Scale-space 第7回 - JavaScript でパックマン!第7回 (一旦最終回) Pythonで画像処理をやってみよう!第7回 - Scale-space 第6回 - Pythonで画像処理をやってみよう!第6回 - Scale-space 第3回 - Python で OAuth2 をつかってみよう! (Pythonで作って学ぶ) Youtube の動画リストを作るアプリの開発
第4回 (Pythonで作って学ぶ) Youtube の動画リストを作るアプリの開発
第2回 JavaScript でパックマン!第4回
- 1. MPS Setagaya 第7回 (2015/10/11) ミーティング
JavaScript でパックマン! 第4回
金子純也
Morning Project Samurai 代表
MPS Setagaya 第7回 (2015/10/11) ミーティング (c) Junya Kaneko
- 7. Step1. 手動で口パク!
var cv = document.getElementById(‘pac-man');
var ctx = cv.getContext("2d");
function drawPacman(ctx ,cx, cy){
ctx.strokeStyle="#FF0000";
ctx.fillStyle="#FF0000";
ctx.beginPath();
ctx.arc(cx, cy, 50, 30 * Math.PI / 180, 330 * Math.PI / 180);
ctx.lineTo(cx, cy);
ctx.lineTo(cx + 50 * Math.cos(30 * Math.PI / 180), cy +
50 * Math.sin(30 * Math.PI / 180));
ctx.fill();
}
drawPacman(ctx, 50, 50);
θ=30
MPS Setagaya 第7回 (2015/10/11) ミーティング (c) Junya Kaneko
- 8. Step1. 手動で口パク!
var cv = document.getElementById(‘pac-man');
var ctx = cv.getContext("2d");
function drawPacman(ctx ,cx, cy, theta){
ctx.strokeStyle="#FF0000";
ctx.fillStyle="#FF0000";
ctx.beginPath();
ctx.arc(cx, cy, 50, theta * Math.PI / 180,
(360 - theta) * Math.PI / 180);
ctx.lineTo(cx, cy);
ctx.lineTo(cx + 50 * Math.cos(theta * Math.PI / 180),
cy + 50 * Math.sin(theta * Math.PI / 180));
ctx.fill();
}
drawPacman(ctx, 50, 50, 30);
drawPacman(ctx, 150, 50, 10);
θ=theta
MPS Setagaya 第7回 (2015/10/11) ミーティング (c) Junya Kaneko
- 9. Step2. 自動でパクパクさせる
var cv = document.getElementById(‘pac-man');
var ctx = cv.getContext("2d");
var cx = 50, cy = 50, theta = 30, dTheta = -1;
function drawPacman(ctx ,cx, cy, theta){ … }
function animationLoop(timestamp) {
if (theta >= 30) dTheta = -1;
else if (theta <=0 ) dTheta = 1;
theta += dTheta;
ctx.clearRect(0, 0, cv.width, cv.height);
drawPacman(ctx, cx, cy, theta);
window.requestAnimationFrame(animationLoop);
}
window.requestAnimationFrame(animationLoop);MPS Setagaya 第7回 (2015/10/11) ミーティング (c) Junya Kaneko
- 13. イベントの例
• onkeydown: キーの押下イベント
• onkeyup: キーが離されたイベント
• onmousedown: マウスのボタンが押下された
• onmouseup: マウスのボタンが離された
MPS Setagaya 第7回 (2015/10/11) ミーティング (c) Junya Kaneko
- 14. 要素へのイベントハンドラの登録
cv.tabIndex = 1;
cv.onkeydown = function(event) {
event.preventDefault();
switch(event.keyCode) {
case 37:
cx -= 1; break;
case 38:
cy -= 1; break;
case 39:
cx += 1; break;
case 40:
cy += 1; break;
}
};
MPS Setagaya 第7回 (2015/10/11) ミーティング (c) Junya Kaneko
- 18. 改良版 onkeydown イベントハンドラ
var moveDirection = [0, 0] //x, y
cv.tabIndex = 1;
cv.onkeydown = function(event) {
event.preventDefault();
switch(event.keyCode) {
case 37:
moveDirection[0] = -1; break;
case 38:
moveDirection[1] = -1; break;
case 39:
moveDirection[0] = 1; break;
case 40:
moveDirection[1] = 1;
}
}; MPS Setagaya 第7回 (2015/10/11) ミーティング (c) Junya Kaneko
- 19. 改良版 描画関数
function animationLoop(timestamp) {
if (theta >= 30) dTheta = -1;
else if (theta <=0 ) dTheta = 1;
theta += dTheta;
cx += moveDirection[0];
cy += moveDirection[1];
ctx.clearRect(0, 0, cv.width, cv.height);
drawPacman(ctx, cx, cy, theta);
window.requestAnimationFrame(animationLoop);
}
MPS Setagaya 第7回 (2015/10/11) ミーティング (c) Junya Kaneko
- 21. cv.onkeyup = function(event) {
event.preventDefault();
switch(event.keyCode) {
case 37:
moveDirection[0] = 0;
break;
case 38:
moveDirection[1] = 0;
break;
case 39:
moveDirection[0] = 0;
break;
case 40:
moveDirection[1] = 0;
}
}; MPS Setagaya 第7回 (2015/10/11) ミーティング (c) Junya Kaneko