我有一個可視化器設置,它每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