【发布时间】:2021-03-06 09:57:17
【问题描述】:
所以我想用 HTML 和 Javascript 制作 DVD 弹跳屏幕,但我无法让球成为 DVD 徽标并在弹跳发生球或图像时更改颜色。有谁知道如何将球更改为图像并在反弹发生时更改其颜色?
<html>
<head>
</head>
<body>
<script>
var context;
var x=100;
var y=100;
var dx=5;
var dy=2;
function init()
{
context= myCanvas.getContext('2d');
setInterval(draw,20);
}
function draw()
{
//base_image = new Image();
//base_image.src = 'dvd.png';
//context.drawImage(base_image, 100, 100);
//base_image.arc(x,y,20,0,Math.PI*2,true);
context.clearRect(0,0, 600,600);
context.beginPath();
context.fillStyle="#0000ff";
//Draws a circle of radius 20 at the coordinates 100,100 on the canvas
context.arc(x,y,20,0,Math.PI*2,true);
context.closePath();
context.fill();
// Boundary Logic
if (x < 0 || x > 600){
dx = -dx;
context.fillStyle="#77ff00";
}
if (y < 0 || y > 600){
dy = -dy;
}
x+=dx;
y+=dy;
}
</script>
//<img src="dvd.png" id="dvd" width="100" height="100">
<body onLoad="init();">
<canvas id="myCanvas" width="600" height="600" >
</canvas>
</body>
</html>
【问题讨论】:
标签: javascript image colors