透明这种颜色的html,透明颜色HTML5帆布

博主创建了一个使用JavaScript和HTML5 Canvas的Pong游戏,遇到问题:当设置Canvas背景为透明时,游戏元素(球和划桨)不再正常绘制,而是颜色混乱。尝试移除背景填充或调整代码无果。讨论中提到可能未调用'ctx.closePath',但未解决。
摘要由CSDN通过智能技术生成

我有一个简单的Pong游戏,用一些Javascript和Canvas标签制作。透明颜色HTML5帆布

我将画布标记的背景颜色设置为透明,以便可以在画布下显示div标记的背景图像。

问题是,当我将它设置为透明时,它不会像正常的十六进制颜色那样正确地绘制球和划桨。它绘制了桨和球的路径,屏幕最终转变了球的颜色。

的JavaScript代码是:

//canvas

var Width = 988;

var Height = 310;

var canvas = document.getElementById("Pong");

canvas.width = Width;

canvas.height = Height;

canvas.setAttribute('tabindex', 1);

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

var FPS = 1000/60;

var BG = {

Color: 'transparent',

Paint: function(){

ctx.fillStyle = this.Color;

ctx.fillRect(0, 0, Width, Height);

}

};

//var Mouse = {X: 0, Y: 0};

var Ball = {

Radius: 6,

Color: '#ffffff',

X: 0,

Y: 0,

VelX: 0,

VelY: 0,

Paint: function(){

ctx.beginPath();

ctx.fillStyle = this.Color;

ctx.arc(this.X, this.Y, this.Radius, 0, Math.PI * 2, false);

ctx.fill();

this.Update();

},

Update: function(){

this.X += this.VelX;

this.Y += this.VelY;

},

Reset: function(){

this.X = Width/2;

this.Y = Height/2;

this.VelX = (!!Math.round(Math.random() * 1) ? 1.5 : -1.5);

this.VelY = (!!Math.round(Math.random() * 1) ? 1.5 : -1.5);

}

};

function Paddle(position){

this.Color = '#ffffff';

this.Width = 15;

this.Height = 60;

this.X = 0;

this.Y = Height/2 - this.Height/2;

this.Score = 0;

if(position == 'left')

this.X = 50;

else this.X = 938;

this.Paint = function(){

ctx.fillStyle = this.Color;

ctx.fillRect(this.X, this.Y, this.Width, this.Height);

ctx.fillStyle = this.Color;

ctx.font = "normal 10pt Calibri";

if(position == 'left'){

ctx.textAlign = "left";

ctx.fillText("score: " + Player.Score, 10, 10);

}else{

ctx.textAlign = "right";

ctx.fillText("score: " + Computer.Score, Width - 10, 10);

}

};

this.IsCollision = function() {

if (Ball.X - Ball.Radius > this.Width + this.X || this.X > Ball.Radius * 2 + Ball.X - Ball.Radius)

return false;

if (Ball.Y - Ball.Radius > this.Height + this.Y || this.Y > Ball.Radius * 2 + Ball.Y - Ball.Radius)

return false;

return true;

};

};

window.requestAnimFrame = (function(){

return window.requestAnimationFrame

|| window.webkitRequestAnimationFrame

|| window.mozRequestAnimationFrame

|| window.oRequestAnimationFrame

|| window.msRequestAnimationFrame

|| function(callback){ return window.setTimeout(callback, FPS); }; }

)();

window.cancelRequestAnimFrame = (function() {

return window.cancelAnimationFrame

|| window.webkitCancelRequestAnimationFrame

|| window.mozCancelRequestAnimationFrame

|| window.oCancelRequestAnimationFrame

|| window.msCancelRequestAnimationFrame

|| clearTimeout }

)();

//game

var Computer = new Paddle();

var Player = new Paddle('left');

//event listener

function MouseMove(e){

Player.Y = e.pageY - Player.Height/2;

}

//attache event

canvas.addEventListener("mousemove", MouseMove, true);

function Paint(){

ctx.beginPath();

BG.Paint();

Computer.Paint();

Player.Paint();

Ball.Paint();

}

function Loop(){

init = requestAnimFrame(Loop);

Paint();

if(Player.IsCollision() || Computer.IsCollision()){

Ball.VelX = Ball.VelX * -1;

Ball.VelX += (Ball.VelX > 0 ? 0.5 : -0.5);

if(Math.abs(Ball.VelX) > Ball.Radius * 1.5)

Ball.VelX = (Ball.VelX > 0 ? Ball.Radius * 1.5 : Ball.Radius * -1.5);

}

if(Ball.Y - Ball.Radius < 0 || Ball.Y + Ball.Radius > Height)

Ball.VelY = Ball.VelY * -1;

if(Ball.X - Ball.Radius <= 0){

Computer.Score++;

Ball.Reset();

}else if(Ball.X + Ball.Radius > Width){

Player.Score++;

Ball.Reset();

}

if(Computer.Score === 10)

GameOver(false);

else if(Player.Score === 10)

GameOver(true);

Computer.Y = (Computer.Y + Computer.Height/2 < Ball.Y ? Computer.Y + Computer.Vel : Computer.Y - Computer.Vel);

};

function GameOver(win){

cancelRequestAnimFrame(init);

BG.Paint();

ctx.fillStyle = "#ffffff";

ctx.font = "bold 40px Calibri";

ctx.textAlign = "center";

ctx.fillText((win ? "A WINNER IS YOU" : "GAME OVER"), Width/2, Height/2);

ctx.font = "normal 16px Calibri";

ctx.fillText("refresh to reply", Width/2, Height/2 + 20);

}

function NewGame(){

Ball.Reset();

Player.Score = 0;

Computer.Score = 0;

Computer.Vel = 1.25;

Loop();

}

NewGame();

我试图简单地移除BG无功,但它只是做同样的事情。

更新:我也试过有和没有ctx.closePath没有成功。

2013-10-24

Carl

+0

你似乎没有调用'ctx.closePath'这可能会导致问题。 –

+0

@david感谢您的评论。我只是试图没有成功。 –

+1

这并不重要,除非您尝试“行程”路径。对于'fill',它会自动关闭。 –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
养虾的池塘材料一般有水泥、塑料、土壤等,其中比较常见的是帆布池。以下是帆布池养虾的教程及注意事项: 1. 池塘选址:选择一块平地,避免池塘建在低洼处或水流较快的地方。 2. 池塘准备:根据需要的池塘大小,选择合适尺寸的帆布池,清理池塘周围的杂草、石头、树枝等,确保池塘周围干净整洁。 3. 帆布池铺设:将帆布池铺放在平整的地面上,注意帆布池四周的边缘要铺平,以免发生漏水现象。 4. 池塘充水:在池塘内加入适量的水,水深不宜过浅或过深,一般建议水深在1.5米左右。 5. 添加养殖饵料:根据虾的品种和养殖的目的,选择适合的饵料,比如鱼粉、豆粉、米糠等。在虾苗投放前,需在水中添加充足的饵料,以促进虾苗的生长。 6. 投放虾苗:在虾池内投放虾苗,投放数量根据池塘的大小而定。虾苗投放前需进行适当的处理,比如消毒、浸泡等,以消除害虫和疾病的影响。 7. 日常养护:定期清理池塘一周围的杂草和底泥,避免过度积累。同时,要注意池塘水质的保持,定期检测水质,确保养殖环境健康。 注意事项: 1. 帆布池养虾的水源要保持清洁,避免水中杂质和有害物质的污染。 2. 养殖过程中,要注意虾苗的生长情况,及时调整养殖饵料的种类和用量。 3. 避免池塘过度养殖,以免引起疾病和污染。 4. 在池塘投放虾苗前,一定要进行虾苗的检疫和消毒,预防疾病的发生。 5. 养殖过程中,要注意池塘的通风和保湿,避免池塘水温过高或过低,影响虾的生长。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值