0
그리드 위에 마우스를 올려 놓으면 사각형이 빨갛게 변합니다. 이제는 페이드 아웃 효과를 사용하여 1 초 후에 다시 원래 색상 (검정색)으로 변경하고 싶습니다. 누군가 도울 수 있습니까?캔버스 색 복원
나는 ctx.restore의 라인을 따라 뭔가를 시도했지만 아무 것도하지 않았으므로 올바르게 구현하지 않았다고 생각한다.
<!DOCTYPE HTML>
<html>
<head>
<style>
body {
margin: 0px;
padding: 0px;
\t \t overflow:hidden;
}
\t
\t #wrap {
\t \t width: 600px;
\t \t height: 600px;
\t }
</style>
</head>
<body bgcolor="#252525">
<div class="wrap"><canvas id="canvas" width="5000" height="3000"></canvas></div>
<script>
var ctx = canvas.getContext("2d"),
cw = 32,
ch = 32,
w = canvas.width,
h = canvas.height;
ctx.translate(0.5, 0.5);
ctx.beginPath();
for(var y = 0; y < h; y += ch) {
for(var x = 0; x < w; x += cw) {
ctx.rect(x, y, cw-2, ch-2); // give 1px space
}
}
ctx.fillStyle = "black";
ctx.strokeStyle = "#000";
ctx.lineWidth=1;
ctx.fill();
ctx.stroke();
canvas.onmousemove = function(e) {
var rect = this.getBoundingClientRect(),
x = e.clientX - rect.left,
y = e.clientY - rect.top,
cx = ((x/cw)|0) * cw,
cy = ((y/ch)|0) * ch;
ctx.fillStyle = "red";
ctx.fillRect(cx+1, cy+1, cw-3, ch-3);
};
</script>
</body>
</html>
당신은 할 수 귀하의 fillRect 할 타임 아웃을 사용 후 https://developer.mozilla.org/en-US/ docs/Web/API/WindowTimers.setTimeout을 사용하여 이전 색상으로 되돌립니다. –
이게 뭔가요? http://jsbin.com/poqezohisi/1/edit?js,output – GameAlchemist
예! 고마워요! "붉은 색"도 무작위로 배정하는 법을 알게됩니까? 빨간색 대신 빨강, 파랑, 녹색 등 임의의 색상을 얻습니다. –