아래 코드는 47 행이 48 행으로 대체 될 때까지 (즉 fillStyle 계산) 작동합니다. 어떤 이유로 rgba의 파란색 부분을 계산하기 위해 clientY를 사용하려고 시도하지만, 알파를 계산하는 것이 문제가되지 않습니다.clientX/clientY를 사용하는 rgba의 실시간 업데이트?
<!DOCTYPE html>
<html>
<head>
<title> My Animation </title>
</head>
<body>
<canvas id="myCanvas" height="768" width="1200"></canvas>
</body>
<script>
var mainCanvas = document.getElementById("myCanvas");
var ctx = mainCanvas.getContext('2d');
var canvasWidth = mainCanvas.width;
var canvasHeight = mainCanvas.height;
// the array of squares
var squares = [];
var mouseX = 1200;
var mouseY = 768;
var putPoint = function (e) {
// update mouse
mouseX = e.clientX;
mouseY = e.clientY;
}
mainCanvas.addEventListener("click", putPoint);
for (i = 0; i < 100; i++) {
squares.push(new Square());
}
function Square() {
this.x = Math.random() * 1024;
this.y = Math.random() * 768;
this.inc = Math.random() * 360;
this.angle = 0;
this.speed = Math.random() * 6 - 3;
}
Square.prototype.update = function() {
ctx.save();
ctx.translate(this.x, this.y);
ctx.rotate(this.angle);
ctx.beginPath();
ctx.rect(-25, -25, 50, 50);
ctx.closePath();
// ctx.fillStyle = "rgba(255,0," + 150 + "," + mouseX/1024 + ")";
ctx.fillStyle = "rgba(255,0," + mouseY/768*255 + "," + mouseX/1024 + ")";
console.log(mouseY/768*255);
ctx.fill();
ctx.restore();
this.inc = this.inc + this.speed;
this.angle = this.inc % 360/57.2958;
}
function draw() {
ctx.clearRect(0, 0, 1024, 768);
for (var i = 0; i < squares.length; i++) {
var myBox = squares[i];
myBox.update();
}
requestAnimationFrame(draw);
}
draw();
</script>
시도가를 디버깅 : CONSOLE.LOG ("RGBA (255,0를"쥐의/768 * 255 + ','+ mouseX/1,024 + ")는"+) 볼 왜 가치가 부정확 한가? – Zydnar