私は最善の方法で問題を提起したが、ここに完全にわからない...<canvas>メソッドをjavascriptのテンプレート内に宣言できますか?
私はHTML5のキャンバスのAPIで遊んでてきた、キャンバスに図形を描画して移動するために、それを得る限り持っている矢印キーで回してください。
私はさまざまな変数と関数をテンプレートに移動しようとしました。その結果、複数の図形が生成されました(最終的には異なるキーで制御されます)。
これは私が持っているものです。
function player(x, y, z, colour, speed){
this.lx = x;
this.ly = y;
this.speed = 10;
this.playerSize = z;
this.colour = colour;
}
playerOne = new player(100, 100, 10, "#F0F");
function persona(z, colour){
zone.fillStyle = colour;
offset = 0 - (z/2);
zone.fillRect(offset, offset, z, z);
}
function move(x, y){
playerOne.lx = playerOne.lx + x;
playerOne.ly = playerOne.ly + y;
zone.clearRect(0, 0, 500, 500);
zone.save();
zone.translate(playerOne.lx, playerOne.ly);
persona(playerOne.playerSize, playerOne.colour);
zone.restore();
}
window.onkeydown = function() {
var direction = this.event.keyCode;
var s = playerOne.speed;
// Arrow Keys
if(direction == 38 && playerOne.ly >= 10){ // Up
move(0,-s);
}
if(direction == 40 && playerOne.ly <= 490){ // Down
move(0,s);
}
if(direction == 37 && playerOne.lx >= 10){ // Left
move(-s,0);
}
if(direction == 39 && playerOne.lx <= 490){ // Right
move(s,0);
}
};
window.onload = function() {
zone = document.getElementById('canvas').getContext('2d');
zone.save();
zone.translate(playerOne.lx, playerOne.ly);
persona(playerOne.playerSize, playerOne.colour);
zone.restore();
};
だから、私は何をしようとしたことは、このようなプレーヤテンプレートにペルソナ機能を移動しました:それは
を言った前function player(x, y, z, colour, speed){
this.lx = x;
this.ly = y;
this.speed = 10;
function persona(){
zone.fillStyle = colour;
var offset = 0 - (z/2);
zone.fillRect(offset, offset, z, z);
}
}
そして、
persona(playerOne.playerSize, playerOne.colour);
今のところ
しかし、これは完全に外れていて動作していないため、わかりません。
私はおそらく間違っていると思いますが、問題は私がオブジェクト/テンプレート内からcanvas.context(スクリプト内のゾーンを呼び出す)を操作しようとしていることだと思います。
多分それはまったく関係がありません。私はテンプレートの文脈で正しく私の人格機能を宣言していないかもしれません。
キャンバスAPIのドキュメントは非常に薄く、正しい方向のヒントは非常に高く評価されます。
HTML 5仕様はまだ – Janie
はい(キャンバスを含む)、今後数年間で多くの変更に非常になりやすいですが、あるとして完全に使用可能であるので、APIは、より多くの、より安定になってきています。 –