JS對象屬性定義與操作,類的定義與對象的實(shí)例化

代碼一:




<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>小球運(yùn)動</title>

<style>

canvas{

border: 1px #000 solid

}

</style>

</head>

<body>

<div>

<canvas id="canvas" width="500" height="200"></canvas>

</div>

</body>

<script>

var canvas=document.querySelector("#canvas");

var ctx = canvas.getContext('2d');

var circle={//定義一個對象

"r":255,

"g":0,

"b":0,

"a":1,

"r":10,

"x":10,

"y":100,

"angle":0,

DrawCircle: function (ctx) {

? ? ? ? ? ? ? ? ctx.fillStyle = 'rgba(' + 255 + ',' + this.g + ',' + this.b + ',' + this.a + ')';

? ? ? ? ? ? ? ? ctx.beginPath();

? ? ? ? ? ? ? ? ctx.arc(this.x, this.y, this.r, 0, 360, false);

? ? ? ? ? ? ? ? ctx.fill();

? ? ? ? ? ? ? ? ctx.closePath();

? ? ? ? ? ? },

? ? ? ? ? ? CircleMove: function () {

? ? ? ? ? ? ? ? var scale = this.angle / 180 * Math.PI;

? ? ? ? ? ? ? ? var dx = 8;

? ? ? ? ? ? ? ? var dy = -14 * Math.cos(scale);

? ? ? ? ? ? ? ? this.x += dx;

? ? ? ? ? ? ? ? this.y += dy;

? ? ? ? ? ? }

? ? ? ? }; //創(chuàng)建一個對象

function Draw(){

ctx.clearRect(0, 0, canvas.width, canvas.height);

circle.DrawCircle(ctx);

? ? ? ? circle.CircleMove();

? ? ? ? circle.angle+=10;

? ? ? ? circle.a -= 0.01;//對象數(shù)值操作

? ? ? ? if(circle.alpha<=0){

? ? ? ? return;

? ? ? ? }

? ? ? ? setTimeout(Draw, 100);

}

Draw();

</script>

</html>


代碼二:

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>小球運(yùn)動</title>

<style>

canvas{

border: 1px #000 dashed

}

</style>

</head>

<body>

<div>

<canvas id="canvas" width="500" height="200"></canvas>

</div>

</body>

<script>

var canvas=document.querySelector("#canvas");

var ctx = canvas.getContext('2d');

? ? function particle(colorr, colorg, colorb, r, x, y, speed, angle, alpha) {

? ? ? ? var obj = {//定義一個類

? ? ? ? ? ? colorr: colorr,

? ? ? ? ? ? colorg:colorg,

? ? ? ? ? ? colorb:colorb,

? ? ? ? ? ? r: r,

? ? ? ? ? ? x: x,

? ? ? ? ? ? y: y,

? ? ? ? ? ? speed:speed,

? ? ? ? ? ? angle: angle,

? ? ? ? ? ? alpha: alpha,

? ? ? ? ? ? draw: function (ctx) {

? ? ? ? ? ? ? ? ctx.fillStyle = 'rgba(' + this.colorr + ',' + this.colorg + ',' + this.colorb + ',' + this.alpha + ')';

? ? ? ? ? ? ? ? ctx.beginPath();

? ? ? ? ? ? ? ? ctx.arc(this.x, this.y, this.r, 0, 360, false);

? ? ? ? ? ? ? ? ctx.fill();

? ? ? ? ? ? ? ? ctx.closePath();

? ? ? ? ? ? },

? ? ? ? ? ? move: function () {

? ? ? ? ? ? ? ? var scale = this.angle / 180 * Math.PI;

? ? ? ? ? ? ? ? var dx = this.speed;

? ? ? ? ? ? ? ? var dy = -14 * Math.cos(scale);

? ? ? ? ? ? ? ? this.x += dx;

? ? ? ? ? ? ? ? this.y += dy;

? ? ? ? ? ? }

? ? ? ? };?

? ? ? ? return obj;

? ? }

var circle=particle(255,0,0,10, 10, 100, 8, 0, 1)//類實(shí)例化成對象

function Draw(){

ctx.clearRect(0, 0, canvas.width, canvas.height);

circle.draw(ctx);

? ? ? ? circle.move();

? ? ? ? circle.angle+=10;

? ? ? ? circle.alpha -= 0.01;

? ? ? ? if(circle.alpha<=0){

? ? ? ? return;

? ? ? ? }

? ? ? ? setTimeout(Draw, 100);

}

Draw();

</script>

</html>

代碼三:

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>小球運(yùn)動</title>

<style>

canvas{

border: 1px #000 dashed

}

</style>

</head>

<body>

<div>

<canvas id="canvas" width="500" height="200"></canvas>

</div>

</body>

<script>

var canvas=document.querySelector("#canvas");

var ctx = canvas.getContext('2d');

? ? function particle(colorr, colorg, colorb, r, x, y, speed, angle, alpha) {

? ? ? ? ? ? this.colorr=colorr,

? ? ? ? ? ? this. colorg=colorg,

? ? ? ? ? ? this.colorb=colorb,

? ? ? ? ? ? this.r=r,

? ? ? ? ? ? this.x=x,

? ? ? ? ? ? this.y=y,

? ? ? ? ? ? this.speed=speed,

? ? ? ? ? ? this.angle=angle,

? ? ? ? ? ? this.alpha=alpha,

? ? ? ? ? ? this.draw=function (ctx) {

? ? ? ? ? ? ? ? ctx.fillStyle = 'rgba(' + this.colorr + ',' + this.colorg + ',' + this.colorb + ',' + this.alpha + ')';

? ? ? ? ? ? ? ? ctx.beginPath();

? ? ? ? ? ? ? ? ctx.arc(this.x, this.y, this.r, 0, 360, false);

? ? ? ? ? ? ? ? ctx.fill();

? ? ? ? ? ? ? ? ctx.closePath();

? ? ? ? ? ? },

? ? ? ? ? ? this.move=function () {

? ? ? ? ? ? ? ? var scale = this.angle / 180 * Math.PI;

? ? ? ? ? ? ? ? var dx = this.speed;

? ? ? ? ? ? ? ? var dy = -14 * Math.cos(scale);

? ? ? ? ? ? ? ? this.x += dx;

? ? ? ? ? ? ? ? this.y += dy;

? ? ? ? ? ? }

? ? }

var circle=new particle(255,0,0,10, 10, 100, 8, 0, 1)//類實(shí)例化成對象

function Draw(){

ctx.clearRect(0, 0, canvas.width, canvas.height);

circle.draw(ctx);

? ? ? ? circle.move();

? ? ? ? circle.angle+=10;

? ? ? ? circle.alpha -= 0.01;

? ? ? ? if(circle.alpha<=0){

? ? ? ? return;

? ? ? ? }

? ? ? ? setTimeout(Draw, 100);

}

Draw();

</script>

</html>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些閱讀 2,132評論 0 2
  • 一:canvas簡介 1.1什么是canvas? ①:canvas是HTML5提供的一種新標(biāo)簽 ②:HTML5 ...
    GreenHand1閱讀 4,874評論 2 32
  • 上網(wǎng)搜索了angularjs裁剪,發(fā)現(xiàn)只有正方形和圓形 http://www.cnblogs.com/smilec...
    四腳蛇閱讀 834評論 0 1
  • 1 Canvas接口元素定義 1.1 getContext()方法 為了在canvas上繪制,你必須先得到一個畫布...
    Kevin_Junbaozi閱讀 1,445評論 1 2
  • 孩子第一次劃船 在公園,第一次劃船,劃的是電動船,把她自己挑選的,塑料皇冠,掉到了水里,這應(yīng)該是她劃船最傷心的地方...
    我心我愿秀閱讀 521評論 0 4

友情鏈接更多精彩內(nèi)容