画布恢复颜色
Canvas Restore color
如果将鼠标悬停在网格上,方块将变为红色。现在我想让它们在 1 秒后使用淡出效果变回原始颜色(黑色)。有人可以帮忙吗?
我已经尝试了类似于ctx.restore的东西,但我想我没有正确实现它,因为它什么也没做。
<!DOCTYPE HTML>
<html>
<head>
<style>
body {
margin: 0px;
padding: 0px;
overflow:hidden;
}
#wrap {
width: 600px;
height: 600px;
}
</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>
感谢您的任何和所有反馈!
若要使正方形淡出,可以使用一个间隔,该间隔将定期以越来越低的不透明度重新绘制矩形。
对于每个矩形,您必须存储一些数据:x,y,颜色,淡出开始的时间,然后是间隔(因此您可以清除它以避免应用程序变得越来越慢)。
我敢打赌,通过阅读代码,你会明白这一切,但不要犹豫,问。
(JSBIN在这里:http://jsbin.com/gufikuwutu/1/edit或下面的代码片段:)
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();
var lastRect = null;
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;
// ignore this rect if we allready drawn it.
if (lastRect && lastRect.cx == cx && lastRect.cy == cy) return;
// pickup random color
var randomHue = Math.floor(Math.random() * 360);
var randomColor = 'hsl(' + randomHue + ', 85%, 60%)';
ctx.fillStyle = randomColor;
ctx.fillRect(cx + 1, cy + 1, cw - 3, ch - 3);
// setup rect data
var rectInfo = {
cx: cx,
cy: cy,
t: Date.now(),
color: randomColor,
interval: 0
};
// setup interval
rectInfo.interval = setInterval(fadeOutRect.bind(null, rectInfo), 30);
lastRect = rectInfo;
};
function fadeOutRect(rectInfo) {
var now = Date.now();
var elapsed = now - rectInfo.t;
var max = 1800;
// clear the rect.
ctx.fillStyle = "#000";
ctx.fillRect(rectInfo.cx + 1, rectInfo.cy + 1, cw - 3, ch - 3);
// exit if too much time elapsed.
if (elapsed > max) {
clearInterval(rectInfo.interval);
return;
}
// draw the rect with an opacity proportionnal to time elapsed.
ctx.save();
ctx.globalAlpha = 1 - elapsed / max;
ctx.fillStyle = rectInfo.color;
ctx.fillRect(rectInfo.cx + 1, rectInfo.cy + 1, cw - 3, ch - 3);
ctx.restore();
}
body {
margin: 0px;
padding: 0px;
overflow: hidden;
}
#wrap {
width: 600px;
height: 600px;
}
<div class="wrap">
<canvas id="canvas" width="5000" height="3000"></canvas>
</div>
相关文章:
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 高亮显示时编辑文本大小和颜色
- 为什么不是't窗口.恢复正常工作吗?(javascript/jquery)
- 用与线条相同的颜色填充多折线图上的点
- 更改使用Chart.js创建的图表中的轴线颜色
- 如何使用jquery更改html中的背景颜色
- 如何临时暂停浏览器渲染,然后恢复整个页面
- NodeJS-readline暂停和恢复事件发射器(逐行读取)
- 动态更改高图中的系列颜色
- 谷歌地图劫持了iphone's滚动(触摸事件)-如何恢复
- jsf中两个字符串的颜色代码差异
- 我们如何使用css或JavaScript在i/j上更改句点(点)的颜色
- 单击功能应反转图像的颜色并恢复先前反转图像的颜色
- 三.js - 鼠标悬停效果不会恢复以前的颜色
- 画布恢复颜色
- 是否可以在每次单击按钮时更改背景颜色并恢复为纯JavaScript中的原始颜色
- 单击时切换按钮的颜色(并恢复其他按钮的颜色)
- D3.js在mouseout上恢复以前的颜色
- 如何将按钮恢复到原来的颜色?
- (HighCharts)手动鼠标悬停后,鼠标悬停时颜色恢复