我有一个可视化器设置,它每7.5秒改变一次颜色,问题是,它会立即执行,你可以在fillStyle上使用.animate()吗?如果没有,是否有任何添加到fillStyle的转换的方法?fillStyle颜色上的过渡
回答
fillStyle
没有自动为其转换设置动画的方法。
但是,它很容易创建动画过渡的动画循环。
你要做的就是提醒你疯狂的内容两次每帧的动画循环存在的过度2.50秒:
战平具有超过您2.50秒降低不透明度起始颜色。
绘制结束颜色的不透明度增加超过2.50秒。
这里的注释的示例代码和演示:
// canvas related variables
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var cw=canvas.width;
var ch=canvas.height;
var duration=2.50;
// starting and ending colors
var rgbStart='#BC49FF';
var rgbEnd='#574FFF';
// calculate the # of frames that requestAnimationFrame can
// draw during the duration
var opacitySteps=parseInt(60*duration);
// set the current opacity step at its starting number (0)
var opacityStep=0;
// start the 2.5 second animation
requestAnimationFrame(animate);
function animate(time){
// calculate the current opacity as a percentage
// of opacityStep/opacitySteps
var opacity=100*(opacityStep/opacitySteps);
if(opacityStep>=opacitySteps-1){ opacity=100; }
// clear the canvas
ctx.clearRect(0,0,cw,ch);
// draw with the starting color using a lessening opacity
ctx.globalAlpha=(100-opacity)/100;
ctx.fillStyle=rgbStart;
ctx.fillRect(20,20,100,75);
ctx.strokeRect(20,20,100,75);
// draw with the ending color using a increasing opacity
ctx.globalAlpha=(opacity)/100;
ctx.fillStyle=rgbEnd;
ctx.fillRect(20,20,100,75);
ctx.strokeRect(20,20,100,75);
// clean up, reset globalAlpha to it's default of 1.00
ctx.globalAlpha=1.00;
// return if all steps have been played
if(++opacityStep>=opacitySteps){return;}
// otherwise request another frame
requestAnimationFrame(animate);
}
$('#again').click(function(){
opacityStep=0;
requestAnimationFrame(animate);
})
body{ background-color: ivory; }
#canvas{border:1px solid red;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<h4>Transitioning a fillStyle over 2.50 seconds</h4>
<br><button id=again>Again</button>
<br><canvas id="canvas" width=300 height=300></canvas>
有没有办法做到这一点,没有requestAnimationFrame?因为我之前已经拥有了它,并且它刚刚破损 – 2015-04-04 16:57:57
当然,您可以使用'setInterval'以2.50秒的间隔调用动画帧。当2.50秒过后,你可以调用'clearInterval'来停止动画循环。话虽如此,使用'requestAnimationFrame'是最好的编程实践(出于性能和其他原因),如果将它添加到工具包中,它将对您的编程有所帮助;-)祝您的项目顺利! – markE 2015-04-04 17:04:04
我已经在我的网站上使用过3次了,但颜色真的很快就变了 – 2015-04-04 17:09:18
- 1. 颜色过渡背景html
- 2. 在YUI中过渡颜色?
- 3. 颜色过渡总是先从黑色
- 4. 从黑与白的过渡到颜色?
- 5. Css过渡与背景颜色
- 6. 过渡动画背景颜色
- 7. 在Android中查看颜色过渡onScroll
- 8. DirectX 9 +着色器效果:禁用颜色的平滑过渡
- 9. 不同的fillStyle颜色在画布中的圆弧
- 10. 如何淡入div中的背景颜色之间的过渡?
- 11. 如何使用动态图案和颜色设置Html5 Canvas FillStyle
- 12. 如何判断fillStyle是否被指定为非法颜色?
- 13. 同时改变fillStyle作为背景颜色
- 14. 错误用简单的翻转颜色过渡,请帮助
- 15. 甜甜圈图 - 地图域的颜色范围(过渡)
- 16. 背景图像和背景颜色之间的CSS过渡
- 17. 如何过渡不同的背景颜色,延迟效果
- 18. 如何从左到右应用textview的颜色过渡android?
- 19. CSS3过渡(背景颜色)拒绝在Firefox中工作
- 20. css过渡:背景颜色和宽度'取消'对方?
- 21. SVG颜色过渡不起作用悬停
- 22. Android Canvas从白色过渡到红色
- 23. 如何减少拼接图像之间的颜色差异以实现平滑的颜色过渡?
- 24. 为什么canvas fill()不使用fillStyle中指定的颜色绘制形状?
- 25. Firefox上的过渡特性
- 26. 颜色变化悬停的过渡也会影响显示没有应用
- 27. 如何使用jQuery的slideToggle()和CSS实现颜色过渡效果?
- 28. CSS3:过渡简化版,工作时的背景颜色是渐变
- 29. -webkit-过渡/ -moz-过渡VS jQuery的
- 30. jQuery的过渡
尝试过渡期 - https://jsfiddle.net/dhvbde8w/ – Tasos 2015-04-04 16:39:47
我需要做一个画布fillStyle属性的过渡,而不是一个div – 2015-04-04 16:53:58