HTML5 ゲームフレームワーク cnGameJS 開発記録 - 基本グラフィックモジュール
1. 関数
このモジュールも非常にシンプルで、主に長方形、円、テキストの3つの基本グラフィックの描画が含まれています。たとえば、長方形の オブジェクトを描画する必要がある場合、まず長方形のオブジェクトを new作成し、次に描画するオブジェクトのdrawメソッドを呼び出します。例: var rect=new cnGame.shape.Rect();
rect.draw();
このモジュールには 3 つのグラフィックス オブジェクトが含まれているため、それぞれに描画、移動、回転、サイズ変更などの独自のさまざまなメソッドがある 3 つのコンストラクターを確立します。 3 つのオブジェクトのメソッドは非常に似ています。説明するために長方形のオブジェクトを例に挙げて説明します。まずコンストラクターを見てみましょう:
/** *矩形对象 **/ var rect=function(options){ if(!(this instanceof arguments.callee)){ return new arguments.callee(options); } this.init(options); }
この関数がコンストラクターとして呼び出されない場合、新しいメソッドが返されることに注意してください。コンストラクター
として関数が常に呼び出され、結果として得られる四角形オブジェクトを返します。次に、初期化のために init を呼び出します。さらに、各オブジェクトには異なるプロパティがありますが、オブジェクトのデフォルト オブジェクトも設定する必要があります。ここでは、コア モジュールの extend 関数を使用して、ユーザーが設定したパラメータをデフォルト オブジェクトのパラメータとマージする必要があります:
/** *默认值对象 **/ var defaultObj={ x:0, y:0, width:100, height:100 }; options=options||{}; options=cg.core.extend(defaultObj,options);
right
頂点とbottom 頂点を保存して、将来の長方形の衝突の検出を容易にします。この関数も、幅、高さ、xy に基づいて右と下を計算します。
/** *更新right和bottom **/ var resetRightBottom=function(elem){ elem.right=elem.x+elem.width; elem.bottom=elem.y+elem.height; }
/** *绘制矩形 **/ draw:function(style,isFill){ var context=cg.context; (cg.core.isUndefined(isFill))&&(isFill=true); if(isFill){ context.fillStyle = style; context.fillRect(this.x, this.y, this.width, this.height); } else{ context.strokeStyle = style; context.strokeRect(this.x, this.y, this.width, this.height); } return this; }
/** * *canvas基本形状对象 * **/ cnGame.register("cnGame.shape",function(cg){ /** *更新right和bottom **/ var resetRightBottom=function(elem){ elem.right=elem.x+elem.width; elem.bottom=elem.y+elem.height; } /** *矩形对象 **/ var rect=function(options){ if(!(this instanceof arguments.callee)){ return new arguments.callee(options); } this.init(options); } rect.prototype={ /** *初始化 **/ init:function(options){ /** *默认值对象 **/ var defaultObj={ x:0, y:0, width:100, height:100, style:"red", isFill:true }; options=options||{}; options=cg.core.extend(defaultObj,options); this.setOptions(options); resetRightBottom(this); }, /** *绘制矩形 **/ setOptions:function(options){ this.x=options.x; this.y=options.y; this.width=options.width; this.height=options.height; this.style=options.style; this.isFill=this.isFill; }, /** *绘制矩形 **/ draw:function(){ var context=cg.context; if(this.isFill){ context.fillStyle = this.style; context.fillRect(this.x, this.y, this.width, this.height); } else{ context.strokeStyle = this.style; context.strokeRect(this.x, this.y, this.width, this.height); } return this; }, /** *将矩形移动一定距离 **/ move:function(dx,dy){ dx=dx||0; dy=dy||0; this.x+=dx; this.y+=dy; resetRightBottom(this); return this; }, /** *将矩形移动到特定位置 **/ moveTo:function(x,y){ x=x||this.x; y=y||this.y; this.x=x; this.y=y; resetRightBottom(this); return this; }, /** *将矩形改变一定大小 **/ resize:function(dWidth,dHeight){ dWidth=dWidth||0; dHeight=dHeight||0; this.width+=dWidth; this.height+=dHeight; resetRightBottom(this); return this; }, /** *将矩形改变到特定大小 **/ resizeTo:function(width,height){ width=width||this.width; height=height||this.height; this.width=width; this.height=height; resetRightBottom(this); return this; } } /** *圆形对象 **/ var circle=function(options){ if(!(this instanceof arguments.callee)){ return new arguments.callee(options); } this.init(options); } circle.prototype={ /** *初始化 **/ init:function(options){ /** *默认值对象 **/ var defaultObj={ x:100, y:100, r:100, startAngle:0, endAngle:Math.PI*2, antiClock:false, style:"red", isFill:true }; options=options||{}; options=cg.core.extend(defaultObj,options); this.setOptions(options); }, /** *设置参数 **/ setOptions=function(options){ this.x=options.x; this.y=options.y; this.r=options.r; this.startAngle=options.startAngle; this.endAngle=options.endAngle; this.antiClock=options.antiClock; this.isFill=options.isFill; this.style=options.style; }, /** *绘制圆形 **/ draw:function(){ var context=cg.context; context.beginPath(); context.arc(this.x,this.y,this.r,this.startAngle,this.endAngle,this.antiClock); context.closePath(); if(this.isFill){ context.fillStyle=this.style; context.fill(); } else{ context.strokeStyle=this.style; context.stroke(); } }, /** *将圆形移动一定距离 **/ move:function(dx,dy){ dx=dx||0; dy=dy||0; this.x+=dx; this.y+=dy; return this; }, /** *将圆形移动到特定位置 **/ moveTo:function(x,y){ x=x||this.x; y=y||this.y; this.x=x; this.y=y; return this; }, /** *将圆形改变一定大小 **/ resize:function(dr){ dr=dr||0; this.r+=dr; return this; }, /** *将圆形改变到特定大小 **/ resizeTo:function(r){ r=r||this.r; this.r=r; return this; } } /** *将圆形改变到特定大小 **/ var text=function(text,options){ if(!(this instanceof arguments.callee)){ return new arguments.callee(text,options); } this.init(text,options); } text.prototype={ /** *初始化 **/ init:function(text,options){ /** *默认值对象 **/ var defaultObj={ x:100, y:100, style:"red", isFill:true }; options=options||{}; options=cg.core.extend(defaultObj,options); this.setOptions(options); this.text=text; }, /** *绘制 **/ draw:function(){ var context=cg.context; (!cg.core.isUndefined(this.font))&&(context.font=this.font); (!cg.core.isUndefined(this.textBaseline))&&(context.textBaseline=this.textBaseline); (!cg.core.isUndefined(this.textAlign))&&(context.textAlign=this.textAlign); (!cg.core.isUndefined(this.maxWidth))&&(context.maxWidth=this.maxWidth); if(this.isFill){ context.fillStyle=this.style; this.maxWidth?context.fillText(this.text,this.x,this.y,this.maxWidth):context.fillText(this.text,this.x,this.y); } else{ context.strokeStyle=this.style; this.maxWidth?context.strokeText(this.text,this.x,this.y,this.maxWidth):context.strokeText(this.text,this.x,this.y); } }, /** *设置参数 **/ setOptions:function(options){ this.x=options.x||this.x; this.y=options.y||this.y; this.maxWidth=options.maxWidth||this.maxWidth; this.font=options.font||this.font; this.textBaseline=options.textBaseline||this.textBaseline; this.textAlign=options.textAlign||this.textAlign; this.isFill=options.isFill||this.isFill; this.style=options.style||this.style; } } this.Text=text; this.Rect=rect; this.Circle=circle; });
以上がHTML5 ゲームフレームワーク cnGameJS 開発記録 - 基本グラフィックモジュールの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









HTML の表の境界線に関するガイド。ここでは、HTML でのテーブルの境界線の例を示しながら、テーブル境界線を定義する複数の方法について説明します。

HTML マージン左のガイド。ここでは、HTML margin-left の概要とその例、およびそのコード実装について説明します。

これは、HTML でのネストされたテーブルのガイドです。ここでは、テーブル内にテーブルを作成する方法をそれぞれの例とともに説明します。

HTML テーブル レイアウトのガイド。ここでは、HTML テーブル レイアウトの値と例および出力について詳しく説明します。

HTML 入力プレースホルダーのガイド。ここでは、コードと出力とともに HTML 入力プレースホルダーの例について説明します。

HTML でのテキストの移動に関するガイド。ここでは、概要、マーキー タグが構文でどのように機能するか、および実装例について説明します。

HTML オンクリック ボタンのガイド。ここでは、それらの紹介、動作、例、およびさまざまなイベントでの onclick イベントについてそれぞれ説明します。
